Files
packetserver/packetserver/http/server.py
2025-12-26 14:04:44 -05:00

65 lines
1.8 KiB
Python

# packetserver/http/server.py
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pathlib import Path
from .database import init_db
from .routers import public, profile, messages, send
from .logging import init_logging
init_logging()
BASE_DIR = Path(__file__).parent.resolve()
app = FastAPI(
title="PacketServer HTTP API",
description="RESTful interface to the AX.25 packet radio BBS",
version="0.1.0",
)
# Define templates EARLY (before importing dashboard)
templates = Jinja2Templates(directory=BASE_DIR / "templates")
from datetime import datetime, timezone
def timestamp_to_date(ts):
if ts is None:
return "Never"
try:
dt = datetime.fromtimestamp(float(ts), tz=timezone.utc)
return dt.strftime("%Y-%m-%d %H:%M:%S UTC")
except (ValueError, TypeError):
return "Invalid"
# Register the filter correctly
templates.env.filters["timestamp_to_date"] = timestamp_to_date
# Static files
app.mount("/static", StaticFiles(directory=BASE_DIR / "static"), name="static")
# Now safe to import dashboard (it needs templates)
from .routers import dashboard, bulletins
from .routers.message_detail import router as message_detail_router
from .routers.messages import html_router
from .routers.objects import router as objects_router
from .routers import objects_html
# initialize database
init_db()
# Include routers
app.include_router(public.router)
app.include_router(profile.router)
app.include_router(messages.router)
app.include_router(send.router)
app.include_router(dashboard.router)
app.include_router(bulletins.router)
app.include_router(bulletins.html_router)
app.include_router(message_detail_router)
app.include_router(html_router)
app.include_router(objects_router)
app.include_router(objects_html.router)