You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
import logging
|
|
import os
|
|
import base64
|
|
from urllib import parse
|
|
from typing import List
|
|
from pydantic import BaseModel
|
|
|
|
from starlette.config import Config
|
|
from starlette.datastructures import CommaSeparatedStrings
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class BaseConfigurationModel(BaseModel):
|
|
"""Base configuration model used by all config options."""
|
|
|
|
pass
|
|
|
|
|
|
def get_env_tags(tag_list: List[str]) -> dict:
|
|
"""Create dictionary of available env tags."""
|
|
tags = {}
|
|
for t in tag_list:
|
|
tag_key, env_key = t.split(":")
|
|
|
|
env_value = os.environ.get(env_key)
|
|
|
|
if env_value:
|
|
tags.update({tag_key: env_value})
|
|
|
|
return tags
|
|
|
|
|
|
def get_config():
|
|
try:
|
|
# Try to load from .env file first
|
|
config = Config(".env")
|
|
except FileNotFoundError:
|
|
# If .env doesn't exist, use environment variables
|
|
config = Config(environ=os.environ)
|
|
|
|
return config
|
|
|
|
|
|
config = get_config()
|
|
|
|
|
|
LOG_LEVEL = config("LOG_LEVEL", default=logging.WARNING)
|
|
ENV = config("ENV", default="local")
|
|
PORT = config("PORT", cast=int, default=8000)
|
|
HOST = config("HOST", default="localhost")
|
|
|
|
|
|
# database
|
|
DATABASE_HOSTNAME = config("DATABASE_HOSTNAME")
|
|
_DATABASE_CREDENTIAL_USER = config("DATABASE_CREDENTIAL_USER")
|
|
_DATABASE_CREDENTIAL_PASSWORD = config("DATABASE_CREDENTIAL_PASSWORD")
|
|
_QUOTED_DATABASE_PASSWORD = parse.quote(str(_DATABASE_CREDENTIAL_PASSWORD))
|
|
DATABASE_NAME = config("DATABASE_NAME", default="digital_twin")
|
|
DATABASE_PORT = config("DATABASE_PORT", default="5432")
|
|
|
|
DATABASE_ENGINE_POOL_SIZE = config("DATABASE_ENGINE_POOL_SIZE", cast=int, default=20)
|
|
DATABASE_ENGINE_MAX_OVERFLOW = config(
|
|
"DATABASE_ENGINE_MAX_OVERFLOW", cast=int, default=0
|
|
)
|
|
# Deal with DB disconnects
|
|
# https://docs.sqlalchemy.org/en/20/core/pooling.html#pool-disconnects
|
|
DATABASE_ENGINE_POOL_PING = config("DATABASE_ENGINE_POOL_PING", default=False)
|
|
SQLALCHEMY_DATABASE_URI = f"postgresql+asyncpg://{_DATABASE_CREDENTIAL_USER}:{_QUOTED_DATABASE_PASSWORD}@{DATABASE_HOSTNAME}:{DATABASE_PORT}/{DATABASE_NAME}"
|
|
|
|
TIMEZONE = "Asia/Jakarta"
|
|
|
|
|
|
AUTH_SERVICE_API = config("AUTH_SERVICE_API", default="http://192.168.1.82:8000/auth")
|
|
|
|
RELIABILITY_APP_URL = config(
|
|
"RELIABILITY_APP_URL", default="http://192.168.1.82:8000/reliability"
|
|
)
|