File size: 1,458 Bytes
b953c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from __future__ import annotations

import os
from typing import List


def env_flag(name: str, default: bool = False) -> bool:
    value = os.getenv(name)
    if value is None:
        return default
    return value.strip().lower() in {"1", "true", "yes", "on"}


def is_production() -> bool:
    return os.getenv("SAMAAN_ENV", os.getenv("APP_ENV", "development")).strip().lower() == "production"


def get_secret_key() -> str:
    secret = os.getenv("SECRET_KEY")
    if secret:
        return secret
    if is_production():
        raise RuntimeError("SECRET_KEY must be set in production")
    return "samaan-secret-key-for-development-only"


def get_database_url() -> str:
    return os.getenv("DATABASE_URL", os.getenv("SQLALCHEMY_DATABASE_URL", "sqlite:///./samaan.db"))


def get_cors_origins() -> List[str]:
    origins = os.getenv("ALLOWED_ORIGINS", "")
    values = [origin.strip() for origin in origins.split(",") if origin.strip()]
    if values:
        return values

    fallback = [origin.strip() for origin in os.getenv("FRONTEND_URL", "").split(",") if origin.strip()]
    if fallback:
        return fallback

    if is_production():
        return []
    return ["http://localhost:3000", "http://localhost:5173", "http://127.0.0.1:5173"]


def get_cors_origin_regex() -> str | None:
    if os.getenv("ALLOWED_ORIGINS", "").strip():
        return None
    if is_production():
        return r"https://.*\.vercel\.app$"
    return None