-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·75 lines (68 loc) · 1.88 KB
/
run.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
#!/usr/bin/env python3
import argparse
import pathlib
import logging.config
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.wsgi import WSGIContainer
from api.config import Environment, load_configuration
from api.wsgi import create_wsgi
__all__ = 'main',
def main():
options = [
(
('-e', '--environment'),
{
'type': Environment,
'help': 'An environment.',
'required': True
},
),
(
('-p', '--port'),
{
'type': int,
'help': 'Server port.',
'default': 8000
},
),
(
('-d', '--debug'),
{
'help': 'debug mode.',
'action': 'store_true',
},
),
(
('-H', '--host'),
{
'type': str,
'help': 'A host address.',
'default': '0.0.0.0',
},
),
]
parser = argparse.ArgumentParser(prog='API server runner')
project_root = pathlib.Path(__file__).parent.resolve()
for opt, kopt in options:
parser.add_argument(
*opt,
**kopt
)
options = parser.parse_args()
config = load_configuration(project_root / 'conf', options.environment)
logging.config.dictConfig(config['logging'])
wsgi = create_wsgi(config)
if options.environment == Environment.dev:
wsgi.run(debug=options.debug, port=options.port)
else:
container = WSGIContainer(wsgi_app)
http_server = HTTPServer(container)
http_server.bind(options.port, options.host)
try:
http_server.start(0)
IOLoop.current().start()
except KeyboardInterrupt:
raise SystemExit
if __name__ == '__main__':
main()