firebreak/web/app/main.py
ZTatman 8e56a5bb1c feat(web): setup wizard, settings page, and Myst → Firebreak rename
Replace manual .env configuration with a browser-based setup wizard.
New users deploy with `docker compose up` and configure Forgejo OAuth
credentials through /setup on first visit. Existing users can
reconfigure via the same page (auth-gated post-setup).

Key changes:

Rename (Myst → Firebreak):
- Config fields, env vars, session cookie, templates, docker-compose,
  README all renamed from Myst to Firebreak

Setup wizard (/setup):
- New AppSettings single-row DB table stores Forgejo config
- Auto-generates Fernet encryption key on first setup
- SetupRequiredMiddleware redirects all routes to /setup pre-config
- Reconfigure mode pre-fills existing values, requires auth
- Landing page links to /setup for recovery if OAuth breaks

Settings page (/settings):
- PAT registration moved out of dashboard into dedicated page
- Token card with registration date, delete, and update actions
- Dashboard shows CTA linking to settings when no PAT registered

Auth updates:
- Routes use AppSettings from DB instead of env-based Settings
- OAuth callback redirects new users to /settings for PAT setup
- Forgejo API errors no longer leak upstream response bodies
- Decrypt failures return actionable 503 instead of unhandled 500

Code quality:
- Extracted templating.py to eliminate circular import hacks
- AppSettings.get_fernet() caches Fernet cipher instance
- User.has_pat property replaces scattered None checks
- lookup_session promoted to public API
- Dead code removed, return type annotations added
- PAT input fields use type="password"
- CSRF gap documented in agents.md for future addressing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-03 11:15:44 -04:00

85 lines
2.7 KiB
Python

from contextlib import asynccontextmanager
from pathlib import Path
from typing import Annotated
from fastapi import Depends, FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app import models as _models # noqa: F401 - register ORM tables on metadata
from app.auth.session import get_current_session, get_optional_session
from app.config import get_settings
from app.database import Base
from app.middleware import SetupRequiredMiddleware
from app.models.app_settings import AppSettings
from app.models.session import Session as UserBrowserSession
from app.routers import auth, links, settings, setup
from app.templating import templates
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage database engine lifecycle and load app settings."""
boot_settings = get_settings()
engine = create_async_engine(
url=boot_settings.database_url,
pool_pre_ping=True,
echo=boot_settings.app_name != "Firebreak",
)
app.state.db_session = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False,
)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with app.state.db_session() as db:
result = await db.execute(select(AppSettings).where(AppSettings.id == 1))
row = result.scalar_one_or_none()
if row:
app.state.setup_complete = True
app.state.app_settings = row
else:
app.state.setup_complete = False
app.state.app_settings = None
yield
await engine.dispose()
app = FastAPI(lifespan=lifespan)
app.add_middleware(SetupRequiredMiddleware)
app.mount("/static", StaticFiles(directory=Path(__file__).parent / "static"), name="static")
app.include_router(auth.router)
app.include_router(links.router)
app.include_router(settings.router)
app.include_router(setup.router)
@app.get("/", response_class=HTMLResponse)
async def root(
request: Request, sess: Annotated[UserBrowserSession | None, Depends(get_optional_session)]
) -> HTMLResponse:
return templates.TemplateResponse(
request,
"index.html",
{"user": sess.user if sess else None},
)
@app.get("/dashboard", response_class=HTMLResponse)
async def dashboard(
request: Request, sess: Annotated[UserBrowserSession, Depends(get_current_session)]
) -> HTMLResponse:
return templates.TemplateResponse(
request,
"dashboard.html",
{
"user": sess.user,
"pat_registered": sess.user.has_pat,
},
)