generated from bunsamosa/fastapi_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (47 loc) · 1.6 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
import json
import structlog
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.base import BaseHTTPMiddleware
from app.import_routes import import_routes
from app.middlewares import create_context
from lib.core.cache_store import CacheStore
from lib.core.data_store import get_connection_pool
from lib.core.logger import initialize_logger
# Create fastAPI app
app = FastAPI()
# Add middlewares
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# add context middleware
app.add_middleware(BaseHTTPMiddleware, dispatch=create_context)
###############################################################################
# Rest server startup hooks
###############################################################################
@app.on_event("startup")
async def startup_event() -> None:
"""
Initialize modules and attach them to app
"""
# cachestore
app.cache_store = CacheStore(namespace="rest_server")
app.data_store = await get_connection_pool()
# TODO delete legacy from here
app.secret_store = CacheStore(namespace="secrets")
address_key = "user_address"
app.address_mapping = app.cache_store.get_dictionary(address_key)
# load player names in memory
with open("lib/utils/final_names.json", encoding="UTF-8") as f:
app.player_names = json.load(f)
# TODO delete till here
# logger
initialize_logger()
app.logger = structlog.get_logger("rest_server")
# routers
import_routes(app)