-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
executable file
·69 lines (54 loc) · 1.81 KB
/
server.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
#!/usr/bin/env python3
import src.core as core
import flask
# from flask_cors import cross_origin
import src.utils.files as files
app = flask.Flask(__name__)
app.debug = True
# placeholder landing page.
@app.route('/')
def landing():
return 'hello world!'
# placeholder callback route.
@app.route('/callback/<survey>', methods = ['GET','POST'])
# @cross_origin()
def callback(survey):
print('callback: ',survey)
if flask.request.method == 'GET':
return handle_spec_request(survey)
else:
data = flask.request.json
print('data: ',data)
core.save_responses(survey,data)
return flask.jsonify({})
# user survey access route...
@app.route('/surveys/<survey>', methods = ['GET'])
def surveys(survey):
print('survey-request: ',survey)
return survey_app(survey,'form')
# building kiosk access route...
@app.route('/kiosks/<survey>',methods=['GET'])
def kiosks(survey):
return survey_app(survey,'kiosk')
# survey app constructor.
def survey_app(survey,mode):
# survey mode integer mapping.
codes = {'kiosk' : 1, 'form' : 0}
# application callback address.
server = '/callback/{}'.format(survey)
# config dict to be passed as flags to the elm-app.
config = {'srvr': server, 'tick': 20, 'mode': codes.get(mode,1)}
# return rendered template w/ config dict inserted.
return flask.render_template('survey.html',config=config)
# handler for survey spec requests from apps.
def handle_spec_request(survey):
# if survey exists, load & return its spec.
if core.survey_exists(survey):
spec = core.load_survey(survey)
return flask.jsonify(spec)
# if survey did not exist, send back `400`.
else:
print('cannot find survey: ',survey)
return flask.Response(status=400)
if __name__ == '__main__':
app.run()