Ограничен CORS через env-переменную ALLOW_ORIGINS
This commit is contained in:
+36
-4
@@ -1,3 +1,6 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
@@ -5,16 +8,38 @@ from . import models
|
|||||||
from .database import engine
|
from .database import engine
|
||||||
from .routers import auth, templates, instances, admin, console
|
from .routers import auth, templates, instances, admin, console
|
||||||
|
|
||||||
|
# Структурированное логирование: единый формат для контейнера.
|
||||||
|
logging.basicConfig(
|
||||||
|
level=os.getenv("LOG_LEVEL", "INFO"),
|
||||||
|
format="%(asctime)s %(levelname)s %(name)s [%(filename)s:%(lineno)d] %(message)s",
|
||||||
|
)
|
||||||
|
logger = logging.getLogger("panel")
|
||||||
|
|
||||||
models.Base.metadata.create_all(bind=engine)
|
models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
app = FastAPI(title="Proxmox VPS Panel", version="0.1.0")
|
app = FastAPI(
|
||||||
|
title="Proxmox VPS Panel",
|
||||||
|
version="0.2.0",
|
||||||
|
description="Self-hosted панель для управления VPS на Proxmox VE.",
|
||||||
|
)
|
||||||
|
|
||||||
|
# CORS: список доменов через переменную окружения ALLOW_ORIGINS (через запятую).
|
||||||
|
# Дефолт — только локальный фронтенд. В проде обязательно задать конкретный домен.
|
||||||
|
_origins_raw = os.getenv("ALLOW_ORIGINS", "http://localhost:5173,http://127.0.0.1:5173")
|
||||||
|
allow_origins = [o.strip() for o in _origins_raw.split(",") if o.strip()]
|
||||||
|
|
||||||
|
if "*" in allow_origins:
|
||||||
|
logger.warning(
|
||||||
|
"ALLOW_ORIGINS='*' — это небезопасно для прода и не работает с allow_credentials=True. "
|
||||||
|
"Укажите конкретные домены."
|
||||||
|
)
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"], # в проде укажите конкретный домен фронтенда
|
allow_origins=allow_origins,
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_methods=["*"],
|
allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
||||||
allow_headers=["*"],
|
allow_headers=["Authorization", "Content-Type"],
|
||||||
)
|
)
|
||||||
|
|
||||||
app.include_router(auth.router)
|
app.include_router(auth.router)
|
||||||
@@ -26,4 +51,11 @@ app.include_router(console.router)
|
|||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
def health():
|
def health():
|
||||||
|
"""Healthcheck — проверяет только то, что приложение живо."""
|
||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health/ready")
|
||||||
|
def readiness():
|
||||||
|
"""Readiness — можно расширить проверкой БД и Proxmox при необходимости."""
|
||||||
|
return {"status": "ready"}
|
||||||
|
|||||||
Reference in New Issue
Block a user