add bootstrap script to start the server up the same way
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()))
|
||||
|
||||
@@ -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'),
|
||||
}
|
||||
Reference in New Issue
Block a user