Initial commit: AegisSight-Monitor-Verwaltung
Dieser Commit ist enthalten in:
81
src/main.py
Normale Datei
81
src/main.py
Normale Datei
@@ -0,0 +1,81 @@
|
||||
"""Verwaltungsportal - FastAPI Anwendung."""
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Depends, HTTPException, status
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
from config import STATIC_DIR, PORT
|
||||
from database import db_dependency
|
||||
from auth import verify_password, create_token
|
||||
from models import LoginRequest, TokenResponse
|
||||
from routers import organizations, licenses, users, dashboard
|
||||
|
||||
import aiosqlite
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
|
||||
)
|
||||
logger = logging.getLogger("verwaltung")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("Verwaltungsportal gestartet auf Port %s", PORT)
|
||||
yield
|
||||
logger.info("Verwaltungsportal beendet")
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="IntelSight Verwaltungsportal",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
# --- Routen ---
|
||||
app.include_router(organizations.router)
|
||||
app.include_router(licenses.router)
|
||||
app.include_router(users.router)
|
||||
app.include_router(dashboard.router)
|
||||
|
||||
|
||||
# --- Login ---
|
||||
@app.post("/api/auth/login", response_model=TokenResponse)
|
||||
async def login(
|
||||
data: LoginRequest,
|
||||
db: aiosqlite.Connection = Depends(db_dependency),
|
||||
):
|
||||
cursor = await db.execute(
|
||||
"SELECT id, username, password_hash FROM portal_admins WHERE username = ?",
|
||||
(data.username,),
|
||||
)
|
||||
admin = await cursor.fetchone()
|
||||
if not admin or not verify_password(data.password, admin["password_hash"]):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Ungueltige Zugangsdaten",
|
||||
)
|
||||
|
||||
token = create_token(admin["id"], admin["username"])
|
||||
return TokenResponse(access_token=token, username=admin["username"])
|
||||
|
||||
|
||||
# --- Statische Dateien ---
|
||||
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def index():
|
||||
return FileResponse(f"{STATIC_DIR}/index.html")
|
||||
|
||||
|
||||
@app.get("/dashboard")
|
||||
async def dashboard_page():
|
||||
return FileResponse(f"{STATIC_DIR}/dashboard.html")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run("main:app", host="0.0.0.0", port=PORT, reload=True)
|
||||
In neuem Issue referenzieren
Einen Benutzer sperren