forked from OpenDSA/OpenDSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
95 lines (79 loc) · 3.33 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
import subprocess
from flask import Flask, jsonify, request, redirect
from flask_autoindex import AutoIndex
import os
import logging
from tools.compress_output import compress_lines
from argparse import ArgumentParser
app = Flask(__name__)
############################## Direct File Index ###############################
AutoIndex(app, browse_root=os.path.curdir)
@app.route('/Books/<book>/')
def redirect_to_rendered_index(book):
'''Quickly access a book instead of browsing to the exact file'''
return redirect(f"/Books/{book}/html/index.html", code=302)
################################## API calls ###############################
def run_command(cmd: str):
app.logger.info("API call for command: {cmd}")
proc = subprocess.run(cmd.split(), capture_output=True)
if proc.returncode:
app.logger.error(f"API: command failure: {cmd}")
app.logger.error(proc.stderr)
else:
app.logger.info("API command success, responding via API..")
resp = {"ok": True, "message": proc.returncode}
resp['stdout_compressed'] = compress_lines(proc.stdout)
resp['stderr_compressed'] = compress_lines(proc.stderr)
return resp
@app.route('/api/configure/', methods=['POST'])
def configure():
if request.method != 'POST':
return jsonify({"ok", False})
script_path = "tools/configure.py"
config_file_path = request.form['config_file_path']
build_path = request.form['build_path']
cmd = f"python3 {script_path} {config_file_path} -b {build_path}"
if request.form['rake'] != 'false':
cmd += " --standalone-modules"
resp = run_command(cmd)
return jsonify(resp)
@app.route('/api/simple2full/', methods=['POST'])
def simple2full():
if request.method != 'POST':
return jsonify({"ok", False})
script_path = "tools/simple2full.py"
input_path = request.form['input_path']
output_path = request.form['output_path']
cmd = f"python3 {script_path} {input_path} {output_path}"
if request.form['rake'] != 'false':
cmd += " --expanded --verbose"
resp = run_command(cmd)
return jsonify(resp)
@app.route('/api/irtcurve/', methods=['POST'])
def irt_curve():
if request.method != 'POST':
return jsonify({"ok", False})
script_path = "tools/irt_curve.py"
bookID = request.form['bookID']
cmd = f"python3 {script_path} {bookID}"
resp = run_command(cmd)
return jsonify(resp)
@app.route('/deforms/api/deformsfeedback/', methods=['POST'])
def deforms_feedback():
if request.method != 'POST':
return jsonify({"ok": False})
script_path = "tools/deforms_feedback.py"
problem_attempt = request.json
str_problem_attempt = str(problem_attempt).replace(" ","")
cmd = f"python3 {script_path} \"{str_problem_attempt}\""
resp = run_command(cmd)
return jsonify(resp)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--host', default="0.0.0.0", nargs=1, help='Specify host [default: 0.0.0.0]')
parser.add_argument('--port', default=8080, type=int, nargs=1, help='Specify port [default: 8080]')
parser.add_argument('--debug', action='store_true', help='Debug flag for Flask')
args = parser.parse_args()
# print(f"OpenDSA API is starting with Flask {args=}")
print('If Traefik proxy is working, URL will be: https://opendsa.localhost.devcom.vt.edu')
app.run(**vars(args))