|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Copyright 2022 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | + |
| 19 | +try: |
| 20 | + from typing import Optional |
| 21 | + from asgiref.compatibility import guarantee_single_callable |
| 22 | + from contextvars import ContextVar |
| 23 | + import fastapi |
| 24 | + from fastapi import FastAPI |
| 25 | + from starlette.routing import Match, Route |
| 26 | +except ImportError: |
| 27 | + fastapi = None |
| 28 | + |
| 29 | +context = ContextVar("context", default={}) |
| 30 | + |
| 31 | + |
| 32 | +def get_fastapi_info(): |
| 33 | + """ |
| 34 | + Get available info from the current FastAPI request, if we're in a |
| 35 | + FastAPI request-response cycle. Else, return an empty dict. |
| 36 | + """ |
| 37 | + info = {} |
| 38 | + if fastapi and context: |
| 39 | + info = context.get() |
| 40 | + return info |
| 41 | + |
| 42 | + |
| 43 | +class SQLCommenterMiddleware: |
| 44 | + """The ASGI application middleware. |
| 45 | + This class is an ASGI middleware that augment SQL statements before execution, |
| 46 | + with comments containing information about the code that caused its execution. |
| 47 | +
|
| 48 | + Args: |
| 49 | + app: The ASGI application callable to forward requests to. |
| 50 | + """ |
| 51 | + |
| 52 | + def __init__(self, app): |
| 53 | + self.app = guarantee_single_callable(app) |
| 54 | + |
| 55 | + async def __call__(self, scope, receive, send): |
| 56 | + """The ASGI application |
| 57 | + Args: |
| 58 | + scope: An ASGI environment. |
| 59 | + receive: An awaitable callable yielding dictionaries |
| 60 | + send: An awaitable callable taking a single dictionary as argument. |
| 61 | + """ |
| 62 | + if scope["type"] not in ("http", "websocket"): |
| 63 | + return await self.app(scope, receive, send) |
| 64 | + |
| 65 | + if not isinstance(scope["app"], FastAPI): |
| 66 | + return await self.app(scope, receive, send) |
| 67 | + |
| 68 | + fastapi_app = scope["app"] |
| 69 | + info = _get_fastapi_info(fastapi_app, scope) |
| 70 | + token = context.set(info) |
| 71 | + |
| 72 | + try: |
| 73 | + await self.app(scope, receive, send) |
| 74 | + finally: |
| 75 | + context.reset(token) |
| 76 | + |
| 77 | + |
| 78 | +def _get_fastapi_info(fastapi_app: FastAPI, scope) -> dict: |
| 79 | + info = { |
| 80 | + "framework": 'fastapi:%s' % fastapi.__version__, |
| 81 | + "app_name": fastapi_app.title, |
| 82 | + } |
| 83 | + |
| 84 | + route = _get_fastapi_route(fastapi_app, scope) |
| 85 | + if route: |
| 86 | + info["controller"] = route.name |
| 87 | + info["route"] = route.path |
| 88 | + |
| 89 | + return info |
| 90 | + |
| 91 | + |
| 92 | +def _get_fastapi_route(fastapi_app: FastAPI, scope) -> Optional[Route]: |
| 93 | + for route in fastapi_app.router.routes: |
| 94 | + # Determine if any route matches the incoming scope, |
| 95 | + # and return the route name if found. |
| 96 | + match, child_scope = route.matches(scope) |
| 97 | + if match == Match.FULL: |
| 98 | + return child_scope["route"] |
| 99 | + return None |
0 commit comments