forked from saxifrage/caac-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·65 lines (47 loc) · 1.34 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
#!/usr/bin/env python
import os
import io
import genmap
import threading
import requests
import flask
DEV = bool(os.environ.get('FLASK_DEBUG', False))
# Job Class
# =========
class Job(threading.Thread):
is_daemon = True
def run(self):
callback_url, topics = self._args
fp = io.StringIO()
big, blocks = genmap.generate_map(topics, **self._kwargs)
genmap.output_svg(topics, fp, big, blocks)
fp.seek(0)
requests.post(callback_url, data=fp.read(), headers={'Content-Type': 'image/svg+xml'})
# Flask App
# =========
app = flask.Flask(__name__)
@app.route('/')
def redirect_to_v1():
return flask.redirect('/v1')
_documentation = open('documentation.html').read()
@app.route('/v1', methods=['GET'])
def documentation():
return _documentation
@app.route('/v1', methods=['POST'])
def v1():
kw = flask.request.get_json()
callback_url = kw.pop('callback_url')
topics = kw.pop('topics')
Job(args=(callback_url, topics), kwargs=kw).start()
return ''
if DEV:
@app.route('/v1/callback-test/<filename>', methods=['POST'])
def callback_test(filename):
open(filename, 'wb+').write(flask.request.get_data())
return ''
if __name__ == '__main__':
app.debug = DEV
kw = dict()
if app.debug:
kw['extra_files'] = ['documentation.html']
app.run(**kw)