0Mart00
Verbumix rebrand: emojis removed, multi-jurisdictional legal docs, cookie consent, footer links
9d4e305 Download src/wordgame/app_factory.py from humut/WordGame: direct link, hf CLI and curl.
- Browser
- Download file 8.3 kB
-
https://huggingface.co/spaces/humut/WordGame/resolve/main/src/wordgame/app_factory.py
- Command line
-
hf download hf://spaces/humut/WordGame/src/wordgame/app_factory.py
-
curl -L -o app_factory.py https://huggingface.co/spaces/humut/WordGame/resolve/main/src/wordgame/app_factory.py
8.3 kB
| """Közös alkalmazás-gyár a public és private appokhoz. | |
| A `settings.APP_MODE` értéke (`"public"` vagy `"private"`) határozza meg, | |
| hogy mely route-ok regisztrálódnak: | |
| - `public` – WordGame (HF Space): játék, login/reg, ranglétra, premium | |
| - `private` – Lingo (HF Space): dashboard, szobák, deck, invite, admin, paddle | |
| Mindkét app ugyanazt a `core/` modult használja (config, database, auth), | |
| csak a route-ok mások. | |
| """ | |
| import os | |
| from fastapi import FastAPI, Request | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from starlette.templating import Jinja2Templates | |
| from starlette.responses import HTMLResponse, RedirectResponse | |
| from slowapi import _rate_limit_exceeded_handler | |
| from slowapi.errors import RateLimitExceeded | |
| from slowapi.middleware import SlowAPIMiddleware | |
| from .core.config import settings | |
| from .core.rate_limit import limiter, rate_limit_exceeded_handler as custom_rate_handler | |
| from .core.i18n import detect_language_from_accept_lang, tr as translate_func, get_translation | |
| from .public.routes import game_routes as public_game | |
| from .public.routes import auth_routes as public_auth | |
| from .private.routes import ( | |
| dashboard_routes as private_dashboard, | |
| room_routes as private_room, | |
| admin_routes as private_admin, | |
| paddle_routes as private_paddle, | |
| slang_routes as private_slang, | |
| sso_callback as private_sso, | |
| ) | |
| from .shared.routes import health_routes | |
| def create_app() -> FastAPI: | |
| """Létrehozza a FastAPI alkalmazást a settings.APP_MODE alapján. | |
| Public mód: a játék + login/register route-ok regisztrálódnak. | |
| Private mód: a dashboard + szobák + admin + paddle + SSO callback. | |
| A health check (shared) mindkét módban elérhető. | |
| """ | |
| app = FastAPI( | |
| title=settings.APP_NAME, | |
| debug=settings.DEBUG, | |
| version="2.0.0", | |
| ) | |
| # ------------------------------------------------------------------ | |
| # Rate limiting (SlowAPI) | |
| # ------------------------------------------------------------------ | |
| app.state.limiter = limiter | |
| app.add_exception_handler(RateLimitExceeded, custom_rate_handler) | |
| app.add_middleware(SlowAPIMiddleware) | |
| # ------------------------------------------------------------------ | |
| # CORS – a másik Space + a saját domain + localhost (dev) | |
| # ------------------------------------------------------------------ | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.ALLOWED_ORIGINS, | |
| allow_credentials=True, | |
| allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"], | |
| allow_headers=["*"], | |
| ) | |
| # ------------------------------------------------------------------ | |
| # Static files (mindkét app kiszolgálja) | |
| # ------------------------------------------------------------------ | |
| current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| # src/static/ – CSS, JS, theme (valodi fajlok: src/static/css/, src/static/js/) | |
| real_static_dir = os.path.join(os.path.dirname(current_dir), "static") | |
| if os.path.isdir(real_static_dir): | |
| app.mount("/static", StaticFiles(directory=real_static_dir), name="static") | |
| # Gyökér static/ – szólisták (1000-most-common), CSAK ha nem azonos a real_static_dir-rel | |
| root_static_dir = os.path.join(os.path.dirname(os.path.dirname(current_dir)), "static") | |
| if os.path.isdir(root_static_dir) and root_static_dir != real_static_dir: | |
| app.mount("/static-data", StaticFiles(directory=root_static_dir), name="static-data") | |
| # ------------------------------------------------------------------ | |
| # Templates – közös mappa (mindkét app a `templates/` mappát használja) | |
| # ------------------------------------------------------------------ | |
| templates_dir = os.path.join(current_dir, "templates") | |
| os.makedirs(templates_dir, exist_ok=True) | |
| templates = Jinja2Templates(directory=templates_dir) | |
| # Google Analytics ID – minden template eléri a {{ google_analytics_id }} változót | |
| templates.env.globals["google_analytics_id"] = settings.GOOGLE_ANALYTICS_ID or "" | |
| # Cross-domain URL-ek – a private Space navigációs linkjeihez | |
| templates.env.globals["public_base_url"] = settings.PUBLIC_BASE_URL or "" | |
| templates.env.globals["private_base_url"] = settings.PRIVATE_BASE_URL or "" | |
| # i18n – translate function available in all templates as _(key, **kwargs) | |
| templates.env.globals["_"] = translate_func | |
| templates.env.globals["lang"] = "en" # default, overridden per-request | |
| # ------------------------------------------------------------------ | |
| # Shared (health, ready) | |
| # ------------------------------------------------------------------ | |
| app.include_router(health_routes.create_router()) | |
| # ------------------------------------------------------------------ | |
| # Language detection middleware – sets lang in request.state | |
| # ------------------------------------------------------------------ | |
| async def detect_language_middleware(request: Request, call_next): | |
| accept_lang = request.headers.get("Accept-Language", "") | |
| detected_lang = detect_language_from_accept_lang(accept_lang) | |
| request.state.lang = detected_lang | |
| # Update the global template variable for this request context | |
| templates.env.globals["lang"] = detected_lang | |
| templates.env.globals["_"] = lambda key, **kwargs: translate_func(detected_lang, key, **kwargs) | |
| response = await call_next(request) | |
| return response | |
| # ------------------------------------------------------------------ | |
| # Mode-specific routes | |
| # ------------------------------------------------------------------ | |
| if settings.is_public: | |
| # Public (WordGame) – játék, login, register | |
| app.include_router( | |
| public_game.create_router(templates), | |
| tags=["game"] | |
| ) | |
| app.include_router( | |
| public_auth.create_router(templates), | |
| prefix="/auth", | |
| tags=["auth"] | |
| ) | |
| # A /jatek-valaszto és a /dashboard route-ok a `public/routes/game_routes.py`-ban | |
| # vannak definiálva (user-kontextussal együtt). | |
| # A /dashboard a public oldalon átirányít a Lingo Space /auth/sso callback-re, | |
| # ahol a user átveszi a session-t. | |
| else: | |
| # Private (Lingo) – dashboard, szobák, deck, invite, admin, paddle, SSO | |
| app.include_router( | |
| private_dashboard.create_router(templates), | |
| prefix="/dashboard", | |
| tags=["dashboard"] | |
| ) | |
| app.include_router( | |
| private_room.create_router(), | |
| prefix="/szoba", | |
| tags=["rooms"] | |
| ) | |
| app.include_router( | |
| private_slang.create_router(), | |
| prefix="/szoba/import", | |
| tags=["slang"] | |
| ) | |
| app.include_router( | |
| private_paddle.create_router(), | |
| tags=["payments"] | |
| ) | |
| app.include_router( | |
| private_admin.create_router(templates), | |
| tags=["admin"] | |
| ) | |
| app.include_router( | |
| private_sso.create_router(), | |
| prefix="/auth", | |
| tags=["auth"] | |
| ) | |
| # ------------------------------------------------------------------ | |
| # 404 handler | |
| # ------------------------------------------------------------------ | |
| async def custom_404_handler(request: Request, exc): | |
| return templates.TemplateResponse( | |
| request=request, | |
| name="404.html", | |
| context={"request": request, "path": request.url.path}, | |
| status_code=404, | |
| ) | |
| # ------------------------------------------------------------------ | |
| # Startup | |
| # ------------------------------------------------------------------ | |
| async def startup_event(): | |
| mode_label = "PUBLIC (Verbumix)" if settings.is_public else "PRIVATE (Lingo)" | |
| print(f"{settings.APP_NAME} - {mode_label} mod elindult!") | |
| print(f"Szerver: http://{settings.HOST}:{settings.PORT}") | |
| print(f"Public base: {settings.PUBLIC_BASE_URL}") | |
| print(f"Private base: {settings.PRIVATE_BASE_URL}") | |
| return app | |