Files
Nat20-Notes/backend/app/config.py
KansaiGaijin 5081ba00f6
Some checks failed
Build and Push / build (push) Failing after 17s
add auth system and split backend into two-image CI/CD build
Backend:
- password hashing via hashlib.scrypt
- stateless HMAC-SHA256 tokens (7-day expiry)
- POST /api/auth/login, /api/auth/register (admin), /api/auth/reset-password
- admin user created from NAT20_ADMIN_USERNAME/PASSWORD on first startup
- users table, campaign_shares table, created_by on campaigns
- require_user dependency on all routes except auth
- campaign sharing: GET/POST/DELETE /api/campaigns/{id}/shares

Frontend:
- AuthContext: user/token state, login/logout, global fetch Auth header
- Login page, Users page (admin user management)
- route protection, sidebar user info/sign out

Docker/CI:
- split backend/Dockerfile into thin app-only image
- backend/Dockerfile.deps builds the heavy WhisperX/PyTorch base
- CI builds deps only when requirements.txt changes
- docker compose pull now fetches ~100KB app layer instead of 3.5GB
2026-07-30 12:05:56 +12:00

103 lines
3.7 KiB
Python

"""
App-wide configuration. Everything is either an environment variable (set once,
at deploy time) or a row in the `settings` table (editable at runtime via the
setup wizard / settings page) - never a hardcoded path or host.
"""
import os
from pathlib import Path
# Deploy-time config (env vars) - where things live on disk inside the container.
DATA_DIR = Path(os.environ.get("APP_DATA_DIR", "/data"))
UPLOAD_DIR = DATA_DIR / "uploads"
AUDIO_DIR = DATA_DIR / "audio"
TRANSCRIPT_DIR = DATA_DIR / "transcriptions"
NOTES_DIR = DATA_DIR / "notes"
DB_PATH = DATA_DIR / "app.db"
CAMPAIGNS_DIR = DATA_DIR / "campaigns"
def campaign_dir(campaign_id: str) -> Path:
return CAMPAIGNS_DIR / campaign_id
def campaign_audio_dir(campaign_id: str) -> Path:
return campaign_dir(campaign_id) / "audio"
def campaign_transcript_dir(campaign_id: str) -> Path:
return campaign_dir(campaign_id) / "transcriptions"
def campaign_notes_dir(campaign_id: str) -> Path:
return campaign_dir(campaign_id) / "notes"
# Auth config
JWT_SECRET = os.environ.get("NAT20_JWT_SECRET", os.urandom(32).hex())
ADMIN_USERNAME = os.environ.get("NAT20_ADMIN_USERNAME", "admin")
ADMIN_PASSWORD = os.environ.get("NAT20_ADMIN_PASSWORD", "admin")
for d in (UPLOAD_DIR, AUDIO_DIR, TRANSCRIPT_DIR, NOTES_DIR):
d.mkdir(parents=True, exist_ok=True)
# Runtime-editable global settings (stored in the `settings` table).
# Only onboarding state lives here; everything else is per-campaign.
GLOBAL_SETTINGS = {
"onboarding_completed": "false",
}
# Per-campaign settings — seeded into campaign_settings on creation.
# Every layer (frontend, router, pipeline) reads these from the campaign.
CAMPAIGN_SETTINGS = {
# Transcription
"whisper_model": "medium", # tiny|base|small|medium|large-v3 - user picks based on their hardware
"whisper_compute_type": "int8",
"hf_token": "", # required for diarization (pyannote gated models)
# Summarization backend: "ollama" (local) or "api" (hosted, OpenAI-compatible)
"llm_mode": "ollama",
"ollama_host": "http://localhost:11434",
"ollama_model": "qwen2.5:7b",
"api_base_url": "https://api.openai.com/v1",
"api_key": "",
"api_model": "gpt-4o-mini",
# Chunking for long transcripts (map-reduce summarization)
"chunk_word_target": "2500",
# Optional world context injected into every summarization prompt
"world_context": "",
"world_context_path": "",
# Player recap style: "story" | "diary" | "bullets" | "custom"
"player_recap_style": "story",
"player_recap_custom_prompt": "",
}
# Env var names that can override CAMPAIGN_SETTINGS at read time.
# Set these in docker-compose.yml to prefill the setup wizard.
CAMPAIGN_ENV_OVERRIDES = {
"hf_token": "NAT20_HF_TOKEN",
"whisper_model": "NAT20_WHISPER_MODEL",
"whisper_compute_type": "NAT20_WHISPER_COMPUTE_TYPE",
"ollama_host": "NAT20_OLLAMA_HOST",
"ollama_model": "NAT20_OLLAMA_MODEL",
"api_base_url": "NAT20_API_BASE_URL",
"api_key": "NAT20_API_KEY",
"api_model": "NAT20_API_MODEL",
"chunk_word_target": "NAT20_CHUNK_WORD_TARGET",
"world_context": "NAT20_WORLD_CONTEXT",
"world_context_path": "NAT20_WORLD_CONTEXT_PATH",
"player_recap_style": "NAT20_PLAYER_RECAP_STYLE",
"player_recap_custom_prompt": "NAT20_PLAYER_RECAP_CUSTOM_PROMPT",
}
def merge_campaign_settings_with_env(raw: dict) -> dict:
"""Priority: DB value > env var > CAMPAIGN_SETTINGS default."""
merged = {**CAMPAIGN_SETTINGS}
for key, env_name in CAMPAIGN_ENV_OVERRIDES.items():
val = os.environ.get(env_name)
if val is not None:
merged[key] = val
for key, val in raw.items():
if val and val.strip():
merged[key] = val.strip()
return merged