"""FastAPI application setup for Naramarket HTTP server."""
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from ..core.config import APP_NAME, SERVER_VERSION
from .routes import router
logger = logging.getLogger("naramarket.api")
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan manager."""
# Startup
logger.info(f"Starting {APP_NAME} HTTP API server...")
yield
# Shutdown
logger.info("Shutting down HTTP API server...")
def create_app() -> FastAPI:
"""Create and configure FastAPI application."""
app = FastAPI(
title="Naramarket MCP Server API",
description="HTTP API for Naramarket data crawling and processing services",
version=SERVER_VERSION,
lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure appropriately for production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API routes
app.include_router(router)
# Global exception handler
@app.exception_handler(Exception)
async def global_exception_handler(request, exc):
logger.error(f"Global exception handler caught: {exc}")
return JSONResponse(
status_code=500,
content={
"error": "Internal server error",
"detail": str(exc) if app.debug else "An unexpected error occurred"
}
)
return app
# Create app instance
app = create_app()