This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geo_server.py
75 lines (59 loc) · 1.82 KB
/
geo_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
"""GeoServer."""
import random
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
from geo import alt, geodata
DEFAULT_CACHE_TIMEOUT = 120
logging.basicConfig(level=logging.INFO)
def _warmup():
logging.info(log_metrics())
random_latlng = [6 + random.random() * 3, 79.9 + random.random()]
geodata.get_latlng_regions(random_latlng)
region_id = 'LK-%d' % (random.randint(1, 9))
geodata.get_region_geo(region_id)
logging.info('GeoServer warmup complete.')
_warmup()
app = Flask(__name__)
CORS(app)
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
cache.init_app(app)
@app.route('/status')
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def status():
"""Index."""
return log_metrics() | {'server': 'geo_server'}
@app.route('/latlng_to_region/<string:latlng_str>')
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def latlng_to_region(latlng_str):
"""Get region from latlng."""
lat, _, lng = latlng_str.partition(',')
lat_lng = (float)(lat), (float)(lng)
return geodata.get_latlng_regions(lat_lng)
@app.route('/region_geo/<string:region_id>')
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def region_geo(region_id):
"""Get region."""
return geodata.get_region_geo(region_id)
@app.route('/altitude/<string:latlng_str>')
@cache.cached(timeout=DEFAULT_CACHE_TIMEOUT)
def altitude(latlng_str):
"""Get altitude for latlng."""
lat, _, lng = latlng_str.partition(',')
lat_lng = (float)(lat), (float)(lng)
return {
'altitude': alt.get_altitude(lat_lng),
}
if __name__ == '__main__':
PORT = 4002
HOST = '0.0.0.0'
logging.info('Starting geo_server on %s:%d...' % (HOST, PORT))
serve(
app,
host=HOST,
port=PORT,
threads=8,
)