-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (62 loc) · 2.44 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
71
72
73
74
75
76
77
78
import importlib
import logging
import pathlib
from contextlib import asynccontextmanager
from datetime import timedelta
from anyio import create_task_group
from fastapi import APIRouter, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette_compress import CompressMiddleware
from json_response import JSONResponseUTF8
from middlewares.cache_control_middleware import CacheControlMiddleware
from middlewares.cache_response_middleware import CacheResponseMiddleware
from middlewares.profiler_middleware import ProfilerMiddleware
from middlewares.version_middleware import VersionMiddleware
from services.aed_service import AEDService
from services.country_service import CountryService
from services.worker_service import WorkerService
@asynccontextmanager
async def lifespan(_):
worker_state = await WorkerService.init()
if worker_state.is_primary:
async with create_task_group() as tg:
await tg.start(CountryService.update_db_task)
await tg.start(AEDService.update_db_task)
await worker_state.set_state('running')
yield
# on shutdown, always abort the tasks
tg.cancel_scope.cancel()
else:
await worker_state.wait_for_state('running')
yield
app = FastAPI(lifespan=lifespan, default_response_class=JSONResponseUTF8)
app.add_middleware(CacheControlMiddleware)
app.add_middleware(CacheResponseMiddleware)
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_headers=['baggage', 'sentry-trace'],
allow_methods=['GET'],
max_age=int(timedelta(days=1).total_seconds()),
)
app.add_middleware(VersionMiddleware)
app.add_middleware(CompressMiddleware)
app.add_middleware(ProfilerMiddleware)
def _make_router(path: str, prefix: str) -> APIRouter:
"""
Create a router from all modules in the given path.
"""
router = APIRouter(prefix=prefix)
counter = 0
for p in pathlib.Path(path).glob('*.py'):
module_name = p.with_suffix('').as_posix().replace('/', '.')
module = importlib.import_module(module_name)
router_attr = getattr(module, 'router', None)
if router_attr is not None:
router.include_router(router_attr)
counter += 1
else:
logging.warning('Missing router in %s', module_name)
logging.info('Loaded %d routers from %s as %r', counter, path, prefix)
return router
app.include_router(_make_router('api/v1', '/api/v1'))