Spaces:
Sleeping
Sleeping
Hkoradiya123 commited on
Commit ·
a0d5656
1
Parent(s): 3ba45b3
feat: Refactor configuration settings to use environment variables for flexibility
Browse files- backend/app/config.py +46 -16
backend/app/config.py
CHANGED
|
@@ -2,36 +2,66 @@ import os
|
|
| 2 |
from pydantic_settings import BaseSettings
|
| 3 |
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
class Settings(BaseSettings):
|
| 6 |
# Database
|
| 7 |
-
DATABASE_URL: str = "sqlite:///./ssc.db"
|
| 8 |
|
| 9 |
# Security
|
| 10 |
-
SECRET_KEY: str = "dev-secret-key-change-in-production"
|
| 11 |
-
ALGORITHM: str = "HS256"
|
| 12 |
-
ACCESS_TOKEN_EXPIRE_MINUTES: int =
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Premium Settings
|
| 15 |
-
PREMIUM_COST: int = 1000 # in rupees
|
| 16 |
-
PREMIUM_DURATION_DAYS: int =
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Email Settings
|
| 19 |
-
SMTP_SERVER: str = "smtp.gmail.com"
|
| 20 |
-
SMTP_PORT: int = 587
|
| 21 |
-
SMTP_USER: str = ""
|
| 22 |
-
SMTP_PASSWORD: str = ""
|
| 23 |
|
| 24 |
# Admin
|
| 25 |
-
ADMIN_EMAIL: str = "admin@ssc.com"
|
| 26 |
-
ADMIN_PASSWORD: str = "admin123"
|
| 27 |
|
| 28 |
# API
|
| 29 |
-
API_TITLE: str = "SSC API"
|
| 30 |
-
API_VERSION: str = "1.0.0"
|
| 31 |
-
DEBUG: bool = False
|
| 32 |
|
| 33 |
# Deployment / CORS
|
| 34 |
-
CORS_ORIGINS: str =
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
@property
|
| 37 |
def cors_origins_list(self) -> list[str]:
|
|
|
|
| 2 |
from pydantic_settings import BaseSettings
|
| 3 |
|
| 4 |
|
| 5 |
+
def env_first(keys: list[str], default: str) -> str:
|
| 6 |
+
for key in keys:
|
| 7 |
+
value = os.getenv(key)
|
| 8 |
+
if value is not None and str(value).strip() != "":
|
| 9 |
+
return value
|
| 10 |
+
return default
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def env_first_int(keys: list[str], default: int) -> int:
|
| 14 |
+
value = env_first(keys, str(default))
|
| 15 |
+
try:
|
| 16 |
+
return int(value)
|
| 17 |
+
except ValueError:
|
| 18 |
+
return default
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def env_first_bool(keys: list[str], default: bool) -> bool:
|
| 22 |
+
value = env_first(keys, "true" if default else "false").strip().lower()
|
| 23 |
+
return value in {"1", "true", "yes", "on"}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
class Settings(BaseSettings):
|
| 27 |
# Database
|
| 28 |
+
DATABASE_URL: str = env_first(["DATABASE_URL", "DATABASEURL"], "sqlite:///./ssc.db")
|
| 29 |
|
| 30 |
# Security
|
| 31 |
+
SECRET_KEY: str = env_first(["SECRET_KEY", "SECRETKEY"], "dev-secret-key-change-in-production")
|
| 32 |
+
ALGORITHM: str = env_first(["ALGORITHM"], "HS256")
|
| 33 |
+
ACCESS_TOKEN_EXPIRE_MINUTES: int = env_first_int(
|
| 34 |
+
["ACCESS_TOKEN_EXPIRE_MINUTES", "ACCESSTOKENEXPIREMINUTES"],
|
| 35 |
+
30,
|
| 36 |
+
)
|
| 37 |
|
| 38 |
# Premium Settings
|
| 39 |
+
PREMIUM_COST: int = env_first_int(["PREMIUM_COST", "PREMIUMCOST"], 1000) # in rupees
|
| 40 |
+
PREMIUM_DURATION_DAYS: int = env_first_int(
|
| 41 |
+
["PREMIUM_DURATION_DAYS", "PREMIUMDURATIONDAYS"],
|
| 42 |
+
30,
|
| 43 |
+
)
|
| 44 |
|
| 45 |
# Email Settings
|
| 46 |
+
SMTP_SERVER: str = env_first(["SMTP_SERVER", "SMTPSERVER"], "smtp.gmail.com")
|
| 47 |
+
SMTP_PORT: int = env_first_int(["SMTP_PORT", "SMTPPORT"], 587)
|
| 48 |
+
SMTP_USER: str = env_first(["SMTP_USER", "SMTPUSER"], "")
|
| 49 |
+
SMTP_PASSWORD: str = env_first(["SMTP_PASSWORD", "SMTPPASSWORD"], "")
|
| 50 |
|
| 51 |
# Admin
|
| 52 |
+
ADMIN_EMAIL: str = env_first(["ADMIN_EMAIL", "ADMINEMAIL"], "admin@ssc.com")
|
| 53 |
+
ADMIN_PASSWORD: str = env_first(["ADMIN_PASSWORD", "ADMINPASSWORD"], "admin123")
|
| 54 |
|
| 55 |
# API
|
| 56 |
+
API_TITLE: str = env_first(["API_TITLE", "APITITLE"], "SSC API")
|
| 57 |
+
API_VERSION: str = env_first(["API_VERSION", "APIVERSION"], "1.0.0")
|
| 58 |
+
DEBUG: bool = env_first_bool(["DEBUG"], False)
|
| 59 |
|
| 60 |
# Deployment / CORS
|
| 61 |
+
CORS_ORIGINS: str = env_first(
|
| 62 |
+
["CORS_ORIGINS", "CORSORIGINS"],
|
| 63 |
+
"http://localhost:3000,http://127.0.0.1:3000",
|
| 64 |
+
)
|
| 65 |
|
| 66 |
@property
|
| 67 |
def cors_origins_list(self) -> list[str]:
|