Some checks are pending
CI / Crawler (Python ${{ matrix.python-version }}) (3.10) (push) Waiting to run
CI / Crawler (Python ${{ matrix.python-version }}) (3.11) (push) Waiting to run
CI / API (Python 3.11) (push) Waiting to run
CI / Database migration (push) Waiting to run
CI / App web build (Node 20) (push) Waiting to run
91 lines
2.4 KiB
Python
91 lines
2.4 KiB
Python
"""
|
|
FastAPI 애플리케이션 진입점
|
|
- 태양광 발전 관제 시스템 미들웨어 서버
|
|
"""
|
|
|
|
from fastapi import Depends, FastAPI, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from supabase import Client
|
|
|
|
from app.core.config import get_settings
|
|
from app.core.database import get_db
|
|
from app.routers import plants, upload, stats
|
|
from app.schemas.health import DetailedHealthResponse, HealthResponse
|
|
|
|
# 설정 로드
|
|
settings = get_settings()
|
|
|
|
# FastAPI 앱 생성
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
version=settings.APP_VERSION,
|
|
description="태양광 발전 관제 시스템을 위한 미들웨어 API 서버",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc"
|
|
)
|
|
|
|
# CORS 미들웨어 설정 (모든 도메인 허용 - 개발용)
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 라우터 등록 (순서 중요: 더 구체적인 경로를 먼저 등록)
|
|
app.include_router(stats.router) # /plants/{plant_id}/stats
|
|
app.include_router(upload.router) # /plants/{plant_id}/upload
|
|
app.include_router(plants.router) # /plants/{company_id} (가장 일반적인 경로)
|
|
|
|
|
|
@app.get("/", tags=["Health"], response_model=HealthResponse)
|
|
def health_check() -> HealthResponse:
|
|
"""
|
|
서버 상태 확인 (Health Check)
|
|
|
|
Returns:
|
|
서버 상태 및 버전 정보
|
|
"""
|
|
return HealthResponse(
|
|
status="healthy",
|
|
app_name=settings.APP_NAME,
|
|
version=settings.APP_VERSION,
|
|
)
|
|
|
|
|
|
@app.get("/health", tags=["Health"], response_model=DetailedHealthResponse)
|
|
def detailed_health_check(
|
|
db: Client = Depends(get_db),
|
|
) -> DetailedHealthResponse:
|
|
"""
|
|
상세 서버 상태 확인
|
|
|
|
Returns:
|
|
서버 상태 및 연결 정보
|
|
"""
|
|
try:
|
|
db.table("plants").select("id").limit(1).execute()
|
|
except Exception as exc:
|
|
raise HTTPException(
|
|
status_code=503,
|
|
detail="데이터베이스 연결 상태를 확인할 수 없습니다.",
|
|
) from exc
|
|
|
|
return DetailedHealthResponse(
|
|
status="healthy",
|
|
app_name=settings.APP_NAME,
|
|
version=settings.APP_VERSION,
|
|
supabase_connected=True,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"app.main:app",
|
|
host="0.0.0.0",
|
|
port=8000,
|
|
reload=settings.DEBUG
|
|
)
|