forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (57 loc) · 1.94 KB
/
Copy pathmain.py
File metadata and controls
77 lines (57 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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)