40 lines
969 B
Python
40 lines
969 B
Python
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'),
|
|
}
|