mapserver/web/tiles.go
2019-01-21 15:13:33 +01:00

48 lines
855 B
Go

package web
import (
"mapserver/app"
"mapserver/coords"
"net/http"
"strconv"
"strings"
)
type Tiles struct {
ctx *app.App
}
func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/tile/")
parts := strings.Split(str, "/")
if len(parts) != 4 {
resp.WriteHeader(500)
resp.Write([]byte("wrong number of arguments"))
return
}
layerid, _ := strconv.Atoi(parts[0])
x, _ := strconv.Atoi(parts[1])
y, _ := strconv.Atoi(parts[2])
zoom, _ := strconv.Atoi(parts[3])
c := coords.NewTileCoords(x, y, zoom, layerid)
tile, err := t.ctx.Objectdb.GetTile(c)
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
} else {
resp.Header().Add("content-type", "image/png")
if tile == nil {
//TODO: blank tile
} else {
resp.Write(tile.Data)
}
}
}