-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·98 lines (74 loc) · 2.83 KB
/
app.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
import os
from werkzeug.middleware.proxy_fix import ProxyFix
from flask import Flask, render_template
from flask import request, redirect
from flask import url_for
from flask_socketio import SocketIO, join_room, leave_room, emit
from werkzeug.routing import Rule
import eventlet
from mirror.store import mirror_exists, generate_valid_code, create_or_update_mirror, push_request, get_all_requests
from mirror.utils import parse_request
eventlet.monkey_patch(socket=True)
REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.wsgi_app = ProxyFix(app.wsgi_app)
socketio = SocketIO(app, message_queue=REDIS_URL)
# Regular routing
# Catch-all methods rule
app.url_map.add(Rule('/r/<code>', endpoint='process_request'))
app.url_map.add(Rule('/r/<code>/<path:path>', endpoint='process_request'))
@app.route('/', methods=['GET', 'POST'])
def index():
error = None
code = ''
if request.method == 'POST':
if 'code' in request.form and len(request.form['code'].strip()) > 0:
code = request.form['code']
if not mirror_exists(code):
return create_or_update_and_visit(code)
else:
error = 'Mirror already exists! Go <a href="{}">check it out.</a>'.format(url_for('view', code=code))
else:
return create_or_update_and_visit(generate_valid_code())
return render_template('index.html', error=error, code=code)
@app.route('/v/<code>', methods=['GET'])
def view(code):
if not mirror_exists(code):
return render_template('404.html')
create_or_update_mirror(code)
return render_template('view.html', code=code, url_root=request.url_root)
@app.endpoint('process_request')
def process_request(code, path=None):
if not mirror_exists(code):
return 'The mirror you tried to access, does not exist.', 404
else:
data = parse_request(request)
push_request(code, data)
app.logger.info("Received request: {}".format(data))
socketio.emit('request', data, room=code)
return 'OK'
def create_or_update_and_visit(code):
create_or_update_mirror(code)
return redirect(url_for('view', code=code))
# Socket routes
@socketio.on('join')
def on_join(room):
app.logger.info("Client connecting to room '{}'".format(room))
join_room(room)
requests = get_all_requests(room)
for r in reversed(requests):
emit('request', r)
@socketio.on('leave')
def on_leave(room):
app.logger.info("Client leaving room '{}'".format(room))
leave_room(room)
@socketio.on('connect')
def connect():
app.logger.info("Somebody connected! event: {}".format(request.event))
if __name__ == '__main__':
if app.config['ENV'] == 'production':
host = '0.0.0.0'
else:
host = '127.0.0.1'
socketio.run(app, host=host)