-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
72 lines (51 loc) · 1.81 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
import json
from functools import wraps
from flask import Flask, render_template, url_for, redirect, request, make_response, jsonify
app = Flask(__name__)
with open('config.json') as cfg:
config = json.load(cfg)
def require_auth(func):
@wraps(func)
def wrapper(*args, **kwargs):
auth = request.cookies.get('auth') or request.args.get('auth')
if auth != config['configpwd']:
return render_template('illegal.html')
resp = make_response(func(*args, **kwargs))
resp.set_cookie('auth', auth)
return resp
return wrapper
def write_config():
with open('config.json', 'w') as cfg:
json.dump(config, cfg)
@app.route('/config/raw/<int:real_shard>')
@require_auth
def get_raw_config(real_shard):
new_config = config.copy()
new_config['shard_min'] = int(real_shard * config['dshards'] / config['shards'])
new_config['shard_max'] = int((real_shard + 1) * config['dshards'] / config['shards'] )
return jsonify(new_config)
@app.route('/')
@require_auth
def index():
return render_template('index.html')
@app.route('/config/get')
@require_auth
def get_config():
return render_template('get-config.html', readable_config=json.dumps(config, indent=4), config=config)
@app.route('/config/set', methods=['GET'])
@require_auth
def set_config():
return render_template('set-config.html', shards=config['shards'], dshards=config['dshards'], token=config['token'])
@app.route('/config/new', methods=['POST'])
@require_auth
def set_config_now():
token = request.form['token']
shards = int(request.form['shards'])
dshards = int(request.form['dshards'])
config['shards'] = shards
config['token'] = token
config['dshards'] = dshards
write_config()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run()