87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
"""Trekker FastAPI entry point."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import FileResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from app.config import settings
|
|
from app.deps import get_store
|
|
from app.ingest.watchdog_daemon import WatchdogDaemon
|
|
from app.routes import export, offline_maps, sync, trails, upload, weather
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
UI_DIR = Path(__file__).resolve().parent.parent / "ui"
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
daemon = WatchdogDaemon(
|
|
get_store(),
|
|
settings.watch.internal_path,
|
|
settings.watch.remote_path,
|
|
settings.watch.poll_interval,
|
|
)
|
|
daemon.start()
|
|
yield
|
|
daemon.stop()
|
|
|
|
|
|
app = FastAPI(title="Trekker", version="0.1.0", lifespan=lifespan)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(upload.router)
|
|
app.include_router(trails.router)
|
|
app.include_router(weather.router)
|
|
app.include_router(export.router)
|
|
app.include_router(offline_maps.router)
|
|
app.include_router(sync.router)
|
|
|
|
|
|
@app.get("/api/health")
|
|
def health():
|
|
return {"status": "ok", "version": app.version, "host_role": settings.host_role}
|
|
|
|
|
|
@app.get("/api/config")
|
|
def public_config():
|
|
"""Non-secret config the UI needs."""
|
|
return {
|
|
"host_role": settings.host_role,
|
|
"tiles_base_url": settings.tiles.base_url,
|
|
"tiles_terrain_url": settings.tiles.terrain_url,
|
|
"max_pack_tiles": settings.tiles.max_pack_tiles,
|
|
"sync_peer_configured": bool(settings.sync.peer_url),
|
|
"llm_configured": bool(settings.llm.api_key or settings.llm.base_url),
|
|
"watch": {
|
|
"internal_path": settings.watch.internal_path,
|
|
"remote_path": settings.watch.remote_path,
|
|
},
|
|
}
|
|
|
|
|
|
if UI_DIR.is_dir():
|
|
app.mount("/app/static", StaticFiles(directory=UI_DIR), name="ui")
|
|
|
|
@app.get("/app")
|
|
@app.get("/app/{path:path}")
|
|
def spa(path: str = ""):
|
|
return FileResponse(UI_DIR / "index.html")
|
|
|
|
@app.get("/")
|
|
def root():
|
|
return FileResponse(UI_DIR / "index.html")
|