-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (87 loc) · 2.53 KB
/
Copy pathmain.py
File metadata and controls
107 lines (87 loc) · 2.53 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import datetime
from contextlib import asynccontextmanager
from pathlib import Path
import graphdoc # type: ignore
import graphql
import uvicorn
from graphql import GraphQLSyntaxError
from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi_mcp import AuthConfig, FastApiMCP
from starlette.responses import Response
from src.api.rest_routes import router as rest_router
from src.config import settings
from src.graphql_api import graphql_app
from src.middleware.auth import verify_auth
@asynccontextmanager
async def lifespan(_app: FastAPI):
"""Application lifespan (replaces deprecated on_event handlers)."""
_app.state.public_paths = {"/", "/graph/"}
yield
app = FastAPI(
title=settings.APP_NAME,
description=settings.APP_DESCRIPTION,
lifespan=lifespan,
)
app.include_router(rest_router)
app.mount("/graph", graphql_app)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["X-Forwarded-For", "Authorization", "Content-Type"],
)
@app.get("/")
async def root():
return {
"message": {
"app_name": settings.APP_NAME,
"system_time": datetime.datetime.now(),
"discovery_endpoint": "/mcp",
}
}
app.mount(
"/doc_templates",
StaticFiles(directory=Path(__file__).parent.absolute() / "./src/doc_templates"),
name="doc_templates",
)
@app.get("/graphql/docs", include_in_schema=False)
async def get_graphql_docs():
"""Handler for graphql docs."""
path = "./schema.graphql"
with open(path, "r", encoding="utf-8") as graphql_file:
schema = graphql_file.read()
try:
graphql.parse(schema)
except GraphQLSyntaxError as exc:
raise Exception(path, str(exc)) from exc
return Response(
content=graphdoc.to_doc(
schema, templates_path="src/doc_templates", use_cache=False
),
media_type="text/html",
)
if settings.AUTH_ENABLED:
mcp = FastApiMCP(
app,
name=settings.APP_NAME,
description=settings.APP_DESCRIPTION,
auth_config=AuthConfig(dependencies=[Depends(verify_auth)]),
)
else:
mcp = FastApiMCP(
app,
name=settings.APP_NAME,
description=settings.APP_DESCRIPTION,
)
mcp.mount_http()
mcp.setup_server()
if __name__ == "__main__":
uvicorn.run(
app,
host=settings.HOST,
port=settings.PORT,
reload=settings.DEBUG_MODE,
)