· 7 years ago · Aug 15, 2018, 09:30 AM
1print('\n !!! RUN !!!')
2
3from aiohttp import web
4import aiohttp_jinja2
5import jinja2
6import cryptography
7import asyncio
8import aioredis
9import base64
10from cryptography import fernet
11from aiohttp_session import setup, get_session
12from aiohttp_session.cookie_storage import EncryptedCookieStorage
13from conf import (
14 db,
15 routes,
16 redis_wrapper,
17 HOST,
18 PORT,
19 TEMPLATES_PATH,
20 DB_NAME,
21 DB_USER,
22 DB_PASS,
23 DB_HOST,
24 DB_PORT,
25 REDIS_HOST,
26 REDIS_PORT
27)
28import views
29from operators import PersonOperator
30from asyncpg.exceptions import UniqueViolationError
31from utils import make_hash
32from shunt_server.db_init import init_db
33
34
35async def gracefull_shutdown(app):
36 # !!! ATTENTION !!! remove all temprary data
37 # while there is nothing to store for a long time
38 await redis_wrapper.redis.flushall()
39 await redis_wrapper.redis.quit()
40 await db.gino.drop_all()
41
42if __name__ == '__main__':
43 loop = asyncio.get_event_loop()
44 loop.run_until_complete(init_db(DB_USER, DB_NAME, DB_HOST, DB_PASS, DB_PORT))
45 redis = loop.run_until_complete(aioredis.create_redis_pool((REDIS_HOST,
46 REDIS_PORT),
47 maxsize=1000))
48 redis_wrapper.set_cli(redis)
49 app = web.Application()
50 app.router.add_static('/static', 'browser_client')
51
52 # secret_key must be 32 url-safe base64-encoded bytes
53 fernet_key = fernet.Fernet.generate_key()
54 secret_key = base64.urlsafe_b64decode(fernet_key)
55 setup(app, EncryptedCookieStorage(secret_key))
56
57 app.add_routes(routes)
58 app.on_shutdown.append(gracefull_shutdown)
59 aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(TEMPLATES_PATH))
60 web.run_app(app, host=HOST, port=PORT)