Archived
Добавлены healthcheck и строгий CORS
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from sqlalchemy import create_engine, text
|
||||||
|
|
||||||
|
from .config import settings
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||||
|
)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
app = FastAPI(
|
||||||
|
title="Proxmox VPS Panel Secure",
|
||||||
|
version="1.0.0",
|
||||||
|
)
|
||||||
|
|
||||||
|
origins = [item.strip() for item in settings.allow_origins.split(",") if item.strip()]
|
||||||
|
if not origins:
|
||||||
|
raise RuntimeError("ALLOW_ORIGINS must contain at least one explicit origin")
|
||||||
|
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=origins,
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
||||||
|
allow_headers=["Authorization", "Content-Type"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
def health() -> dict[str, str]:
|
||||||
|
"""Проверяет, что процесс приложения запущен."""
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health/ready")
|
||||||
|
def readiness() -> dict[str, str]:
|
||||||
|
"""Проверяет доступность критичной зависимости PostgreSQL."""
|
||||||
|
engine = create_engine(settings.database_url, pool_pre_ping=True)
|
||||||
|
try:
|
||||||
|
with engine.connect() as connection:
|
||||||
|
connection.execute(text("SELECT 1"))
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
|
return {"status": "ready"}
|
||||||
Reference in New Issue
Block a user