- FastAPI app med 7 bundled module-typer (proxmox, esxi, truenas, splunk, linux, windows, http-json) - Multi-instance support: flere instanser af samme modultype med adskilte assets - Community/Enterprise licensing med admin enterprise-toggle - Capacity forecasting, trends, what-if simulator, alert policies - MCP server + LLM copilot integration - Dockerfile + rune.yaml til Yggdrasil
65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
"""Headroom application entrypoint."""
|
|
from __future__ import annotations
|
|
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
from typing import AsyncIterator
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.responses import FileResponse, JSONResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from . import capacity, config, db, registry, seed
|
|
from .routes import router
|
|
from .scheduler import scheduler
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
|
config.ensure_dirs()
|
|
db.init_db()
|
|
registry.sync_modules()
|
|
if seed.seed_if_empty():
|
|
print(f"[headroom] seeded demo fleet ({config.SEED_DAYS} days of metrics)", flush=True)
|
|
try:
|
|
capacity.evaluate_policies()
|
|
except Exception as exc:
|
|
print(f"[headroom] initial policy evaluation failed: {exc}", flush=True)
|
|
if config.SCHEDULER_ENABLED:
|
|
await scheduler.start()
|
|
print(f"[headroom] ready — http://127.0.0.1:8787 (data: {config.DATA_DIR})", flush=True)
|
|
yield
|
|
await scheduler.stop()
|
|
|
|
|
|
app = FastAPI(
|
|
title="Headroom Capacity API",
|
|
description="Modular capacity management for homelabs — REST, MCP and plugin modules.",
|
|
version=config.APP_VERSION,
|
|
lifespan=lifespan,
|
|
)
|
|
app.include_router(router)
|
|
|
|
|
|
@app.get("/", include_in_schema=False)
|
|
def index() -> FileResponse:
|
|
return FileResponse(config.STATIC_DIR / "index.html")
|
|
|
|
|
|
if config.STATIC_DIR.exists():
|
|
app.mount("/static", StaticFiles(directory=str(config.STATIC_DIR)), name="static")
|
|
|
|
|
|
@app.get("/{path:path}", include_in_schema=False)
|
|
def spa(path: str):
|
|
if path.startswith(("api/", "docs", "openapi.json", "redoc")):
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
candidate = (config.STATIC_DIR / path).resolve()
|
|
try:
|
|
candidate.relative_to(config.STATIC_DIR.resolve())
|
|
except ValueError:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
if candidate.is_file():
|
|
return FileResponse(candidate)
|
|
return FileResponse(config.STATIC_DIR / "index.html")
|