72 lines
1.6 KiB
Python
72 lines
1.6 KiB
Python
|
|
"""Jinkendo Kairo — FastAPI application entry point."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
|
||
|
|
from fastapi import FastAPI
|
||
|
|
from fastapi.middleware.cors import CORSMiddleware
|
||
|
|
|
||
|
|
from db import check_db
|
||
|
|
from version import APP_NAME, APP_VERSION, DB_SCHEMA_VERSION
|
||
|
|
|
||
|
|
if os.getenv("SKIP_DB_MIGRATE", "").strip().lower() in ("1", "true", "yes"):
|
||
|
|
print("[SKIP_DB_MIGRATE] Migrationen übersprungen")
|
||
|
|
else:
|
||
|
|
import run_migrations
|
||
|
|
|
||
|
|
exit_code = run_migrations.main()
|
||
|
|
if exit_code != 0:
|
||
|
|
print(f"[FAIL] Migrationen fehlgeschlagen (Exit {exit_code})")
|
||
|
|
sys.exit(exit_code)
|
||
|
|
|
||
|
|
allowed_origins = [
|
||
|
|
origin.strip()
|
||
|
|
for origin in os.getenv("ALLOWED_ORIGINS", "http://localhost:3097").split(",")
|
||
|
|
if origin.strip()
|
||
|
|
]
|
||
|
|
|
||
|
|
app = FastAPI(
|
||
|
|
title="Jinkendo Kairo",
|
||
|
|
version=APP_VERSION,
|
||
|
|
docs_url="/api/docs" if os.getenv("ENVIRONMENT", "development") != "production" else None,
|
||
|
|
redoc_url=None,
|
||
|
|
)
|
||
|
|
|
||
|
|
app.add_middleware(
|
||
|
|
CORSMiddleware,
|
||
|
|
allow_origins=allowed_origins,
|
||
|
|
allow_credentials=True,
|
||
|
|
allow_methods=["*"],
|
||
|
|
allow_headers=["*"],
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/api/health")
|
||
|
|
def api_health():
|
||
|
|
db_status = "ok"
|
||
|
|
try:
|
||
|
|
if not check_db():
|
||
|
|
db_status = "error"
|
||
|
|
except Exception:
|
||
|
|
db_status = "error"
|
||
|
|
|
||
|
|
status = "ok" if db_status == "ok" else "degraded"
|
||
|
|
return {
|
||
|
|
"status": status,
|
||
|
|
"app": APP_NAME,
|
||
|
|
"db": db_status,
|
||
|
|
"version": APP_VERSION,
|
||
|
|
"schema": DB_SCHEMA_VERSION,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/api/version")
|
||
|
|
def api_version():
|
||
|
|
return {
|
||
|
|
"app": APP_NAME,
|
||
|
|
"version": APP_VERSION,
|
||
|
|
"schema": DB_SCHEMA_VERSION,
|
||
|
|
}
|