61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
from typing import TypedDict
|
|
from pathlib import Path
|
|
import os
|
|
|
|
from quart_common.web.env import env_bool, env_int
|
|
from server.Server import Server
|
|
|
|
class RunConfig(TypedDict):
|
|
host: str
|
|
port: int
|
|
debug: bool
|
|
|
|
def build_server_from_env(default_snake_type:str) -> Server:
|
|
data_path = str(Path(__file__).resolve().parent.parent)
|
|
redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
|
|
|
|
metrics_backend = os.environ.get('METRICS_BACKEND', None)
|
|
if metrics_backend is None:
|
|
metrics_backend = os.environ.get('BACKEND', 'memory')
|
|
|
|
metrics_redis_url = os.environ.get('METRICS_REDIS_URL', redis_url)
|
|
metrics_ttl_sec_raw = os.environ.get('METRICS_TTL_SEC', None)
|
|
metrics_ttl_sec = env_int('METRICS_TTL_SEC', 900) if metrics_ttl_sec_raw is not None else None
|
|
|
|
gameplay_db_enabled = env_bool('GAMEPLAY_DB_ENABLED', True)
|
|
gameplay_db_backend = os.environ.get('GAMEPLAY_DB_BACKEND', 'sqlite')
|
|
gameplay_db_path = os.environ.get(
|
|
'GAMEPLAY_DB_PATH',
|
|
os.path.join(data_path, 'data', 'database', 'gameplay.sqlite3'),
|
|
)
|
|
gameplay_db_busy_timeout_ms = env_int('GAMEPLAY_DB_BUSY_TIMEOUT_MS', 5000)
|
|
gameplay_db_pg_dsn = os.environ.get('GAMEPLAY_DB_PG_DSN', None)
|
|
|
|
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,
|
|
metrics_backend=metrics_backend,
|
|
metrics_redis_url=metrics_redis_url,
|
|
metrics_ttl_sec=metrics_ttl_sec,
|
|
gameplay_db_enabled=gameplay_db_enabled,
|
|
gameplay_db_backend=gameplay_db_backend,
|
|
gameplay_db_path=gameplay_db_path,
|
|
gameplay_db_busy_timeout_ms=gameplay_db_busy_timeout_ms,
|
|
gameplay_db_pg_dsn=gameplay_db_pg_dsn,
|
|
)
|
|
|
|
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': env_int('PORT', 8000),
|
|
'debug': env_bool('DEBUG'),
|
|
}
|