Skip to content

Commit 4fe9020

Browse files
committed
Add Glug support to mbtileserver
1 parent a66c949 commit 4fe9020

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

resources/mbtileserver.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,45 @@
2020
require 'rack'
2121
require 'sqlite3'
2222

23+
begin; require 'glug' # Optional glug dependency
24+
rescue LoadError; end # |
25+
2326
db = SQLite3::Database.new(ARGV[0])
2427
content_types = {
2528
json: "application/json",
26-
png: "image/png"
29+
png: "image/png",
30+
pbf: "application/octet-stream"
2731
}
2832

2933
app = Proc.new do |env|
3034
path = env['REQUEST_PATH'].sub(/^\//,'')
35+
3136
if path =~ %r!/(\d+)/(\d+)/(\d+).*\.pbf!
37+
# Serve .pbf tile from mbtiles
3238
z,x,y = $1.to_i, $2.to_i, $3.to_i
3339
tms_y = 2**z - y - 1
3440
res = db.execute("SELECT tile_data FROM tiles WHERE zoom_level=? AND tile_column=? AND tile_row=?", [z, x, tms_y])
3541
if res.length>0
3642
puts "Serving #{z}/#{x}/#{y}"
3743
blob = res[0][0]
38-
['200', {'Content-Type'=>'application/x-protobuf', 'Content-Encoding'=>'gzip'}, [blob]]
44+
['200', {'Content-Type'=>'application/x-protobuf', 'Content-Encoding'=>'gzip', 'Content-Length'=>blob.bytesize.to_s }, [blob]]
3945
else
4046
puts "Empty #{z}/#{x}/#{y}"
41-
['200', {'Content-Type'=>'application/x-protobuf' }, []]
47+
['200', {'Content-Type'=>'application/x-protobuf', 'Content-Length'=>'0' }, []]
4248
end
49+
4350
elsif ARGV.include?(path)
51+
# Serve static file
4452
ct = path.match(/\.(\w+)$/) ? (content_types[$1.to_sym] || 'text/html') : 'text/html'
4553
['200', {'Content-Type' => ct}, [File.read(path)]]
54+
55+
elsif ARGV.include?(path.sub(/\.json$/,'.glug'))
56+
# Convert .glug style to .json
57+
glug = Glug::Stylesheet.new { instance_eval(File.read( path.sub(/\.json$/,'.glug') )) }.to_json
58+
['200', {'Content-Type' => 'application/json' }, [glug] ]
59+
4660
else
61+
# Not found
4762
['404', {'Content-Type' => 'text/html'}, ["Resource at #{path} not found"]]
4863
end
4964
end

0 commit comments

Comments
 (0)