Skip to content

Commit b57513a

Browse files
author
zeekay
committed
Refactor code layout.
1 parent ca4ea2c commit b57513a

File tree

2 files changed

+116
-103
lines changed

2 files changed

+116
-103
lines changed

flask_uwsgi_websocket/__init__.py

Lines changed: 3 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -12,111 +12,11 @@
1212
import os
1313
import sys
1414
import uuid
15-
from ._uwsgi import uwsgi
16-
17-
18-
class WebSocketClient(object):
19-
'''
20-
Default WebSocket client has a blocking recieve method, but still exports rest of uWSGI API.
21-
'''
22-
def __init__(self, fd, timeout=60):
23-
self.fd = fd
24-
self.timeout = timeout
25-
self.id = str(uuid.uuid1())
26-
27-
def receive(self):
28-
return uwsgi.websocket_recv()
29-
30-
def recv(self):
31-
return uwsgi.websocket_recv()
32-
33-
def recv_nb(self):
34-
return uwsgi.websocket_recv_nb()
35-
36-
def send(self, msg):
37-
return uwsgi.websocket_send(msg)
38-
39-
def send_binary(self, msg):
40-
return uwsgi.websocket_send_binary(msg)
41-
42-
def send_from_sharedarea(self, id, pos):
43-
return uwsgi.websocket_send_from_sharedarea(id, pos)
44-
45-
def send_binary_from_sharedarea(self, id, pos):
46-
return uwsgi.websocket_send_binary_from_sharedarea(id, pos)
47-
48-
49-
class WebSocketMiddleware(object):
50-
client = WebSocketClient
51-
52-
def __init__(self, wsgi_app, websocket):
53-
self.wsgi_app = wsgi_app
54-
self.websocket = websocket
55-
56-
def __call__(self, environ, start_response):
57-
handler = self.websocket.routes.get(environ['PATH_INFO'])
58-
59-
if handler:
60-
uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'], environ.get('HTTP_ORIGIN', ''))
61-
handler(self.client(uwsgi.connection_fd(), self.websocket.timeout))
62-
else:
63-
return self.wsgi_app(environ, start_response)
64-
65-
66-
class WebSocket(object):
67-
middleware = WebSocketMiddleware
68-
69-
def __init__(self, app=None, timeout=60):
70-
if app:
71-
self.init_app(app)
72-
self.timeout = timeout
73-
self.routes = {}
74-
75-
def run(self, app=None, debug=False, host='localhost', port=5000, **kwargs):
76-
if not app:
77-
app = self.app.name + ':app'
78-
79-
# kwargs are treated as uwsgi arguments
80-
if kwargs.get('master') is None:
81-
kwargs['master'] = True
82-
83-
# boolean should be treated as empty value
84-
for k,v in kwargs.items():
85-
if v is True:
86-
kwargs[k] = ''
87-
88-
# constructing uwsgi arguments
89-
uwsgi_args = ' '.join(['--{0} {1}'.format(k,v) for k,v in kwargs.items()])
90-
args = 'uwsgi --http {0}:{1} --http-websockets {2} --wsgi {3}'.format(host, port, uwsgi_args, app)
91-
92-
print args
93-
94-
# set enviromental variable to trigger adding debug middleware
95-
if self.app.debug or debug:
96-
args = 'FLASK_UWSGI_DEBUG=true {0}'.format(args)
97-
98-
# run uwsgi with our args
99-
sys.exit(os.system(args))
100-
101-
def init_app(self, app):
102-
self.app = app
103-
104-
if os.environ.get('FLASK_UWSGI_DEBUG'):
105-
from werkzeug.debug import DebuggedApplication
106-
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
107-
app.debug = True
108-
109-
app.wsgi_app = self.middleware(app.wsgi_app, self)
110-
app.run = lambda **kwargs: self.run(**kwargs)
111-
112-
def route(self, rule):
113-
def decorator(f):
114-
self.routes[rule] = f
115-
return f
116-
return decorator
117-
11815

16+
from ._uwsgi import uwsgi
17+
from .websocket import *
11918
from .async import *
19+
12020
try:
12121
from ._gevent import *
12222
except ImportError:

flask_uwsgi_websocket/websocket.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import os
2+
import sys
3+
import uuid
4+
from ._uwsgi import uwsgi
5+
6+
7+
class WebSocketClient(object):
8+
'''
9+
Default WebSocket client has a blocking recieve method, but still exports
10+
rest of uWSGI API.
11+
'''
12+
def __init__(self, fd, timeout=60):
13+
self.fd = fd
14+
self.timeout = timeout
15+
self.id = str(uuid.uuid1())
16+
17+
def receive(self):
18+
return uwsgi.websocket_recv()
19+
20+
def recv(self):
21+
return uwsgi.websocket_recv()
22+
23+
def recv_nb(self):
24+
return uwsgi.websocket_recv_nb()
25+
26+
def send(self, msg):
27+
return uwsgi.websocket_send(msg)
28+
29+
def send_binary(self, msg):
30+
return uwsgi.websocket_send_binary(msg)
31+
32+
def send_from_sharedarea(self, id, pos):
33+
return uwsgi.websocket_send_from_sharedarea(id, pos)
34+
35+
def send_binary_from_sharedarea(self, id, pos):
36+
return uwsgi.websocket_send_binary_from_sharedarea(id, pos)
37+
38+
39+
class WebSocketMiddleware(object):
40+
'''
41+
WebSocket Middleware that handles handshake and passes route a WebSocketClient.
42+
'''
43+
client = WebSocketClient
44+
45+
def __init__(self, wsgi_app, websocket):
46+
self.wsgi_app = wsgi_app
47+
self.websocket = websocket
48+
49+
def __call__(self, environ, start_response):
50+
handler = self.websocket.routes.get(environ['PATH_INFO'])
51+
52+
if handler:
53+
uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'], environ.get('HTTP_ORIGIN', ''))
54+
handler(self.client(uwsgi.connection_fd(), self.websocket.timeout))
55+
else:
56+
return self.wsgi_app(environ, start_response)
57+
58+
59+
class WebSocket(object):
60+
'''
61+
Flask extension which makes it easy to integrate uWSGI-powered WebSockets
62+
into your applications.
63+
'''
64+
middleware = WebSocketMiddleware
65+
66+
def __init__(self, app=None, timeout=60):
67+
if app:
68+
self.init_app(app)
69+
self.timeout = timeout
70+
self.routes = {}
71+
72+
def run(self, app=None, debug=False, host='localhost', port=5000, **kwargs):
73+
if not app:
74+
app = self.app.name + ':app'
75+
76+
# kwargs are treated as uwsgi arguments
77+
if kwargs.get('master') is None:
78+
kwargs['master'] = True
79+
80+
# boolean should be treated as empty value
81+
for k,v in kwargs.items():
82+
if v is True:
83+
kwargs[k] = ''
84+
85+
# constructing uwsgi arguments
86+
uwsgi_args = ' '.join(['--{0} {1}'.format(k,v) for k,v in kwargs.items()])
87+
args = 'uwsgi --http {0}:{1} --http-websockets {2} --wsgi {3}'.format(host, port, uwsgi_args, app)
88+
89+
print args
90+
91+
# set enviromental variable to trigger adding debug middleware
92+
if self.app.debug or debug:
93+
args = 'FLASK_UWSGI_DEBUG=true {0}'.format(args)
94+
95+
# run uwsgi with our args
96+
sys.exit(os.system(args))
97+
98+
def init_app(self, app):
99+
self.app = app
100+
101+
if os.environ.get('FLASK_UWSGI_DEBUG'):
102+
from werkzeug.debug import DebuggedApplication
103+
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
104+
app.debug = True
105+
106+
app.wsgi_app = self.middleware(app.wsgi_app, self)
107+
app.run = lambda **kwargs: self.run(**kwargs)
108+
109+
def route(self, rule):
110+
def decorator(f):
111+
self.routes[rule] = f
112+
return f
113+
return decorator

0 commit comments

Comments
 (0)