from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import Response import yaml import os from database import init_db from routers import posts, comments, likes # Load OpenAPI spec from workspace root openapi_path = os.path.join(os.path.dirname(__file__), "..", "openapi.yaml") with open(openapi_path, "r", encoding="utf-8") as f: original_openapi_yaml = f.read() with open(openapi_path, "r", encoding="utf-8") as f: openapi_spec = yaml.safe_load(f) # Create FastAPI app with custom OpenAPI app = FastAPI( title=openapi_spec["info"]["title"], description=openapi_spec["info"]["description"], version=openapi_spec["info"]["version"], docs_url="/", redoc_url=None, openapi_url="/openapi.json" ) # CORS - Allow all origins app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.on_event("startup") def startup_event(): """Initialize database on startup""" init_db() @app.get("/openapi.yaml", include_in_schema=False) def get_openapi_yaml(): """Serve the exact OpenAPI YAML file""" return Response(content=original_openapi_yaml, media_type="application/x-yaml") @app.get("/openapi.json", include_in_schema=False) def get_openapi_json(): """Serve the exact OpenAPI spec as JSON""" return openapi_spec # Include routers with /api prefix app.include_router(posts.router, prefix="/api") app.include_router(comments.router, prefix="/api") app.include_router(likes.router, prefix="/api") def custom_openapi(): """Use the loaded OpenAPI spec exactly as is""" if app.openapi_schema: return app.openapi_schema # Return the original spec without any modifications app.openapi_schema = openapi_spec return app.openapi_schema app.openapi = custom_openapi if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)