-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.py
executable file
·70 lines (58 loc) · 2 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
#! /usr/bin/env python
import asyncio
import aiohttp_jinja2
import aiohttp_debugtoolbar
import jinja2
from aiohttp_session import session_middleware
from aiohttp_session.cookie_storage import EncryptedCookieStorage
from aiohttp import web
from routes import routes
from middlewares import db_handler, authorize
from motor import motor_asyncio as ma
from settings import *
async def on_shutdown(app):
for ws in app['websockets']:
await ws.close(code=1001, message='Server shutdown')
async def shutdown(server, app, handler):
server.close()
await server.wait_closed()
app.client.close() # database connection close
await app.shutdown()
await handler.finish_connections(10.0)
await app.cleanup()
async def init(loop):
app = web.Application(loop=loop, middlewares=[
session_middleware(EncryptedCookieStorage(SECRET_KEY)),
authorize,
db_handler,
# aiohttp_debugtoolbar.middleware,
])
app['websockets'] = []
handler = app.make_handler()
if DEBUG:
aiohttp_debugtoolbar.setup(app)
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates'))
# route part
for route in routes:
app.router.add_route(route[0], route[1], route[2], name=route[3])
app.router.add_static('/static', 'static', name='static')
# end route part
# db connect
app.client = ma.AsyncIOMotorClient(MONGO_HOST)
app.db = app.client[MONGO_DB_NAME]
# end db connect
app.on_shutdown.append(on_shutdown)
serv_generator = loop.create_server(handler, SITE_HOST, SITE_PORT)
return serv_generator, handler, app
loop = asyncio.get_event_loop()
serv_generator, handler, app = loop.run_until_complete(init(loop))
serv = loop.run_until_complete(serv_generator)
log.debug('start server %s' % str(serv.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
log.debug(' Stop server begin')
finally:
loop.run_until_complete(shutdown(serv, app, handler))
loop.close()
log.debug('Stop server end')