-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (61 loc) · 1.74 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from fastapi import FastAPI, Depends
import uvicorn
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import ORJSONResponse
from fastapi import WebSocket
from tortoise.contrib.fastapi import register_tortoise
import os
from typing import Annotated
import common.config as config
from routers import router
from routers.ws import websocket_endpoint
app = FastAPI(
default_response_class=ORJSONResponse,
root_path=config.ROOT_PATH,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
app.include_router(router, prefix="/v1")
db_config: dict = {
'connections': {
# Dict format for connection
'default': {
'engine': 'tortoise.backends.asyncpg',
'credentials': {
'host': os.environ["DB_HOST"],
'port': os.environ["DB_PORT"],
'user': os.environ["DB_USER"],
'password': os.environ["DB_PASSWORD"],
'database': os.environ["DB_DATABASE"],
},
},
},
'apps': {
'models': {
'models': ["aerich.models", "routers"],
# If no default_connection specified, defaults to 'default'
'default_connection': 'default',
}
}
}
register_tortoise(
app=app,
config=db_config,
generate_schemas=False
)
from routers.accounts import Token, require_token_ws
from fastapi import Request
@app.websocket("/ws")
async def ws_endpoint(
websocket: WebSocket,
#token: Annotated[Token, Depends(require_token_ws)],
):
response = await websocket_endpoint(websocket)
return response
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)