-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
70 lines (57 loc) · 1.86 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
58
59
60
61
62
63
64
65
66
67
68
69
70
from typing import List
from databases import Database
import fastapi
from device.device import Device
import asyncio
from fastapi.middleware.cors import CORSMiddleware
from device.device_router import DeviceRouter
from device.db_connection import database
from device.models import DeviceModel
import zk
from zk.exception import ZKConnectionLostError, ZKError, ZKNetworkError
app = fastapi.FastAPI()
app.state.database = database
origins = [
"*",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
devices: List[Device]= []
dr = DeviceRouter(devices)
@app.on_event("startup")
async def startup() -> None:
global dr
database_:Database = app.state.database
if not database_.is_connected:
await database_.connect()
for device in await DeviceModel.objects.all():
d = Device(**device.dict())
devices.append(d)
dr = DeviceRouter(devices)
@app.on_event("shutdown")
async def shutdown() -> None:
database_:Database = app.state.database
if database_.is_connected:
await database_.disconnect()
@app.exception_handler(ZKError)
async def unicorn_exception_handler(request: fastapi.Request, exc: ZKError):
return fastapi.responses.JSONResponse(
status_code=418,
content={"detail": f"Oops! We failed to communicate with the device."},
)
@app.middleware("http")
async def timeout_middleware(request: fastapi.Request, call_next):
REQUEST_TIMEOUT_ERROR = 60
try:
return await asyncio.wait_for(call_next(request), timeout=REQUEST_TIMEOUT_ERROR)
except asyncio.TimeoutError:
return fastapi.responses.JSONResponse({
'detail': 'Request processing time excedeed limit'
},
status_code=fastapi.status.HTTP_504_GATEWAY_TIMEOUT)
app.include_router(dr.getRouter())