add bootstrap script to start the server up the same way

This commit is contained in:
2026-04-03 18:01:18 +02:00
parent 4ca905fbf0
commit 1e57ad1af6
3 changed files with 45 additions and 29 deletions
+2 -12
View File
@@ -1,15 +1,5 @@
from server.Server import Server
import os
from server.bootstrap import build_server_from_env
server = Server(
data_path=os.path.dirname(__file__),
snake_type=os.environ.get("SNAKE", "BestBattleSnake"),
storage_type=os.environ.get("STORAGE", "LocalStorage"),
debug=os.environ.get("DEBUG_SERVER", False),
check_tls_security=False,
)
if os.environ.get("STORE_GAME_HISTORY", None):
server.enable_store_game_state()
server = build_server_from_env(default_snake_type="TemplateSnake")
app = server.app
+4 -17
View File
@@ -13,8 +13,9 @@
# For more info see docs.battlesnake.com
from server.CreateEnvironmentFile import CreateEnvironmentFile
from server.Server import Server
from server.bootstrap import build_run_config, build_server_from_env
import asyncio
import os
# Start server when `python main.py` is run
@@ -26,19 +27,5 @@ if __name__ == "__main__":
"SNAKE": "TemplateSnake",
})
server = Server(
data_path=os.path.dirname(__file__),
snake_type=os.environ.get("SNAKE", "TemplateSnake"),
storage_type=os.environ.get("STORAGE", "LocalStorage"),
debug=os.environ.get("DEBUG_SERVER", False),
check_tls_security=False,
)
if os.environ.get("STORE_GAME_HISTORY", None):
server.enable_store_game_state()
server.run(
host=os.environ.get("HOST", "0.0.0.0"),
port=int(os.environ.get("PORT", "8000")),
debug=bool(os.environ.get("DEBUG", False)),
)
server = build_server_from_env(default_snake_type="TemplateSnake")
asyncio.run(server.run(**build_run_config()))
+39
View File
@@ -0,0 +1,39 @@
from typing import TypedDict
from pathlib import Path
import os
from server.Server import Server
class RunConfig(TypedDict):
host: str
port: int
debug: bool
def env_bool(name:str, default:bool=False) -> bool:
value = os.environ.get(name)
if value is None:
return default
return value.lower() in {'1', 'true', 'yes', 'on'}
def build_server_from_env(default_snake_type:str) -> Server:
data_path = str(Path(__file__).resolve().parent.parent)
server = Server(
data_path=data_path,
snake_type=os.environ.get('SNAKE', default_snake_type),
storage_type=os.environ.get('STORAGE', 'LocalStorage'),
debug=env_bool('DEBUG_SERVER'),
check_tls_security=False,
)
if env_bool('STORE_GAME_HISTORY'):
server.enable_store_game_state()
return server
def build_run_config() -> RunConfig:
return {
'host': os.environ.get('HOST', '0.0.0.0'),
'port': int(os.environ.get('PORT', '8000')),
'debug': env_bool('DEBUG'),
}