Spaces:
Build error
Build error
| from __future__ import annotations | |
| import os | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse, RedirectResponse | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.openapi.docs import get_swagger_ui_html | |
| from app.memory import init_db | |
| from app.openenv_env.api import router as openenv_router | |
| from app.routes import router | |
| from app.utils import setup_logging | |
| from app.config import settings | |
| def create_app() -> FastAPI: | |
| setup_logging(settings.log_level) | |
| init_db(settings.sqlite_path) | |
| app = FastAPI(title="AI Email Assistant", version="1.0.0", docs_url=None) | |
| # Dev-friendly CORS. Restrict allow_origins in production. | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| app.include_router(router) | |
| app.include_router(openenv_router) | |
| # Serve the optional frontend for convenience. | |
| frontend_dir = os.path.join(os.path.dirname(__file__), "frontend") | |
| has_frontend = os.path.isdir(frontend_dir) | |
| if has_frontend: | |
| app.mount("/ui", StaticFiles(directory=frontend_dir, html=True), name="ui") | |
| static_dir = os.path.join(os.path.dirname(__file__), "static") | |
| os.makedirs(static_dir, exist_ok=True) | |
| app.mount("/static", StaticFiles(directory=static_dir), name="static") | |
| async def custom_swagger_ui_html(): | |
| return get_swagger_ui_html( | |
| openapi_url=app.openapi_url, | |
| title=app.title + " - Swagger UI", | |
| oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, | |
| swagger_css_url="/static/swagger.css", | |
| ) | |
| async def root(): | |
| if has_frontend: | |
| return RedirectResponse(url="/ui/") | |
| return JSONResponse( | |
| { | |
| "name": app.title, | |
| "version": app.version, | |
| "docs": {"swagger_ui": "/docs", "redoc": "/redoc", "openapi_json": "/openapi.json"}, | |
| "endpoints": {"api": ["/api/v1"]}, | |
| } | |
| ) | |
| return app | |
| app = create_app() | |