Path: blob/main/singlestoredb/apps/_config.py
469 views
import os1from dataclasses import dataclass2from typing import Optional345@dataclass6class AppConfig:7listen_port: int8base_url: str9base_path: str10notebook_server_id: str11app_token: Optional[str]12user_token: Optional[str]13running_interactively: bool14is_gateway_enabled: bool15is_local_dev: bool1617@staticmethod18def _read_variable(name: str) -> str:19value = os.environ.get(name)20if value is None:21raise RuntimeError(22f'Missing {name} environment variable. '23'Is the code running outside SingleStoreDB notebook environment?',24)25return value2627@classmethod28def from_env(cls) -> 'AppConfig':29port = cls._read_variable('SINGLESTOREDB_APP_LISTEN_PORT')30base_url = cls._read_variable('SINGLESTOREDB_APP_BASE_URL')31base_path = cls._read_variable('SINGLESTOREDB_APP_BASE_PATH')32notebook_server_id = cls._read_variable('SINGLESTOREDB_NOTEBOOK_SERVER_ID')33is_local_dev_env_var = cls._read_variable('SINGLESTOREDB_IS_LOCAL_DEV')3435workload_type = os.environ.get('SINGLESTOREDB_WORKLOAD_TYPE')36running_interactively = workload_type == 'InteractiveNotebook'3738is_gateway_enabled = 'SINGLESTOREDB_NOVA_GATEWAY_ENDPOINT' in os.environ3940app_token = os.environ.get('SINGLESTOREDB_APP_TOKEN')41user_token = os.environ.get('SINGLESTOREDB_USER_TOKEN')4243# Make sure the required variables are present44# and present useful error message if not45if running_interactively:46if is_gateway_enabled:47app_token = cls._read_variable('SINGLESTOREDB_APP_TOKEN')48else:49user_token = cls._read_variable('SINGLESTOREDB_USER_TOKEN')5051return cls(52listen_port=int(port),53base_url=base_url,54base_path=base_path,55notebook_server_id=notebook_server_id,56app_token=app_token,57user_token=user_token,58running_interactively=running_interactively,59is_gateway_enabled=is_gateway_enabled,60is_local_dev=is_local_dev_env_var == 'true',61)6263@property64def token(self) -> Optional[str]:65"""66Returns None if running non-interactively67"""68if self.is_gateway_enabled:69return self.app_token70else:71return self.user_token727374