62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
import logging
|
||
import os
|
||
|
||
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
|
||
from . import models
|
||
from .database import engine
|
||
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)
|
||
|
||
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(
|
||
CORSMiddleware,
|
||
allow_origins=allow_origins,
|
||
allow_credentials=True,
|
||
allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
||
allow_headers=["Authorization", "Content-Type"],
|
||
)
|
||
|
||
app.include_router(auth.router)
|
||
app.include_router(templates.router)
|
||
app.include_router(instances.router)
|
||
app.include_router(admin.router)
|
||
app.include_router(console.router)
|
||
|
||
|
||
@app.get("/health")
|
||
def health():
|
||
"""Healthcheck — проверяет только то, что приложение живо."""
|
||
return {"status": "ok"}
|
||
|
||
|
||
@app.get("/health/ready")
|
||
def readiness():
|
||
"""Readiness — можно расширить проверкой БД и Proxmox при необходимости."""
|
||
return {"status": "ready"}
|