main.py•2.03 kB
"""
Main application initialization for the Sectional MCP Panel.
Combines API and UI servers.
"""
import os
import logging
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from src.database.database import init_db
from src.api import api_router
from src.ui.ui_server import ui_app
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger("mcp_panel")
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Lifecycle events for the FastAPI application.
This runs before the application starts and after it shuts down.
"""
# Startup: Initialize the database
logger.info("Initializing MCP Panel application")
await init_db()
logger.info("Database initialized")
# Yield control to the application
yield
# Shutdown: Cleanup resources
logger.info("Shutting down MCP Panel application")
# Create FastAPI application
app = FastAPI(
title="Sectional MCP Panel",
description="Management service for MCP servers organized into sections",
version="0.1.0",
lifespan=lifespan,
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # For development; restrict in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Root endpoint
@app.get("/")
async def root():
"""Root endpoint for the API."""
return {
"name": "Sectional MCP Panel",
"version": "0.1.0",
"status": "running"
}
# Health check endpoint
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {"status": "healthy"}
# Include API router
app.include_router(api_router)
# Mount UI application
app.mount("/ui", ui_app)
# Run the application if executed directly
if __name__ == "__main__":
uvicorn.run("src.main:app", host="0.0.0.0", port=8000, reload=True)