-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (34 loc) · 1.09 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
"""
aapi.main
~~~~~~~~~
Entry-point for aapi.
"""
import asyncio
import uvloop
import aiohttp
import loggings
from aiohttp import web
from basemodel import close_db, init_db
from middlewares import error_handle_middleware
from routes import setup_routes
from configs import PORT
# Get logger
logger = loggings.logger
# Install the uvloop event loop policy.
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
async def init_app(app: web.Application):
"""Initiate web application settings after app is instantiated."""
logger.debug('Init application.')
setup_routes(app)
await init_db(app)
app['http_session'] = aiohttp.ClientSession()
async def close_app(app: web.Application):
"""Close web application gracefully."""
await close_db(app)
await app['http_session'].close()
APP = web.Application(middlewares=[error_handle_middleware], debug=False, logger=None)
# Setup on_startup and on_cleanup.
APP.on_startup.append(init_app)
APP.on_cleanup.append(close_app)
host, port = '0.0.0.0', PORT
web.run_app(APP, host=host, port=port, access_log=None, print=logger.debug)