-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
120 lines (83 loc) · 2.71 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from functools import wraps
import logging
import mimetypes
import os
import secrets
from flask import Flask, redirect, render_template, request, session
from flask_login import LoginManager, current_user
from flask_socketio import SocketIO, disconnect, emit, join_room, leave_room
from werkzeug.middleware.proxy_fix import ProxyFix
from config import *
from models import User
from oauth import oauth, user
def authenticated_only(f):
@wraps(f)
def wrapped(*args, **kwargs):
if not current_user.is_authenticated:
disconnect()
else:
return f(*args, **kwargs)
return wrapped
def get_locale():
if "locale" not in session:
session["locale"] = request.accept_languages.best_match({"en", "ja"})
return session["locale"]
# Windows registry can get this messed up, so overriding here
mimetypes.add_type("application/javascript", ".js")
mimetypes.add_type("text/css", ".css")
port = int(os.environ.get("PORT", 5000))
# construct app
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_host=1)
app.config["SECRET_KEY"] = secrets.token_urlsafe(16)
app.register_blueprint(user)
app.jinja_env.globals["get_locale"] = get_locale
oauth.init_app(app)
login_manager = LoginManager()
login_manager.login_view = "login"
login_manager.init_app(app)
socket = SocketIO()
socket.init_app(app)
@login_manager.user_loader
def load_user(id):
return User(id)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/en", methods=["GET", "POST"])
def lang_en():
session["locale"] = "en"
return redirect("/")
@app.route("/jp", methods=["GET", "POST"])
def lang_jp():
session["locale"] = "ja"
return redirect("/")
@socket.on("connect")
def on_connect():
logging.info(f"Client {request.sid} has connected.")
@socket.on("disconnect")
def on_disconnect():
logging.info(f"Client {request.sid} has disconnected.")
@socket.on("join")
def on_join(data):
room = data["room"]
logging.info(f"Client {request.sid} has joined room: '{room}'.")
join_room(room)
@socket.on("leave")
def on_leave(data):
room = data["room"]
logging.info(f"Client {request.sid} left room: '{room}'.")
leave_room(room)
@socket.on("plot")
def handle_plot_requests(data):
if current_user.is_authenticated:
# forward message from guests to plotter
logging.info(f"Client {request.sid} has requested a plot.")
emit("plot", data, to="plotter")
@socket.on("notify")
def handle_notifications(data):
# forward message from plotter to guests
logging.info(f"Plotter {request.sid} has sent a status update.")
emit("notify", data, to="guests")
if __name__ == "__main__":
socket.run(app, host="0.0.0.0", port=port)