From 51bc900b008a03519a3492b16d6964871d228e72 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 18 Jan 2019 10:44:10 +0100 Subject: [PATCH] tile route --- web/serve.go | 6 +----- web/tiles.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 web/tiles.go diff --git a/web/serve.go b/web/serve.go index 45ec943..fbcdd97 100644 --- a/web/serve.go +++ b/web/serve.go @@ -1,7 +1,6 @@ package web import ( - "fmt" "github.com/sirupsen/logrus" "mapserver/app" "mapserver/vfs" @@ -19,10 +18,7 @@ func Serve(ctx *app.App) { mux := http.NewServeMux() mux.Handle("/", http.FileServer(vfs.FS(ctx.Config.Webdev))) - - mux.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "Api endpoint") - }) + mux.Handle("/tile/", &Tiles{ctx: ctx}) err := http.ListenAndServe(":"+strconv.Itoa(ctx.Config.Port), mux) if err != nil { diff --git a/web/tiles.go b/web/tiles.go new file mode 100644 index 0000000..4ae38ef --- /dev/null +++ b/web/tiles.go @@ -0,0 +1,34 @@ +package web + +import ( + "mapserver/app" + "mapserver/coords" + "net/http" + "strings" + "strconv" +) + +type Tiles struct { + ctx *app.App +} + +func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request){ + str := strings.TrimPrefix(req.URL.Path, "/tile/") + parts := strings.Split(str, "/") + 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) + data, err := t.ctx.Tilerenderer.Render(c) + + if err != nil { + resp.WriteHeader(500) + resp.Write([]byte(err.Error())) + + } else { + resp.Header().Add("content-type", "image/png") + resp.Write(data) + } +}