-
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import Field, field_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
BACKEND_DIR = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=BACKEND_DIR / ".env", env_file_encoding="utf-8", extra="ignore"
|
||||
)
|
||||
|
||||
APP_NAME: str = "Saura Shakti API"
|
||||
ENVIRONMENT: str = "development" # development | test
|
||||
API_PREFIX: str = "/api"
|
||||
|
||||
DATABASE_URL: str
|
||||
DB_ECHO: bool = False
|
||||
|
||||
JWT_SECRET_KEY: str = Field(min_length=32)
|
||||
JWT_ALGORITHM: str = "HS256"
|
||||
JWT_EXPIRE_MINUTES: int = 120
|
||||
|
||||
# Comma-separated list in .env
|
||||
CORS_ORIGINS: str = "http://localhost:5173"
|
||||
|
||||
# Media / uploads
|
||||
UPLOAD_DIR: Path = BACKEND_DIR / "uploads"
|
||||
MEDIA_URL_PATH: str = "/media"
|
||||
MEDIA_BASE_URL: str = "http://localhost:8020/media"
|
||||
MAX_UPLOAD_SIZE_MB: int = 10
|
||||
MAX_FILES_PER_UPLOAD: int = 20
|
||||
MAX_IMAGE_PIXELS: int = 60_000_000 # guards against decompression bombs
|
||||
|
||||
# Watermark
|
||||
WATERMARK_LOGO_PATH: Path = BACKEND_DIR / "assets" / "watermark" / "logo.png"
|
||||
WATERMARK_WIDTH_RATIO: float = 0.18 # logo width relative to image width
|
||||
WATERMARK_MIN_WIDTH: int = 80
|
||||
WATERMARK_MAX_WIDTH: int = 600
|
||||
WATERMARK_MARGIN_RATIO: float = 0.03 # margin relative to the shorter image side
|
||||
WATERMARK_OPACITY: float = 0.75
|
||||
WATERMARK_SHADOW_OPACITY: float = 0.35
|
||||
|
||||
THUMBNAIL_MAX_SIZE: int = 640
|
||||
|
||||
# Email (quotations). If SMTP_HOST is empty, emails are written to OUTBOX_DIR as .eml files.
|
||||
SMTP_HOST: str = ""
|
||||
SMTP_PORT: int = 587
|
||||
SMTP_USER: str = ""
|
||||
SMTP_PASSWORD: str = ""
|
||||
SMTP_STARTTLS: bool = True
|
||||
MAIL_FROM: str = "Saura Shakti <no-reply@localhost>"
|
||||
OUTBOX_DIR: Path = BACKEND_DIR / "outbox"
|
||||
|
||||
# Simple in-memory rate limits (per client IP, per process)
|
||||
LOGIN_RATE_LIMIT_PER_MINUTE: int = 10
|
||||
PUBLIC_FORM_RATE_LIMIT_PER_MINUTE: int = 10
|
||||
|
||||
@field_validator("WATERMARK_OPACITY", "WATERMARK_SHADOW_OPACITY")
|
||||
@classmethod
|
||||
def _opacity_range(cls, v: float) -> float:
|
||||
if not 0 <= v <= 1:
|
||||
raise ValueError("opacity must be between 0 and 1")
|
||||
return v
|
||||
|
||||
@property
|
||||
def cors_origins_list(self) -> list[str]:
|
||||
return [o.strip() for o in self.CORS_ORIGINS.split(",") if o.strip()]
|
||||
|
||||
@property
|
||||
def max_upload_bytes(self) -> int:
|
||||
return self.MAX_UPLOAD_SIZE_MB * 1024 * 1024
|
||||
|
||||
@property
|
||||
def is_test(self) -> bool:
|
||||
return self.ENVIRONMENT == "test"
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
return Settings() # type: ignore[call-arg]
|
||||
|
||||
|
||||
settings = get_settings()
|
||||
Reference in New Issue
Block a user