-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
46 lines (35 loc) · 1.1 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
"""
REST app
"""
import argparse
from aiohttp import web
def setup_config(app):
from pgfire.conf import config
app['config'] = config
async def setup_storage(app):
from pgfire.engine.storage.postgres import PostgresJsonStorage
dbconfig = app['config']['db']
app['storage'] = PostgresJsonStorage(dbconfig)
async def close_storage(app):
app['storage'].close()
def setup_routes(app):
from pgfire.rest.routes import routes
for route in routes:
path = route[0]
handler = route[1]
method = route[2]
app.router.add_route(method, path, handler)
def prepare_app():
_app = web.Application()
setup_config(_app)
_app.on_startup.append(setup_storage)
_app.on_cleanup.append(close_storage)
setup_routes(_app)
return _app
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="aiohttp server example")
parser.add_argument('--host', default='localhost')
parser.add_argument('--port', default=8666)
args = parser.parse_args()
_app = prepare_app()
web.run_app(_app, host=args.host, port=args.port)