forked from loop-godong/loopchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_proxy.py
106 lines (80 loc) · 2.78 KB
/
rest_proxy.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import logging
import multiprocessing
import getopt
import gunicorn.app.base
from gunicorn.six import iteritems
import loopchain.utils as util
from loopchain import configure as conf
from loopchain.rest_server.rest_server import ServerComponents
def number_of_workers():
return (multiprocessing.cpu_count() * 2) + 1
class StandaloneApplication(gunicorn.app.base.BaseApplication):
"""Web server runner by gunicorn.
"""
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super(StandaloneApplication, self).__init__()
def load_config(self):
config = dict([(key, value) for key, value in iteritems(self.options)
if key in self.cfg.settings and value is not None])
for key, value in iteritems(config):
self.cfg.set(key.lower(), value)
def load(self):
return self.application
def usage():
print("USAGE: RESTful proxy")
print("python3 rest_proxy.py [option] [value] ...")
print("-------------------------------")
print("option list")
print("-------------------------------")
print("-h or --help : print this usage")
print("-p or --port : port of Peer Service itself")
print("-d : Display colored log.")
def main(argv):
util.logger.spam("RESTful proxy main got argv(list): " + str(argv))
try:
opts, args = getopt.getopt(argv, "dhp:",
["help",
"port=",
])
except getopt.GetoptError as e:
logging.error(e)
usage()
sys.exit(1)
# Port and host values.
port = conf.PORT_PEER
host = '0.0.0.0'
# Parse command line arguments.
for opt, arg in opts:
if opt == "-p":
port = int(arg)
elif opt == "-d":
util.set_log_level_debug()
elif opt == "-h":
usage()
return
# Connect gRPC stub.
ServerComponents().set_stub_port(port, conf.IP_LOCAL)
ServerComponents().set_argument()
ServerComponents().set_resource()
api_port = port + conf.PORT_DIFF_REST_SERVICE_CONTAINER
logging.debug("Run gunicorn webserver for HA. Port = %s", str(api_port))
certfile = ""
keyfile = ""
if ServerComponents().ssl_context is not None:
certfile = ServerComponents().ssl_context(0)
keyfile = ServerComponents().ssl_context(1)
options = {
'bind': '%s:%s' % (host, api_port),
'workers': number_of_workers(),
'certfile': certfile,
'keyfile': keyfile
}
StandaloneApplication(ServerComponents().app, options).run()
# Run as gunicorn web server.
if __name__ == "__main__":
main(sys.argv[1:])