forked from Traqora/astroml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (84 loc) · 3.08 KB
/
Copy pathmain.py
File metadata and controls
100 lines (84 loc) · 3.08 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from typing import Optional
from pydantic import Field, validator
from pydantic_settings import BaseSettings
from functools import lru_cache
class Settings(BaseSettings):
"""
Application configuration using Pydantic BaseSettings.
Environment variables can override these defaults.
Configuration is loaded from .env file if present.
"""
# Application settings
app_name: str = Field(default="AstroML Dashboard API", env="APP_NAME")
app_version: str = Field(default="1.0.0", env="APP_VERSION")
debug: bool = Field(default=False, env="DEBUG")
# Server settings
host: str = Field(default="0.0.0.0", env="HOST")
port: int = Field(default=8000, env="PORT")
# Database settings
database_url: str = Field(
default="sqlite:///./astroml.db",
env="DATABASE_URL",
description="Database connection URL"
)
database_pool_size: int = Field(default=10, env="DATABASE_POOL_SIZE")
database_max_overflow: int = Field(default=20, env="DATABASE_MAX_OVERFLOW")
# API settings
api_key: Optional[str] = Field(default=None, env="API_KEY")
api_key_name: str = Field(default="X-API-Key", env="API_KEY_NAME")
# CORS settings
allowed_origins: list[str] = Field(
default=["http://localhost:5173", "http://localhost:3000"],
env="ALLOWED_ORIGINS"
)
cors_allow_credentials: bool = Field(default=True, env="CORS_ALLOW_CREDENTIALS")
cors_allow_methods: list[str] = Field(
default=["*"],
env="CORS_ALLOW_METHODS"
)
cors_allow_headers: list[str] = Field(
default=["*"],
env="CORS_ALLOW_HEADERS"
)
# Security settings
secret_key: str = Field(
default="your-secret-key-change-in-production",
env="SECRET_KEY"
)
algorithm: str = Field(default="HS256", env="ALGORITHM")
access_token_expire_minutes: int = Field(
default=30,
env="ACCESS_TOKEN_EXPIRE_MINUTES"
)
# Logging settings
log_level: str = Field(default="INFO", env="LOG_LEVEL")
log_format: str = Field(
default="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
env="LOG_FORMAT"
)
@validator("port")
def validate_port(cls, v: int) -> int:
"""Validate port number is in valid range."""
if not 1024 <= v <= 65535:
raise ValueError(f"Port {v} is not in valid range (1024-65535)")
return v
@validator("log_level")
def validate_log_level(cls, v: str) -> str:
"""Validate log level is valid."""
valid_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
if v.upper() not in valid_levels:
raise ValueError(f"Invalid log level: {v}. Must be one of {valid_levels}")
return v.upper()
class Config:
"""Pydantic configuration."""
env_file = ".env"
env_file_encoding = "utf-8"
case_sensitive = False
@lru_cache()
def get_settings() -> Settings:
"""
Get cached settings instance.
Returns:
Settings: Application settings instance
"""
return Settings()