|
1 | 1 | from fastapi import FastAPI |
2 | 2 | from starlette.middleware import Middleware |
3 | | -from starlette_context import plugins |
| 3 | +from starlette_context import context, plugins |
4 | 4 | from starlette_context.middleware import ContextMiddleware |
| 5 | +from sqlalchemy import create_engine |
5 | 6 |
|
6 | 7 | from seedwork.domain.value_objects import UUID |
7 | 8 | from seedwork.infrastructure.repository import InMemoryRepository |
|
12 | 13 | import api.config as config |
13 | 14 | from api.routers import catalog, users |
14 | 15 |
|
| 16 | +# database engine |
| 17 | +engine = create_engine(config.DATABASE_URL, echo=config.DEBUG) |
| 18 | + |
| 19 | + |
15 | 20 | middleware = [ |
16 | 21 | Middleware( |
17 | 22 | ContextMiddleware, |
18 | 23 | plugins=(plugins.RequestIdPlugin(), plugins.CorrelationIdPlugin()), |
19 | 24 | ) |
20 | 25 | ] |
21 | 26 |
|
22 | | -app = FastAPI(debug=config.DEBUG) |
| 27 | +app = FastAPI(debug=config.DEBUG, middleware=middleware) |
23 | 28 |
|
24 | 29 | app.include_router(catalog.router) |
25 | 30 | app.include_router(users.router) |
| 31 | +app.engine = engine |
26 | 32 |
|
27 | 33 |
|
28 | 34 | @app.get("/") |
29 | 35 | async def root(): |
30 | | - # print(request) |
31 | 36 | return {"info": "Online auctions API. See /docs for documentation"} |
| 37 | + |
| 38 | + |
| 39 | +@app.get("/test") |
| 40 | +async def test(): |
| 41 | + print((context.get("X-Request-ID"))) |
| 42 | + return {"test": "test"} |
| 43 | + |
| 44 | + |
| 45 | +# #### |
| 46 | +# from fastapi import Depends |
| 47 | + |
| 48 | + |
| 49 | +# def common_parameters(x: str, y: int): |
| 50 | +# print(args, kwargs) |
| 51 | +# return None |
| 52 | + |
| 53 | + |
| 54 | +# def common_parameters2(z: int = 10): |
| 55 | +# print(args, kwargs) |
| 56 | +# return None |
| 57 | + |
| 58 | + |
| 59 | +# def get_context(): |
| 60 | +# context = object() |
| 61 | +# context.correlation_id = "1234" |
| 62 | +# return context |
| 63 | + |
| 64 | + |
| 65 | +# @app.get("/dependency") |
| 66 | +# async def root( |
| 67 | +# # foo=Depends(common_parameters), |
| 68 | +# # bar=Depends(common_parameters2), |
| 69 | +# context=Depends(get_context), |
| 70 | +# ): |
| 71 | +# # print(request) |
| 72 | +# return { |
| 73 | +# "info": "Online auctions API. See /docs for documentation " |
| 74 | +# + context.correlation_id |
| 75 | +# } |
0 commit comments