-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
57 lines (47 loc) · 1.25 KB
/
main.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
'''
FastAPI Demo
Main application
'''
# Standard Imports
# PyPi Imports
from fastapi import Depends, FastAPI
from starlette.requests import Request
from starlette.responses import Response
# Local Imports
import database.models as models
from database.setup import (
session_local,
engine
)
from routers import (
token,
users
)
###############################################################################
models.Base.metadata.create_all(bind=engine)
app = FastAPI( #pylint: disable=invalid-name
title="FastAPI Demo",
description="This is a demo application for FastAPI",
version="0.0.1"
)
###############################################################################
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
'''Function to populate requests with a database session'''
response = Response("Internal server error", status_code=500)
try:
request.state.db = session_local()
response = await call_next(request)
finally:
request.state.db.close()
return response
###############################################################################
app.include_router(
token.router,
tags=["token"]
)
app.include_router(
users.router,
dependencies=[Depends(token.check_current_user)],
tags=["users"]
)