142 lines
4.7 KiB
Django/Jinja
142 lines
4.7 KiB
Django/Jinja
{%- macro str_field(name, value) -%}
|
|
{%- set line = " " ~ name ~ ': str = "' ~ value ~ '"' -%}
|
|
{%- if line | length <= 88 or value | length + 10 > 88 %}{{ line }}{% else %} {{ name }}: str = (
|
|
"{{ value }}"
|
|
){% endif -%}
|
|
{%- endmacro -%}
|
|
{% if database != 'none' or use_redis %}import os
|
|
|
|
{% endif %}from pydantic import Field{% if database != 'none' or 'aiogram_bot' in backend_services or include_payments or use_llm or use_redis %}, SecretStr{% endif %}
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
{% if database != 'none' or use_redis %}
|
|
|
|
def is_prod() -> bool:
|
|
return os.getenv("RUN_ENVIRONMENT") == "prod"
|
|
{% endif %}
|
|
|
|
class Section(BaseSettings):
|
|
model_config = SettingsConfigDict(extra="ignore")
|
|
|
|
|
|
class LogSettings(Section):
|
|
level: str = "INFO"
|
|
level_external: str = "WARNING"
|
|
show_time: bool = False
|
|
show_locals: bool = False
|
|
console_width: int = 150
|
|
{% if database == 'mongo' %}
|
|
|
|
class DatabaseSettings(Section):
|
|
host: str = "mongodb"
|
|
port: int = 27017
|
|
user: str = "{{ project_slug }}"
|
|
password: SecretStr = SecretStr("{{ project_slug }}")
|
|
db_name: str = "{{ project_slug }}"
|
|
connection_params: str = "?authSource=admin"
|
|
{{ str_field("scripts_connection_url", "mongodb://" ~ project_slug ~ ":" ~ project_slug ~ "@localhost:27017/" ~ project_slug ~ "?authSource=admin") }}
|
|
|
|
@property
|
|
def connection_url(self) -> str:
|
|
if not is_prod():
|
|
return self.scripts_connection_url
|
|
password = self.password.get_secret_value()
|
|
return f"mongodb://{self.user}:{password}@{self.host}:{self.port}/{self.db_name}{self.connection_params}"
|
|
{% elif database == 'postgres' %}
|
|
|
|
class DatabaseSettings(Section):
|
|
host: str = "postgres"
|
|
port: int = 5432
|
|
user: str = "{{ project_slug }}"
|
|
password: SecretStr = SecretStr("{{ project_slug }}")
|
|
db_name: str = "{{ project_slug }}"
|
|
min_pool_size: int = 5
|
|
max_pool_size: int = 20
|
|
{{ str_field("scripts_connection_url", "postgresql://" ~ project_slug ~ ":" ~ project_slug ~ "@localhost:5432/" ~ project_slug) }}
|
|
|
|
@property
|
|
def connection_url(self) -> str:
|
|
if not is_prod():
|
|
return self.scripts_connection_url
|
|
password = self.password.get_secret_value()
|
|
return f"postgresql://{self.user}:{password}@{self.host}:{self.port}/{self.db_name}"
|
|
|
|
@property
|
|
def async_connection_url(self) -> str:
|
|
return self.connection_url.replace("postgresql://", "postgresql+asyncpg://", 1)
|
|
{% endif %}{% if use_redis %}
|
|
|
|
class RedisSettings(Section):
|
|
host: str = "redis"
|
|
port: int = 6379
|
|
db: int = 0
|
|
password: SecretStr | None = None
|
|
cache_ttl: int = 300
|
|
scripts_connection_url: str = "redis://localhost:6379/0"
|
|
|
|
@property
|
|
def url(self) -> str:
|
|
if not is_prod():
|
|
return self.scripts_connection_url
|
|
auth = f":{self.password.get_secret_value()}@" if self.password else ""
|
|
return f"redis://{auth}{self.host}:{self.port}/{self.db}"
|
|
{% endif %}{% if 'api' in backend_services %}
|
|
|
|
class ApiSettings(Section):
|
|
host: str = "0.0.0.0" # noqa: S104
|
|
port: int = 8080
|
|
workers: int = 1
|
|
docs: bool = False
|
|
{%- if serve_spa %}
|
|
static_dir: str = "static"
|
|
{%- endif %}
|
|
{% endif %}{% if 'aiogram_bot' in backend_services %}
|
|
|
|
class BotSettings(Section):
|
|
token: SecretStr = SecretStr("")
|
|
admins: list[int] = Field(default_factory=list)
|
|
{% endif %}{% if include_payments %}
|
|
|
|
class CryptoPaySettings(Section):
|
|
token: SecretStr = SecretStr("")
|
|
{% endif %}{% if use_llm %}
|
|
|
|
class LlmSettings(Section):
|
|
model: str = "google-gla:gemini-2.5-flash"
|
|
gemini_api_key: SecretStr = SecretStr("")
|
|
{% endif %}
|
|
|
|
class Settings(BaseSettings):
|
|
log: LogSettings = Field(default_factory=LogSettings)
|
|
{%- if database != 'none' %}
|
|
db: DatabaseSettings = Field(default_factory=DatabaseSettings)
|
|
{%- endif %}
|
|
{%- if use_redis %}
|
|
redis: RedisSettings = Field(default_factory=RedisSettings)
|
|
{%- endif %}
|
|
{%- if 'api' in backend_services %}
|
|
api: ApiSettings = Field(default_factory=ApiSettings)
|
|
{%- endif %}
|
|
{%- if 'aiogram_bot' in backend_services %}
|
|
bot: BotSettings = Field(default_factory=BotSettings)
|
|
{%- endif %}
|
|
{%- if include_payments %}
|
|
crypto_pay: CryptoPaySettings = Field(default_factory=CryptoPaySettings)
|
|
{%- endif %}
|
|
{%- if use_llm %}
|
|
llm: LlmSettings = Field(default_factory=LlmSettings)
|
|
{%- endif %}
|
|
|
|
model_config = SettingsConfigDict(
|
|
{%- if layout == 'part' %}
|
|
case_sensitive=False,
|
|
env_file=("../.env", ".env"),
|
|
env_nested_delimiter="__",
|
|
extra="ignore",
|
|
{%- else %}
|
|
case_sensitive=False, env_file=".env", env_nested_delimiter="__", extra="ignore"
|
|
{%- endif %}
|
|
)
|
|
|
|
|
|
env = Settings()
|