-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (63 loc) · 1.89 KB
/
Copy pathmain.py
File metadata and controls
81 lines (63 loc) · 1.89 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
import datetime
from contextlib import asynccontextmanager
from pathlib import Path
import graphdoc # type: ignore
import graphql
import uvicorn
from graphql import GraphQLSyntaxError
from starlette.responses import Response
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from src.config import settings
from src.graphql_api import graphql_app
@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, lifespan=lifespan)
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(),
}
}
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 __name__ == "__main__":
uvicorn.run(
"main:app",
host=settings.HOST,
reload=settings.DEBUG_MODE,
port=settings.PORT,
)