-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfactory.py
More file actions
87 lines (74 loc) · 2.66 KB
/
Copy pathfactory.py
File metadata and controls
87 lines (74 loc) · 2.66 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
from fastapi import FastAPI
from fastapi.routing import APIRoute
from fastapi.staticfiles import StaticFiles
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import FileResponse, RedirectResponse
from trapdata.api.config import settings
from trapdata.api.views import api_router
def create_app():
description = f"{settings.PROJECT_NAME} API"
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_PATH}/openapi.json",
docs_url="/docs/",
description=description,
redoc_url="/redoc/",
)
setup_routers(app)
setup_cors_middleware(app)
serve_static_app(app)
return app
def setup_routers(app: FastAPI) -> None:
app.include_router(api_router, prefix=settings.API_PATH)
# The following operation needs to be at the end of this function
use_route_names_as_operation_ids(app)
def serve_static_app(app):
app.mount(
"/static/crops",
StaticFiles(directory=settings.user_data_path / "crops"),
name="crops",
)
app.mount(
"/static/captures",
StaticFiles(directory=settings.image_base_path),
name="captures",
)
app.mount(
"/",
StaticFiles(directory="trapdata/webui/public"),
name="static",
)
@app.middleware("http")
async def _add_404_middleware(request: Request, call_next):
"""Serves static assets on 404"""
response = await call_next(request)
path = request["path"]
if path.startswith(settings.API_PATH) or path.startswith("/docs"):
return response
if response.status_code == 404:
return FileResponse("trapdata/webui/public/index.html")
return response
def setup_cors_middleware(app):
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=["*"],
expose_headers=["Content-Range", "Range"],
allow_headers=["Authorization", "Range", "Content-Range"],
)
def use_route_names_as_operation_ids(app: FastAPI) -> None:
"""
Simplify operation IDs so that generated API clients have simpler function
names.
Should be called only after all routes have been added.
"""
route_names = set()
for route in app.routes:
if isinstance(route, APIRoute):
if route.name in route_names:
raise Exception("Route function names should be unique")
route.operation_id = route.name
route_names.add(route.name)