-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
executable file
·90 lines (66 loc) · 2.04 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
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
#!/usr/bin/env python
import os
import sys
import shutil
import math
import traceback
import bottle
C_BLACK = "\033[1;30m"
C_RED = "\033[1;31m"
C_GREEN = "\033[1;32m"
C_BROWN = "\033[1;33m"
C_BLUE = "\033[1;34m"
C_MAGENTA = "\033[1;35m"
C_CYAN = "\033[1;36m"
C_LIGHTGREY = "\033[1;37m"
C_OFF = "\033[0m"
C_CLEAR = "\033[2K"
class EnableCors(object):
name = 'enable_cors'
api = 2
def apply(self, fn, context):
def _enable_cors(*args, **kwargs):
# set CORS headers
bottle.response.headers['Access-Control-Allow-Origin'] = '*'
bottle.response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
bottle.response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
# bottle.response.headers['Access-Control-Allow-Methods'] = '*'
# bottle.response.headers['Access-Control-Allow-Headers'] = '*'
if bottle.request.method != 'OPTIONS':
return fn(*args, **kwargs)
return _enable_cors
app = bottle.app()
app.install(EnableCors())
db = None
def get_tile_file(z,x,y):
print 'get_tile_file ', (z,x,y)
z = int(z)
x = int(x)
y = int(y)
base_dir = sys.argv[1]
path = base_dir + '/%d/%d/%d.pbf' % (z,x,y)
if os.path.isfile(path):
with open(path, 'rb') as fh:
data = fh.read()
print len(data),
print C_GREEN, path, C_OFF
return data
else:
print C_RED, path, C_OFF
return None
return None
@app.route('<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./static')
@app.route('/')
def hello():
bottle.redirect("hab.html")
@app.get('/<x>/<y>/<z>.pbf', methods=['GET'])
def tiles(x=0, y=0, z=0):
bottle.response.content_type = 'application/x-protobuf'
# bottle.response.content_disposition = 'attachment' # https://github.com/openmaptiles/postserve/blob/master/server.py#L89
# bottle.response.content_encoding = 'gzip'
tile = get_tile_file(z, x, y) # mind: zoom is first arg
return tile
# http://localhost:3000/552/329/10.pbf
bottle.run( host='localhost', port=3000, debug=True, reloader=True )