Initial commit: Crumbforest Architecture Refinement v1 (Clean)
This commit is contained in:
175
app/routers/home.py
Normal file
175
app/routers/home.py
Normal file
@@ -0,0 +1,175 @@
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
import json
|
||||
import os
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Load deployment config
|
||||
deployment_config = None
|
||||
_translations_cache = {}
|
||||
|
||||
def load_config():
|
||||
"""Load configuration files on startup."""
|
||||
global deployment_config
|
||||
|
||||
config_path = os.path.join('deployment_config.json')
|
||||
|
||||
with open(config_path) as f:
|
||||
deployment_config = json.load(f)
|
||||
|
||||
def load_characters(lang: str = "de"):
|
||||
"""Load character data for given language."""
|
||||
if lang not in ["de", "en", "fr"]:
|
||||
lang = "de"
|
||||
|
||||
characters_path = os.path.join('static', 'data', f'characters.{lang}.json')
|
||||
try:
|
||||
with open(characters_path) as f:
|
||||
return json.load(f)
|
||||
except FileNotFoundError:
|
||||
# Fallback to German
|
||||
with open(os.path.join('static', 'data', 'characters.de.json')) as f:
|
||||
return json.load(f)
|
||||
|
||||
def load_translations(lang: str = "de"):
|
||||
"""Load translation JSON for given language with caching."""
|
||||
global _translations_cache
|
||||
|
||||
# Default to German if language not supported
|
||||
if lang not in ["de", "en", "fr"]:
|
||||
lang = "de"
|
||||
|
||||
# Check cache first
|
||||
if lang not in _translations_cache:
|
||||
translation_path = os.path.join('i18n', f'{lang}.json')
|
||||
try:
|
||||
with open(translation_path) as f:
|
||||
_translations_cache[lang] = json.load(f)
|
||||
except FileNotFoundError:
|
||||
# Fallback to German if translation file not found
|
||||
with open(os.path.join('i18n', 'de.json')) as f:
|
||||
_translations_cache[lang] = json.load(f)
|
||||
|
||||
return _translations_cache[lang]
|
||||
|
||||
# Load on module import
|
||||
load_config()
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
async def home_index(req: Request, lang: str = None):
|
||||
"""
|
||||
Public home page - no auth required.
|
||||
"""
|
||||
# Get language from query param or session
|
||||
if lang is None:
|
||||
lang = req.session.get("lang", "de")
|
||||
|
||||
req.session["lang"] = lang
|
||||
translations = load_translations(lang)
|
||||
|
||||
return req.app.state.render(
|
||||
req,
|
||||
"home/index.html",
|
||||
deployment=deployment_config,
|
||||
t=translations,
|
||||
lang=lang
|
||||
)
|
||||
|
||||
@router.get("/about", response_class=HTMLResponse)
|
||||
async def home_about(req: Request):
|
||||
"""
|
||||
About / Mission page.
|
||||
"""
|
||||
lang = req.session.get("lang", "de")
|
||||
translations = load_translations(lang)
|
||||
|
||||
return req.app.state.render(
|
||||
req,
|
||||
"home/about.html",
|
||||
deployment=deployment_config,
|
||||
t=translations,
|
||||
lang=lang
|
||||
)
|
||||
|
||||
@router.get("/crew", response_class=HTMLResponse)
|
||||
async def home_crew(req: Request):
|
||||
"""
|
||||
Crew / Characters page.
|
||||
"""
|
||||
lang = req.session.get("lang", "de")
|
||||
translations = load_translations(lang)
|
||||
characters = load_characters(lang)
|
||||
|
||||
return req.app.state.render(
|
||||
req,
|
||||
"home/crew.html",
|
||||
deployment=deployment_config,
|
||||
characters=characters,
|
||||
t=translations,
|
||||
lang=lang
|
||||
)
|
||||
|
||||
@router.get("/hardware", response_class=HTMLResponse)
|
||||
async def home_hardware(req: Request):
|
||||
"""
|
||||
Hardware information page.
|
||||
"""
|
||||
lang = req.session.get("lang", "de")
|
||||
translations = load_translations(lang)
|
||||
|
||||
return req.app.state.render(
|
||||
req,
|
||||
"home/hardware.html",
|
||||
deployment=deployment_config,
|
||||
t=translations,
|
||||
lang=lang
|
||||
)
|
||||
|
||||
@router.get("/software", response_class=HTMLResponse)
|
||||
async def home_software(req: Request):
|
||||
"""
|
||||
Software information page.
|
||||
"""
|
||||
lang = req.session.get("lang", "de")
|
||||
translations = load_translations(lang)
|
||||
|
||||
return req.app.state.render(
|
||||
req,
|
||||
"home/software.html",
|
||||
deployment=deployment_config,
|
||||
t=translations,
|
||||
lang=lang
|
||||
)
|
||||
|
||||
@router.get("/impressum", response_class=HTMLResponse)
|
||||
async def home_impressum(req: Request):
|
||||
"""
|
||||
Impressum / Legal notice page.
|
||||
"""
|
||||
lang = req.session.get("lang", "de")
|
||||
translations = load_translations(lang)
|
||||
|
||||
return req.app.state.render(
|
||||
req,
|
||||
"home/impressum.html",
|
||||
deployment=deployment_config,
|
||||
t=translations,
|
||||
lang=lang
|
||||
)
|
||||
|
||||
@router.get("/datenschutz", response_class=HTMLResponse)
|
||||
async def home_datenschutz(req: Request):
|
||||
"""
|
||||
Privacy policy / Datenschutz page.
|
||||
"""
|
||||
lang = req.session.get("lang", "de")
|
||||
translations = load_translations(lang)
|
||||
|
||||
return req.app.state.render(
|
||||
req,
|
||||
"home/datenschutz.html",
|
||||
deployment=deployment_config,
|
||||
t=translations,
|
||||
lang=lang
|
||||
)
|
||||
Reference in New Issue
Block a user