forked from one-love/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
executable file
·64 lines (47 loc) · 1.32 KB
/
backend.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
#!/usr/bin/env python
import os
from flask import redirect, url_for, request
from flask_script import Manager
from flask_socketio import join_room, disconnect
from gevent import Greenlet
from onelove import OneLove
from onelove.monitor import monitor
from onelove.utils import create_app
config_name = os.getenv('FLASK_CONFIG') or 'default'
app = create_app(config_name)
onelove = OneLove(app)
manager = Manager(app)
@manager.command
def runserver():
onelove.socketio.run(
app,
host="0.0.0.0",
debug=True,
use_reloader=True,
)
onelove.collect.init_script(manager)
@app.route('/')
def index():
return redirect(url_for(onelove.api.endpoint('doc')))
@onelove.socketio.on('connect')
def on_connect():
from onelove.models import User
token = request.args.get('token', None)
request.namespace = '/onelove'
if token is None:
disconnect()
return
current_identity = None
try:
current_identity = onelove.jwt.jwt_decode_callback(token)
except:
disconnect()
if current_identity is None:
disconnect()
return
user = User.objects.get(id=current_identity['identity'])
join_room(str(user.id))
if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
Greenlet.spawn(monitor)
if __name__ == '__main__':
manager.run()