This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.py
171 lines (130 loc) · 4.2 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import json
import logging
import tornado.ioloop
import tornado.web
from tornado.options import options, define
from cent.core import generate_token, generate_channel_sign
logging.getLogger().setLevel(logging.DEBUG)
define(
"port", default=3000, help="app port", type=int
)
define(
"centrifuge", default='localhost:8000',
help="centrifuge address without url scheme", type=str
)
define(
"project_key", default='', help="project key", type=str
)
define(
"project_secret", default='', help="project secret key", type=str
)
# let it be your application's user ID
USER_ID = '2694'
INFO = json.dumps({
'first_name': 'Alexandr',
'last_name': 'Emelin'
})
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
def get_auth_data():
user = USER_ID
now = str(int(time.time()))
token = generate_token(options.project_secret, options.project_key, user, now, info=INFO)
auth_data = {
'token': token,
'user': user,
'project': options.project_key,
'timestamp': now,
'info': INFO
}
return auth_data
class SockjsHandler(tornado.web.RequestHandler):
def get(self):
"""
Render template with data required to connect to Centrifuge using SockJS.
"""
self.render(
"index_sockjs.html",
auth_data=get_auth_data(),
centrifuge_address=options.centrifuge
)
class WebsocketHandler(tornado.web.RequestHandler):
def get(self):
"""
Render template with data required to connect to Centrifuge using Websockets.
"""
self.render(
"index_websocket.html",
auth_data=get_auth_data(),
centrifuge_address=options.centrifuge
)
class CentrifugeAuthHandler(tornado.web.RequestHandler):
"""
Allow all users to subscribe on channels they want.
"""
def check_xsrf_cookie(self):
pass
def post(self):
client_id = self.get_argument("client")
channels = self.get_arguments("channels")
logging.info("{0} wants to subscribe on {1}".format(client_id, ", ".join(channels)))
to_return = {}
for channel in channels:
info = json.dumps({
'channel_extra_info_example': 'you can add additional JSON data when authorizing'
})
to_return[channel] = {
"sign": generate_channel_sign(options.project_secret, client_id, channel, info=info),
"info": info
}
# but here we allow to join any private channel and return additional
# JSON info specific for channel
self.set_header('Content-Type', 'application/json; charset="utf-8"')
self.write(json.dumps(to_return))
class CentrifugeRefreshHandler(tornado.web.RequestHandler):
"""
Allow all users to subscribe on channels they want.
"""
def check_xsrf_cookie(self):
pass
def post(self):
#raise tornado.web.HTTPError(403)
logging.info("client wants to refresh its connection parameters")
user = USER_ID
now = str(int(time.time()))
token = generate_token(options.project_secret, options.project_key, user, now, info=INFO)
to_return = {
'token': token,
'user': user,
'project': options.project_key,
'timestamp': now,
'info': INFO
}
self.set_header('Content-Type', 'application/json; charset="utf-8"')
self.write(json.dumps(to_return))
def run():
options.parse_command_line()
app = tornado.web.Application(
[
(r'/', IndexHandler),
(r'/sockjs', SockjsHandler),
(r'/ws', WebsocketHandler),
(r'/centrifuge/auth', CentrifugeAuthHandler),
(r'/centrifuge/refresh', CentrifugeRefreshHandler)
],
debug=True
)
app.listen(options.port)
logging.info("app started, visit http://localhost:%s" % options.port)
tornado.ioloop.IOLoop.instance().start()
def main():
try:
run()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()