-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcloudserver.py
91 lines (71 loc) · 2.33 KB
/
cloudserver.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
import datetime
import json
import numpy as np
import traceback
from cloudvolume import CloudVolume
# Import local modules
import process
import config
from flask import Flask, request, make_response, jsonify
app = Flask(__name__)
@app.route("/")
def hello():
"""Return service running."""
running = datetime.datetime.now() - started
return "Service running for {}".format(running)
@app.route("/help")
def help():
"""Return API endpoints."""
h = """
<h1>Available API endpoints</h1>
<table style="width:50%">
<tr>
<th>Name</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td><code>values (POST)</code></td>
<td>returns segmentation IDs at given location</td>
<td>TODO</td>
<tr>
</table>
"""
return h
@app.route("/values", methods=['POST'])
def values():
"""Return segment IDs at given locations."""
# Parse values
try:
if hasattr(request, 'json') and request.json.get('locations'):
locs = request.json['locations']
elif hasattr(request, 'form'):
locs = request.form.get('locations')
except BaseException as e:
app.logger.error('Error: {}'.format(e))
return {'error': str(e)}
if not locs:
return make_response(jsonify({'error': 'No locations provided'}), 400)
if isinstance(locs, str):
locs = json.loads(locs)
try:
locs = np.array(locs)
except BaseException as e:
app.logger.error('Error: {}'.format(e))
return {'error': str(e)}
app.logger.debug('Locations queried: {}'.format(str(locs)))
if locs.shape[0] > config.MaxLocations:
err = {'error': 'Max number of locations ({}) exceeded'.format(config.MaxLocations)}
return make_response(jsonify(err), 400)
try:
seg_ids = process.get_multiple_ids(locs, vol,
max_workers=config.MaxWorkers)
except BaseException:
tb = traceback.format_exc()
app.logger.error('Error: {}'.format(tb))
return make_response(jsonify({'error': str(tb)}), 500)
return jsonify(seg_ids.tolist())
started = datetime.datetime.now()
vol = CloudVolume(**config.CloudVolumeKwargs)
if __name__ == "__main__":
app.run(host='0.0.0.0')