-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
89 lines (72 loc) · 2.55 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import mimetypes, json
from beaker.middleware import SessionMiddleware
from waitress import serve
import numpy as np
import texscript
# TODO: consider adding mongo db for saving script files as unique url's
# BUG: history is broken
def app(environ, start_response):
length = int(environ.get('CONTENT_LENGTH', '0'))
math = environ['wsgi.input'].read(length).decode('utf-8')
path = environ['PATH_INFO']
content_type = mimetypes.guess_type(path)[0] or 'text/html'
session = environ['beaker.session']
if environ['REQUEST_METHOD'] == 'GET':
if path == '/' or path == '/index.html':
status = '200 OK'
output = open('www/index.html').read()
else:
try:
status = '200 OK'
if content_type[:5] == 'image':
start_response(status, [('Content-Type', 'image/jpeg')])
return environ['wsgi.file_wrapper'](open('www' + path, 'rb'), 32768)
else:
output = open('www' + path).read()
except FileNotFoundError:
status = '404 Not Found'
output = status
elif environ['REQUEST_METHOD'] == 'POST':
if path == '/compute':
session['variables'] = {} if 'variables' not in session else session['variables']
session['functions'] = {} if 'functions' not in session else session['functions']
session['history'] = [] if 'history' not in session else session['history']
tex = texscript.session(session['variables'], session['functions'], session['history'])
if 'HTTP_X_NO_HISTORY' not in environ:
tex.history.append(math)
try:
status = '200 OK'
val = tex.compute(math)
if type(val) == type(np.array([])):
val = val.tolist()
try:
output = json.dumps(val)
except TypeError:
if type(val) == type(np.array([])):
output = str(val.tolist())
else:
output = str(val)
session['variables'] = tex._['variables']
session['functions'] = [tex._['functions'][fn]['math'] for fn in tex._['functions']]
session['history'] = tex.history
except (ValueError, SyntaxError, IndexError) as err:
status = '400 Bad Request'
output = 'Invalid Tex'
else:
status = '500 Internal Server Error'
output = status
output = output.encode('utf-8')
headers = [
('Content-type', content_type),
('Access-control-allow-origin', '*'),
('Content-Length', str(len(output)))
]
session.save()
start_response(status, headers)
return [output]
wsgi_app = SessionMiddleware(app, {
'session.type': 'file',
'session.cookie_expires': True,
'session.data_dir': '/tmp'
})
serve(wsgi_app)