-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
72 lines (56 loc) · 1.97 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
# use greenlets for greater concurrency
#from gevent import monkey; monkey.patch_all()
import bottle
import json
import os
from bottle import request, response, static_file
from simfile import SMParser
from urllib.parse import parse_qsl
PROD = True
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
SIMFILES_DIR = PROJECT_ROOT + '/simfiles'
BUILD_DIR = PROJECT_ROOT + '/frontend/build'
STATIC_DIR = BUILD_DIR + '/static'
app = bottle.Bottle()
@app.get('/')
def root():
return static_file('index.html', root=BUILD_DIR)
@app.get('/static/css/<filename>')
def static_css(filename):
return static_file(filename, root=STATIC_DIR + '/css')
@app.get('/static/js/<filename>')
def static_js(filename):
return static_file(filename, root=STATIC_DIR + '/js')
@app.get('/api/v1/simfiles')
def api_v1_simfiles():
sims = sorted(os.listdir(SIMFILES_DIR), key=lambda s: s.lower())
return json.dumps([{'value': sim, 'label': sim}
for sim in sims])
@app.get('/api/v1/simfiles/<name>', method='GET')
def api_v1_simfiles_name(name):
query_params = dict(parse_qsl(request.query_string))
# strip invalid params
[query_params.pop(param) for param in list(query_params.keys())
if not param in SMParser.analyze.__code__.co_varnames]
try:
with open(SIMFILES_DIR + '/' + os.path.basename(name), encoding='utf_8_sig') as fh:
parsed = SMParser(fh.read())
# just override this for now, not all charts have a Hard/Expert chart
query_params['difficulty'] = list(parsed.charts['Single'].keys())[-1]
return {
"result": parsed.analyze(**query_params)
}
except Exception as e:
return { "errors": "Could not load simfile (bad param?): " + e.message }
def run():
bottle.debug(not PROD)
bottle.run(
app=app,
host='0.0.0.0',
port=os.environ.get('PORT', 8000),
reloader=not PROD,
server='gunicorn',
workers=8,
)
if __name__=='__main__':
run()