-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
47 lines (36 loc) · 924 Bytes
/
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
from fastapi import FastAPI, Request, Response
from starlette.middleware.cors import CORSMiddleware
from book import app as app_book
from db import session
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=[
# TODO CORS URLS
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get('/')
async def index():
return {
'msg': ':bird: 33-4 :tiger:'
}
app.include_router(
app_book.api,
prefix='/book',
tags=['book']
)
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
response = Response("Internal server error", status_code=500)
try:
request.state.db = session()
response = await call_next(request)
finally:
request.state.db.close()
return response
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8080)