Files
yunke/yunke-server/app/api/main.py
T

48 lines
1.5 KiB
Python
Raw Normal View History

2026-08-01 12:10:03 +08:00
"""FastAPI app for the yunke data-mirror viewing API.
Run locally with::
uvicorn app.api.main:app --reload
Includes:
* ``GET /api/health`` -> ``{"status": "ok"}`` liveness probe.
* ``GET /api/customers`` (see :mod:`app.api.routers.customer`).
2026-08-01 14:13:50 +08:00
* ``GET /api/media/{media_hash}`` (see :mod:`app.api.routers.media`).
2026-08-01 12:10:03 +08:00
CORS is configured permissively (wildcard origin, no credentials) so the local
2026-08-01 14:30:28 +08:00
frontend can call the API during development.
Auth (Task 2.9): a single shared-token dependency (:func:`app.api.auth.require_auth`)
is applied globally on the app. Trust model: when ``Settings.api_token`` is
empty (the default) auth is disabled — we trust the network boundary (this is a
local/intranet tool). When a token is configured, every request must present it
via ``Authorization: Bearer <token>`` or ``?token=``. ``/api/health`` is exempt
so liveness probes work without the token. See :mod:`app.api.auth`.
2026-08-01 12:10:03 +08:00
"""
from __future__ import annotations
2026-08-01 14:30:28 +08:00
from fastapi import Depends, FastAPI
2026-08-01 12:10:03 +08:00
from fastapi.middleware.cors import CORSMiddleware
2026-08-01 14:30:28 +08:00
from app.api.auth import require_auth
2026-08-01 14:13:50 +08:00
from app.api.routers import customer, media
2026-08-01 12:10:03 +08:00
2026-08-01 14:30:28 +08:00
app = FastAPI(title="yunke data mirror", dependencies=[Depends(require_auth)])
2026-08-01 12:10:03 +08:00
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(customer.router)
2026-08-01 14:13:50 +08:00
app.include_router(media.router)
2026-08-01 12:10:03 +08:00
@app.get("/api/health")
async def health() -> dict[str, str]:
"""Trivial liveness probe."""
return {"status": "ok"}