reuse the code from quart_common.web.env to import env_bool and env_int into all classes that are useing this functions
This commit is contained in:
+5
-9
@@ -2,6 +2,7 @@ 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):
|
||||
@@ -9,18 +10,13 @@ class RunConfig(TypedDict):
|
||||
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)
|
||||
redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
|
||||
game_state_backend = os.environ.get('GAME_STATE_BACKEND', 'memory')
|
||||
game_state_redis_url = os.environ.get('GAME_STATE_REDIS_URL', redis_url)
|
||||
game_state_ttl_sec = int(os.environ.get('GAME_STATE_TTL_SEC', '900'))
|
||||
game_state_ttl_sec = env_int('GAME_STATE_TTL_SEC', 900)
|
||||
|
||||
metrics_backend = os.environ.get('METRICS_BACKEND', None)
|
||||
if metrics_backend is None:
|
||||
@@ -31,14 +27,14 @@ def build_server_from_env(default_snake_type:str) -> Server:
|
||||
if metrics_ttl_sec_raw is None:
|
||||
metrics_ttl_sec = (game_state_ttl_sec if metrics_backend.strip().lower() == 'redis' else None)
|
||||
else:
|
||||
metrics_ttl_sec = int(metrics_ttl_sec_raw)
|
||||
metrics_ttl_sec = env_int('METRICS_TTL_SEC', game_state_ttl_sec)
|
||||
|
||||
gameplay_db_enabled = env_bool('GAMEPLAY_DB_ENABLED', True)
|
||||
gameplay_db_path = os.environ.get(
|
||||
'GAMEPLAY_DB_PATH',
|
||||
os.path.join(data_path, 'data', 'database', 'gameplay.sqlite3'),
|
||||
)
|
||||
gameplay_db_busy_timeout_ms = int(os.environ.get('GAMEPLAY_DB_BUSY_TIMEOUT_MS', '5000'))
|
||||
gameplay_db_busy_timeout_ms = env_int('GAMEPLAY_DB_BUSY_TIMEOUT_MS', 5000)
|
||||
|
||||
server = Server(
|
||||
data_path=data_path,
|
||||
@@ -66,6 +62,6 @@ def build_server_from_env(default_snake_type:str) -> Server:
|
||||
def build_run_config() -> RunConfig:
|
||||
return {
|
||||
'host': os.environ.get('HOST', '0.0.0.0'),
|
||||
'port': int(os.environ.get('PORT', '8000')),
|
||||
'port': env_int('PORT', 8000),
|
||||
'debug': env_bool('DEBUG'),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user