Skip to content

Commit d90fd38

Browse files
committed
updating webservice
1 parent dae3184 commit d90fd38

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

webservice/main.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#app.debug = True # Uncomment this if you wish to debug
1313

1414
port = 8765
15-
info = json.load(
16-
open(os.path.join(os.environ['HOME'], ".smc", "info.json"), 'r'))
15+
info_path = os.path.join(os.environ['HOME'], ".smc", "info.json")
16+
info = json.load(open(info_path, 'r'))
1717
base_url = "/%s/port/%s" % (info['project_id'], port)
1818

1919
html_template = """
@@ -43,25 +43,48 @@ def info():
4343
<ul>
4444
<li><a href="%(route)s/">%(route)s/ - list of routes</a></li>
4545
<li><a href="%(route)s/get">%(route)s/get - simple get request</a></li>
46-
</ul>""" % {'route': base_url}
46+
<li><a href="%(route)s/header">%(route)s/header - header information</a></li>
47+
</ul>""" % {
48+
'route': base_url
49+
}
4750
return html_template % {'title': 'Info', 'content': content}
4851

4952

5053
@app.route(base_url + '/get', methods=['GET'])
5154
def get_something():
5255
content = """<p>Try something like <a href="%(base_url)s/get?something=foo">%(base_url)s/get?something=foo</a></p>""" % {
53-
'base_url': base_url}
56+
'base_url': base_url
57+
}
5458
if request.method == 'GET':
5559
something = request.args.get("something", "")
5660
if len(something) > 0:
5761
content = "Got " + something
5862
return html_template % {'title': 'Info', 'content': content}
5963

64+
65+
@app.route(base_url + '/header', methods=['GET'])
66+
def get_header():
67+
68+
header = ''
69+
70+
for k, v in request.headers.iteritems():
71+
header += '{} = {}\n'.format(k, v)
72+
73+
content = '''
74+
<div>The header information I got about you ...</div>
75+
<pre>
76+
{header}
77+
</pre>
78+
'''.format(header=header)
79+
80+
return content
81+
82+
6083
if __name__ == "__main__":
6184
try:
6285
info = json.load(
6386
open(os.path.join(os.environ['HOME'], ".smc", "info.json"), 'r'))
64-
print("Try to open https://cloud.sagemath.com" + base_url + '/')
87+
print("Try to open https://cocalc.com" + base_url + '/')
6588
app.run(host='0.0.0.0', port=port)
6689
import sys
6790
sys.exit(0)

0 commit comments

Comments
 (0)