-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (54 loc) · 1.73 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
#!/bin/env python
"""
Main file to run application.
It start server on ip: 127.0.0.1:8080
"""
import os
import sys
import tornado.httpserver
import tornado.ioloop
import tornado.options
import app_server as app
# Return the libs path (this path is appended to module search path/sys.path).
def get_libs_path():
"""
Get path to libraries depends it's started on localhost or OpenShift
"""
zpath = os.getenv("OPENSHIFT_REPO_DIR")
zpath = zpath if zpath else "./"
return os.path.abspath(os.path.join(zpath, "libs"))
# Return the interface we need to bind to.
def get_bind_interface():
"""
Get ip address depends it's started on localhost or OpenShift
"""
ipaddr = os.getenv("OPENSHIFT_PYTHON_IP")
return ipaddr if ipaddr else "127.0.0.1"
def get_bind_interface_port():
"""
Get port number depends it's started on localhost or OpenShift
"""
port = os.getenv("OPENSHIFT_PYTHON_PORT")
try:
port = int(port)
except TypeError:
port = 8080
return port
# Server main.
def start_tornado():
"""
Start Tornado web server
"""
# Tornado server address and port number.
tornado.options.define("address", default=get_bind_interface(),
help="network address/interface to bind to")
tornado.options.define("port", default=get_bind_interface_port(),
help="port number to bind to", type=int)
tornado.options.parse_command_line()
zoptions = tornado.options.options
zserver = tornado.httpserver.HTTPServer(app.Application())
zserver.listen(zoptions.port, zoptions.address)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
sys.path.append(get_libs_path())
start_tornado()