52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
# app/main.py
|
|
import asyncio
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""FastAPI 애플리케이션 생명주기 관리"""
|
|
# Startup - 애플리케이션 시작 시
|
|
print("Starting up...")
|
|
|
|
try:
|
|
from config import prj_settings
|
|
|
|
# DEBUG 모드일 때만 데이터베이스 테이블 자동 생성
|
|
if prj_settings.DEBUG:
|
|
from app.database.session import create_db_tables
|
|
|
|
await create_db_tables()
|
|
print("Database tables created (DEBUG mode)")
|
|
except asyncio.TimeoutError:
|
|
print("Database initialization timed out")
|
|
# 타임아웃 시 앱 시작 중단하려면 raise, 계속하려면 pass
|
|
raise
|
|
except Exception as e:
|
|
print(f"Database initialization failed: {e}")
|
|
# 에러 시 앱 시작 중단하려면 raise, 계속하려면 pass
|
|
raise
|
|
|
|
yield # 애플리케이션 실행 중
|
|
|
|
# Shutdown - 애플리케이션 종료 시
|
|
print("Shutting down...")
|
|
|
|
# 공유 HTTP 클라이언트 종료
|
|
from app.utils.creatomate import close_shared_client
|
|
from app.utils.upload_blob_as_request import close_shared_blob_client
|
|
|
|
await close_shared_client()
|
|
await close_shared_blob_client()
|
|
|
|
# 데이터베이스 엔진 종료
|
|
from app.database.session import dispose_engine
|
|
|
|
await dispose_engine()
|
|
|
|
|
|
# FastAPI 앱 생성 (lifespan 적용)
|
|
app = FastAPI(title="CastAD", lifespan=lifespan)
|