141 lines
4.2 KiB
Python
141 lines
4.2 KiB
Python
import os
|
|
from app.core.env_setting import EnvSetting
|
|
from fastapi import FastAPI
|
|
from contextlib import asynccontextmanager
|
|
from app.core.database import create_tables, drop_tables
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from starlette.middleware.sessions import SessionMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from app.presentation.api.v1.auth import router as auth_router
|
|
from app.presentation.api.v1.user import router as user_router
|
|
from app.presentation.api.v1.social import router as social_router
|
|
from app.presentation.api.v1.moviemakers import router as moviemaker_router
|
|
from app.presentation.api.v1.uservideo import router as uservideo_router
|
|
from app.presentation.api.v1.ai import router as ai_router
|
|
from app.infra.google.redis import redis_manager
|
|
|
|
settings = EnvSetting()
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""애플리케이션 생명주기 관리"""
|
|
# 애플리케이션 시작 시
|
|
settings = EnvSetting()
|
|
print(f"📍 Environment: {settings.ENVIRONMENT}")
|
|
|
|
try:
|
|
await redis_manager.connect()
|
|
print("✅ Redis 연결 성공")
|
|
except Exception as e:
|
|
print(f"❌ Redis 연결 실패: {e}")
|
|
# Redis 연결 실패 시에도 애플리케이션은 계속 실행
|
|
|
|
yield # 애플리케이션 실행 중
|
|
|
|
# 애플리케이션 종료 시
|
|
try:
|
|
await redis_manager.close()
|
|
print("✅ Redis 연결 해제 완료")
|
|
except Exception as e:
|
|
print(f"❌ Redis 연결 해제 중 오류: {e}")
|
|
|
|
def create_app() -> FastAPI:
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
|
version="1.0.0",
|
|
lifespan=lifespan # 생명주기 관리 추가
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 세션 미들웨어 설정
|
|
app.add_middleware(SessionMiddleware, secret_key=settings.SESSION_SECRET_KEY)
|
|
|
|
create_tables()
|
|
os.makedirs("uploads", exist_ok=True)
|
|
os.makedirs("crawling_results", exist_ok=True)
|
|
os.makedirs("success_log", exist_ok=True)
|
|
|
|
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")
|
|
app.mount("/crawling_results", StaticFiles(directory="crawling_results"), name="crawling_results")
|
|
app.mount("/ai", StaticFiles(directory="ai"), name="ai")
|
|
app.mount("/success_log", StaticFiles(directory="success_log"), name="success_log")
|
|
|
|
# 임시 코드 추가
|
|
os.makedirs("test-video", exist_ok=True)
|
|
app.mount("/test-video", StaticFiles(directory="test-video"), name="test-video")
|
|
|
|
# 라우터 등록
|
|
app.include_router(auth_router)
|
|
app.include_router(social_router)
|
|
app.include_router(user_router)
|
|
app.include_router(moviemaker_router)
|
|
app.include_router(uservideo_router)
|
|
app.include_router(ai_router)
|
|
# app.include_router(video_router)
|
|
# app.include_router(items_router)
|
|
# app.include_router(profile_router)
|
|
|
|
return app
|
|
|
|
# API 라우터 등록
|
|
# app.include_router(
|
|
# users.router,
|
|
# prefix=f"{settings.API_V1_STR}/users",
|
|
# tags=["users"]
|
|
# )
|
|
|
|
app = create_app()
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {
|
|
"message": "FastAPI DDD with Celery Workers",
|
|
"version": "1.0.0",
|
|
"docs": "/docs"
|
|
}
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {
|
|
"status": "healthy",
|
|
"database": "connected",
|
|
"redis": "connected",
|
|
"celery": "running"
|
|
}
|
|
|
|
from app.services.mureka_service_fix import MurekaServiceFix
|
|
from pydantic import BaseModel
|
|
|
|
class CreateMusic(BaseModel):
|
|
lyrics: str
|
|
prompt: str
|
|
|
|
class GetMusic(BaseModel):
|
|
task_id: str
|
|
|
|
@app.get("/test")
|
|
async def test():
|
|
mureka_service = MurekaServiceFix()
|
|
response = mureka_service.connect_mureka()
|
|
return response
|
|
|
|
@app.post("/test2")
|
|
async def test2(req: CreateMusic):
|
|
mureka_service = MurekaServiceFix()
|
|
response = mureka_service.generate_music(req.lyrics, req.prompt)
|
|
return response
|
|
|
|
@app.get("/test3")
|
|
async def test3(req: GetMusic):
|
|
mureka_service = MurekaServiceFix()
|
|
response = mureka_service.get_music(req.task_id)
|
|
return response |