forked from nuuuwan/gig_server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gig_server.py
81 lines (65 loc) · 1.98 KB
/
gig_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
"""GIGServer."""
import logging
from flask import Flask
from flask_caching import Cache
from flask_cors import CORS
from waitress import serve
from utils.sysx import log_metrics
import gig.ents
import gig.nearby
import gig.ext_data
DEFAULT_CACHE_TIMEOUT = 120
logging.basicConfig(level=logging.INFO)
app = Flask(__name__)
CORS(app)
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
cache.init_app(app)
# ----------------------------------------------------------------
# Handlers
# ----------------------------------------------------------------
@app.route('/status')
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def status():
"""Index."""
data = log_metrics()
data['server'] = 'gig_server'
return data
@app.route('/entities/<string:entity_ids_str>')
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def entities(entity_ids_str):
"""Get entity."""
_entity_ids = entity_ids_str.split(';')
return gig.ents.multiget_entities(_entity_ids)
@app.route('/entity_ids/<string:entity_type>')
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def entity_ids(entity_type):
"""Get entity IDs."""
return {
'entity_ids': gig.ents.get_entity_ids(entity_type),
}
@app.route('/nearby/<string:latlng_str>')
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def nearby(latlng_str):
"""Get places near latlng."""
lat, _, lng = latlng_str.partition(',')
lat_lng = (float)(lat), (float)(lng)
return {
'nearby_entity_info_list': gig.nearby.get_nearby_entities(lat_lng),
}
@app.route(
'/ext_data/<string:data_group>/<string:table_id>/<string:entity_id>'
)
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def ext_data(data_group, table_id, entity_id):
"""Get extended data."""
return gig.ext_data.get_table_data(data_group, table_id, [entity_id])
if __name__ == '__main__':
PORT = 4001
HOST = '0.0.0.0'
logging.info('Starting gig_server on %s:%d...', HOST, PORT)
serve(
app,
host=HOST,
port=PORT,
threads=8,
)