-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgateway.py
executable file
·96 lines (75 loc) · 2.93 KB
/
gateway.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
#!/usr/bin/env python
import tornado.options
import tornado.web
import tornado.websocket
import obelisk
import threading
import code
import config
# Install Tornado reactor loop into Twister
# http://www.tornadoweb.org/en/stable/twisted.html
from tornado.platform.twisted import TwistedIOLoop
from twisted.internet import reactor
TwistedIOLoop().install()
from crypto2crypto import CryptoTransportLayer
from tornado.options import define, options, parse_command_line
parse_command_line()
import rest_handlers
import obelisk_handler
import querysocket_handler
import jsonchan
import broadcast
import ticker
define("port", default=8888, help="run on the given port", type=int)
global ioloop
ioloop = tornado.ioloop.IOLoop.instance()
class GatewayApplication(tornado.web.Application):
def __init__(self, service):
settings = dict(debug=True)
settings.update(options.as_dict())
client = obelisk.ObeliskOfLightClient(service)
self.client = client
self.obelisk_handler = obelisk_handler.ObeliskHandler(client)
self.brc_handler = broadcast.BroadcastHandler()
self.p2p = CryptoTransportLayer(config.get('p2p-port', 8889), config.get('external-ip', '127.0.0.1'))
self.p2p.join_network(config.get('seeds', []))
self.json_chan_handler = jsonchan.JsonChanHandler(self.p2p)
self.ticker_handler = ticker.TickerHandler()
#websocket uri space
handlers = [
## WebSocket Handler
(r"/", querysocket_handler.QuerySocketHandler)
]
# helloobelisk uri space
uri_space= r"/rest/v1/"
other_handlers = [
(uri_space + r'net(?:/)?', rest_handlers.NetHandler),
(uri_space + r'height(?:/)?', rest_handlers.HeightHandler),
(uri_space + r'address/([^/]*)(?:/)?', rest_handlers.AddressHistoryHandler),
(uri_space + r'tx/([^/]*)(?:/)?', rest_handlers.TransactionHandler),
(uri_space + r'block/([^/]*)(?:/)?', rest_handlers.BlockHeaderHandler),
(uri_space + r"block/([^/]*)/transactions(?:/)?", rest_handlers.BlockTransactionsHandler),
]
all_handlers = other_handlers + handlers
tornado.web.Application.__init__(self, all_handlers, **settings)
class DebugConsole(threading.Thread):
daemon = True
def __init__(self, application):
self.application = application
super(DebugConsole, self).__init__()
self.start()
def run(self):
console = code.InteractiveConsole()
code.interact(local=dict(globals(), **locals()))
def main(service):
application = GatewayApplication(service)
tornado.autoreload.start(ioloop)
application.listen(config.get('websocket-port', 8888))
#debug_console = DebugConsole(application)
reactor.run()
if __name__ == "__main__":
service = config.get("obelisk-url", "tcp://127.0.0.1:9091")
try:
main(service)
except KeyboardInterrupt:
reactor.stop()