81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
"""
|
|
FastAPI Web Server for CADDesigner.
|
|
Web server implementation compatible with the OpenAI API specification.
|
|
"""
|
|
|
|
from contextlib import asynccontextmanager
|
|
import uvicorn
|
|
|
|
from bootstrap_env import load_project_env
|
|
|
|
load_project_env()
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from .state import server_state
|
|
from .routers import (
|
|
health_router,
|
|
agent_router,
|
|
conversation_router,
|
|
chat_router,
|
|
)
|
|
from .error_handlers import not_found_handler, internal_error_handler
|
|
from SimpleLLMFunc.logger import push_error, app_log
|
|
from SimpleLLMFunc.observability.langfuse_client import flush_all_observations
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Application lifecycle management."""
|
|
app_log("🚀 Initializing CADDesigner Web Server...")
|
|
try:
|
|
server_state.initialize()
|
|
app_log("✅ CADDesigner initialized successfully!")
|
|
except Exception as e:
|
|
push_error(f"❌ Failed to initialize CADDesigner: {e}")
|
|
raise
|
|
|
|
yield
|
|
|
|
app_log("🔄 Shutting down CADDesigner Web Server...")
|
|
try:
|
|
flush_all_observations()
|
|
app_log("✅ Flushed Langfuse observations")
|
|
except Exception as e:
|
|
push_error(f"⚠️ Failed to flush Langfuse observations: {e}")
|
|
|
|
|
|
# Create the FastAPI application.
|
|
app = FastAPI(
|
|
title="CADDesigner API",
|
|
description="OpenAI-compatible API for CADDesigner",
|
|
version="1.0.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# Add CORS middleware.
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # TODO: Restrict specific domains in production.
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Register routers.
|
|
app.include_router(health_router)
|
|
app.include_router(agent_router)
|
|
app.include_router(conversation_router)
|
|
app.include_router(chat_router)
|
|
|
|
# Register error handlers.
|
|
app.add_exception_handler(404, not_found_handler)
|
|
app.add_exception_handler(500, internal_error_handler)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run("web_interface.server:app", host="0.0.0.0", port=8000, reload=True)
|