Feat/backend config - #1050
Conversation
pydantic.settings and made appropraite changes.
|
@shantanushok is attempting to deploy a commit to the Darshan's projects Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Pull request overview
This PR introduces a centralized backend configuration module (backend/utils/config.py) using pydantic-settings to replace scattered os.getenv usage, and wires the validated settings into core backend entrypoints (app startup, DB, RAG, uploads, and services).
Changes:
- Added a
Settings(BaseSettings) wrapper for key environment variables (paths, ports, limits, defaults). - Replaced direct environment lookups in app/service/route modules with
settings.*accessors. - Simplified settings timeout parsing by delegating type coercion to Pydantic.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| backend/utils/config.py | New centralized settings object and defaults (env-backed). |
| backend/app.py | Uses settings for frontend dist path and CORS/CSRF allowlist origins. |
| backend/utils/audit_log.py | Sources audit log directory from validated settings. |
| backend/services/rag_service.py | Sources ChromaDB directory from validated settings. |
| backend/services/ollama_service.py | Sources Ollama host/base URL from validated settings. |
| backend/services/db_service.py | Sources DB path/vacuum threshold from settings; updates seed default_model. |
| backend/routes/upload.py | Uses settings for upload directory and max upload size. |
| backend/routes/settings.py | Removes manual env parsing for settings timeout and uses settings. |
Suppressed comments (3)
backend/utils/config.py:4
- Unused import:
Listfromtypingis not referenced in this module.
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import Field
from typing import List
from pathlib import Path
backend/utils/config.py:10
SETTINGS_API_TIMEOUT_SECONDSpreviously allowed floats (the route code parsedfloat(os.getenv(...))). Withsettings_api_timeout_seconds: int, values like"0.5"will now fail validation and crash at import/startup. Consider making this afloatand aligning the default withDEFAULT_SETTINGS_API_TIMEOUT_SECONDS(2.0) inroutes/settings.py.
settings_api_timeout_seconds: int = Field(default=10, alias="SETTINGS_API_TIMEOUT_SECONDS")
backend/utils/config.py:8
- Issue #917 / PR description call out config validation for the security middleware allowlist, but
cors_originsis still an unvalidated comma-separated string. This means malformed origins (missing scheme/host) will pass through to CORS +OriginValidationMiddlewarewithout startup-time validation.
cors_origins: str = Field(default="http://localhost:3000,http://127.0.0.1:3000,http://localhost:5173,http://localhost:8000", alias="CORS_ORIGINS")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||
| from pydantic import Field |
| INSERT OR IGNORE INTO app_settings (key, value) VALUES | ||
| ('default_model', '"llama3"'), | ||
| ('default_model', '""" + json.dumps(settings.default_model) + """'), | ||
| ('default_language', '"en"'), |
…/local_mind into feat/backend-config
|
@imDarshanGK can you please merge this :] |
|
Hey @imDarshanGK can you review this please.. |
Description
Resolves #917
Added centralized configuration validation via
pydantic-settings. This ensures that all environment variables (e.g. paths, ports, and limits) are strictly typed and validated on application startup instead of relying on scatteredos.getenvcalls with missing validation.Summary of Changes
backend/utils/config.py:Settingsclass inheriting fromBaseSettings.frontend_dist,cors_origins,upload_dir, etc.) along with their default fallbacks.backend/app.py:settings.frontend_distandsettings.cors_origins.backend/utils/audit_log.py:AUDIT_LOG_DIRto usesettings.audit_log_dir.backend/services/rag_service.py:CHROMA_PATHwithsettings.chromadb_dir.backend/services/ollama_service.py:OLLAMA_BASE_URLretrieval to usesettings.ollama_host.backend/services/db_service.py:VACUUM_THRESHOLDandDB_PATHto use the validated settings."llama3"DB seed withsettings.default_model.backend/routes/upload.py:UPLOAD_DIRvia settings and tied the 50MB upload limit tosettings.max_file_size.backend/routes/settings.py:settings_api_timeout_secondssince Pydantic handles this validation natively.Verification
Ran the full backend test suite to verify no regressions in routes or services: