-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip serialization for predictable responses
- Loading branch information
Showing
8 changed files
with
123 additions
and
98 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
from fastapi import Request | ||
from pyinstrument import Profiler | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from starlette.responses import HTMLResponse | ||
|
||
|
||
# https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-a-web-request-in-fastapi | ||
async def profiler_middleware(request: Request, call_next): | ||
profiling = request.query_params.get('profile', False) | ||
if profiling: | ||
profiler = Profiler() | ||
profiler.start() | ||
await call_next(request) | ||
profiler.stop() | ||
return HTMLResponse(profiler.output_html()) | ||
else: | ||
return await call_next(request) | ||
class ProfilerMiddleware(BaseHTTPMiddleware): | ||
# https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-a-web-request-in-fastapi | ||
async def dispatch(self, request: Request, call_next): | ||
profiling = request.query_params.get('profile', False) | ||
if profiling: | ||
profiler = Profiler() | ||
profiler.start() | ||
await call_next(request) | ||
profiler.stop() | ||
return HTMLResponse(profiler.output_html()) | ||
else: | ||
return await call_next(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from fastapi import Request | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
|
||
from config import VERSION | ||
|
||
|
||
async def version_middleware(request: Request, call_next): | ||
response = await call_next(request) | ||
response.headers['X-Version'] = VERSION | ||
return response | ||
class VersionMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next): | ||
response = await call_next(request) | ||
response.headers['X-Version'] = VERSION | ||
return response |