This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
forked from graft-project/GraftSuperNode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
49 lines (37 loc) · 1.36 KB
/
main.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
#!/usr/bin/env python
# import signal
from supernode_protocol import SupernodeProtocol
from logger import service_logger as logger
import tornado.web
import tornado.websocket
import tornado.httpserver
import tornado.ioloop
import tornado.gen
import json
settings = {
'port': 8080
}
class APIHandler(tornado.web.RequestHandler):
def post(self, *args, **kwargs):
request_body = json.loads(self.request.body)
SupernodeProtocol.instance().register_supernode(self.request.headers['Host'])
logger.info('Received message: {}'.format(request_body))
result = SupernodeProtocol.instance().process(**request_body)
logger.info('Send message: {}'.format(result))
self.write(json.dumps(result))
class HttpApplication(tornado.web.Application):
def __init__(self):
handlers = [
(r'/api', APIHandler)
]
tornado.web.Application.__init__(self, handlers)
def run_tornado():
http_app = HttpApplication()
http_server = tornado.httpserver.HTTPServer(http_app)
logger.info('Running REST tornado server on port %s ...' % settings['port'])
http_server.listen(settings['port'])
# signal.signal(signal.SIGUSR1, ConnectionTimeoutHandler.instance().log_open_connections)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
# Run tornado application
run_tornado()