- Create app factory with routers and templates - Auth, assets, activities, anchors, storage, users, renderers routers - Federation router for WebFinger and nodeinfo - Jinja2 templates for L2 pages - Config and dependency injection 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
"""
|
|
Art-DAG L2 Server Application Factory.
|
|
|
|
Creates and configures the FastAPI application with all routers and middleware.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import JSONResponse, HTMLResponse
|
|
|
|
from artdag_common import create_jinja_env
|
|
|
|
from .config import settings
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Manage database connection pool lifecycle."""
|
|
import db
|
|
await db.init_pool()
|
|
yield
|
|
await db.close_pool()
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
"""
|
|
Create and configure the L2 FastAPI application.
|
|
|
|
Returns:
|
|
Configured FastAPI instance
|
|
"""
|
|
app = FastAPI(
|
|
title="Art-DAG L2 Server",
|
|
description="ActivityPub server for Art-DAG ownership and federation",
|
|
version="1.0.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# Initialize Jinja2 templates
|
|
template_dir = Path(__file__).parent / "templates"
|
|
app.state.templates = create_jinja_env(template_dir)
|
|
|
|
# Custom 404 handler
|
|
@app.exception_handler(404)
|
|
async def not_found_handler(request: Request, exc):
|
|
from artdag_common.middleware import wants_html
|
|
if wants_html(request):
|
|
from artdag_common import render
|
|
return render(app.state.templates, "404.html", request,
|
|
user=None,
|
|
)
|
|
return JSONResponse({"detail": "Not found"}, status_code=404)
|
|
|
|
# Include routers
|
|
from .routers import auth, assets, activities, anchors, storage, users, renderers
|
|
|
|
# Root routes
|
|
app.include_router(auth.router, prefix="/auth", tags=["auth"])
|
|
app.include_router(users.router, tags=["users"])
|
|
|
|
# Feature routers
|
|
app.include_router(assets.router, prefix="/assets", tags=["assets"])
|
|
app.include_router(activities.router, prefix="/activities", tags=["activities"])
|
|
app.include_router(anchors.router, prefix="/anchors", tags=["anchors"])
|
|
app.include_router(storage.router, prefix="/storage", tags=["storage"])
|
|
app.include_router(renderers.router, prefix="/renderers", tags=["renderers"])
|
|
|
|
# WebFinger and ActivityPub discovery
|
|
from .routers import federation
|
|
app.include_router(federation.router, tags=["federation"])
|
|
|
|
return app
|
|
|
|
|
|
# Create the default app instance
|
|
app = create_app()
|