51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
from pathlib import Path
|
|
from typing import Any, Dict, Optional
|
|
|
|
from pydantic import BaseSettings, validator
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
# Base settings
|
|
PROJECT_NAME: str = "Web Scraper CLI"
|
|
PROJECT_DESCRIPTION: str = "A FastAPI-based web scraper with CLI interface"
|
|
VERSION: str = "0.1.0"
|
|
API_V1_STR: str = "/api/v1"
|
|
|
|
# Server settings
|
|
HOST: str = "0.0.0.0"
|
|
PORT: int = 8000
|
|
DEBUG: bool = True
|
|
|
|
# Database settings
|
|
DB_DIR: Path = Path("/app") / "storage" / "db"
|
|
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
|
|
|
@validator("SQLALCHEMY_DATABASE_URL", pre=True)
|
|
def validate_db_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
|
|
"""
|
|
Ensure the database directory exists.
|
|
"""
|
|
if isinstance(v, str) and v.startswith("sqlite"):
|
|
db_dir = values.get("DB_DIR")
|
|
if db_dir:
|
|
db_dir.mkdir(parents=True, exist_ok=True)
|
|
return v
|
|
return v
|
|
|
|
# Scraper settings
|
|
DEFAULT_USER_AGENT: str = (
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
|
"(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
|
)
|
|
DEFAULT_TIMEOUT: int = 30 # seconds
|
|
|
|
# Ratelimit settings
|
|
DEFAULT_RATE_LIMIT: float = 1.0 # requests per second
|
|
|
|
class Config:
|
|
env_file = ".env"
|
|
case_sensitive = True
|
|
|
|
|
|
settings = Settings()
|