Fix Issue 9: Enforce configuration precedence rules

- Established explicit precedence: ENV vars > config.json > defaults
- Updated fastapi_app.py to only sync config.json when ENV var not set
- Added precedence logging to show which source is used
- Documented precedence rules with examples in CONFIGURATION.md

Key principle: ENV variables always take precedence, config.json is
fallback only. This ensures deployment flexibility and clear priority.

All config tests passing (26/26)
This commit is contained in:
2026-01-24 21:23:48 +01:00
parent ed3882991f
commit c4080e4e57
3 changed files with 71 additions and 28 deletions

View File

@@ -10,24 +10,42 @@ This document provides a comprehensive reference for all configuration options i
### Configuration Sources
Aniworld uses a layered configuration system:
Aniworld uses a layered configuration system with **explicit precedence rules**:
1. **Environment Variables** (highest priority)
2. **`.env` file** in project root
3. **`data/config.json`** file
4. **Default values** (lowest priority)
1. **Environment Variables** (highest priority) - Takes precedence over all other sources
2. **`.env` file** in project root - Loaded as environment variables
3. **`data/config.json`** file - Persistent file-based configuration
4. **Default values** (lowest priority) - Built-in fallback values
### Precedence Rules
**Critical Principle**: `ENV VARS > config.json > defaults`
- **Environment variables always win**: If a value is set via environment variable, it will NOT be overridden by config.json
- **config.json as fallback**: If an ENV var is not set (or is empty/default), the value from config.json is used
- **Defaults as last resort**: Built-in default values are used only if neither ENV var nor config.json provide a value
### Loading Mechanism
Configuration is loaded at application startup via Pydantic Settings.
Configuration is loaded at application startup in `src/server/fastapi_app.py`:
```python
# src/config/settings.py
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
1. **Pydantic Settings** loads ENV vars and .env file with defaults
2. **config.json** is loaded via `ConfigService`
3. **Selective sync**: config.json values sync to settings **only if** ENV var not set
4. **Runtime access**: Code uses `settings` object (which has final merged values)
**Example**:
```bash
# If ENV var is set:
ANIME_DIRECTORY=/env/path # This takes precedence
# config.json has:
{"other": {"anime_directory": "/config/path"}} # This is ignored
# Result: settings.anime_directory = "/env/path"
```
Source: [src/config/settings.py](../src/config/settings.py#L1-L96)
**Source**: [src/config/settings.py](../src/config/settings.py#L1-L96), [src/server/fastapi_app.py](../src/server/fastapi_app.py#L139-L185)
---