-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
148 lines (104 loc) · 4.05 KB
/
app.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import time
from typing import Optional
from fastapi import FastAPI, Request
from pydantic import BaseModel
from starlette.datastructures import MutableHeaders
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from fastapi.responses import ORJSONResponse, UJSONResponse, JSONResponse
import json
# from https://fastapi.tiangolo.com/tutorial/body/#import-pydantics-basemodel
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
resp_class = os.getenv("JSONRESPONSECLASS", "JSONResponse")
resp_classes = {
"ORJSONResponse": ORJSONResponse,
"UJSONResponse": UJSONResponse,
"JSONResponse": JSONResponse
}
if resp_class in ["ORJSONResponse", "UJSONResponse", "JSONResponse"]:
print(f"Set {resp_class}")
else:
print(f"Unsupported response class: {resp_class}, falling back to JSONResponse")
resp_class = "JSONResponse"
app = FastAPI(debug=False, default_response_class=resp_classes.get(resp_class))
json_data = json.load(open('./test_json_1MB.json'))
@app.post("/async/items/")
async def create_item(item: Item):
return item
@app.post("/sync/items/")
def create_item(item: Item):
return item
@app.post("/async/big_json_response/")
async def big_json_response(item: Item):
_ = item
return json_data
@app.post("/sync/big_json_response/")
def big_json_response(item: Item):
_ = item
return json_data
# from https://fastapi.tiangolo.com/tutorial/middleware/
if os.getenv("PROCESSTIMEMIDDLEWARE"):
print("Load PROCESSTIMEMIDDLEWARE")
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
# from https://www.starlette.io/middleware/#basehttpmiddleware
if os.getenv("CUSTOMHEADERMIDDLEWARE"):
print("Load CUSTOMHEADERMIDDLEWARE")
class CustomHeaderMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
response = await call_next(request)
response.headers["Custom"] = "Example"
return response
app.add_middleware(CustomHeaderMiddleware)
class STARLETTEProcessTimeMiddleware:
app: ASGIApp
def __init__(
self,
app: ASGIApp,
) -> None:
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] != "http":
await self.app(scope, receive, send)
return
start_time = time.time()
async def send_wrapper(message: Message) -> None:
if message["type"] == "http.response.start":
headers = MutableHeaders(scope=message)
headers.append("X-Process-Time", str(time.time() - start_time))
await send(message)
await self.app(scope, receive, send_wrapper)
if os.getenv("STARLETTEPROCESSTIMEIDDLEWARE"):
print("Load STARLETTEPROCESSTIMEIDDLEWARE")
app.add_middleware(STARLETTEProcessTimeMiddleware)
class STARLETTECustomHeaderMiddleware:
"""Load request ID from headers if present. Generate one otherwise."""
app: ASGIApp
def __init__(
self,
app: ASGIApp,
) -> None:
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] != "http":
await self.app(scope, receive, send)
return
async def send_wrapper(message: Message) -> None:
if message["type"] == "http.response.start":
headers = MutableHeaders(scope=message)
headers.append("Custom", "Example")
await send(message)
await self.app(scope, receive, send_wrapper)
if os.getenv("STARLETTECUSTOMHEADERMIDDLEWARE"):
print("Load STARLETTECUSTOMHEADERMIDDLEWARE")
app.add_middleware(STARLETTECustomHeaderMiddleware)