From ed9e37e8928a8991e51732c8c52b1c4f7d32a038 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Thu, 18 Sep 2014 07:52:37 +0200 Subject: [PATCH] mtwebmapper: Cleaned up gorilla mux usage a bit. Added stub for post target to get changes from mtredisalize server. --- cmd/mtwebmapper/main.go | 43 ++++++++++++------- cmd/mtwebmapper/subbaseline.go | 74 ++++++++++++++++++--------------- cmd/mtwebmapper/tilesupdater.go | 25 +++++++++++ 3 files changed, 93 insertions(+), 49 deletions(-) create mode 100644 cmd/mtwebmapper/tilesupdater.go diff --git a/cmd/mtwebmapper/main.go b/cmd/mtwebmapper/main.go index f859242..f4bacd5 100644 --- a/cmd/mtwebmapper/main.go +++ b/cmd/mtwebmapper/main.go @@ -15,31 +15,42 @@ import ( func main() { var ( - port int - host string - webDir string - mapDir string + webPort int + webHost string + webDir string + mapDir string + redisPort int + redisHost string ) - flag.IntVar(&port, "port", 8808, "port of the web server") - flag.IntVar(&port, "p", 8808, "port of the web server (shorthand)") - flag.StringVar(&host, "host", "localhost", "address to bind") - flag.StringVar(&host, "h", "localhost", "address to bind (shorthand)") + flag.IntVar(&webPort, "web-port", 8808, "port of the web server") + flag.IntVar(&webPort, "p", 8808, "port of the web server (shorthand)") + flag.StringVar(&webHost, "web-host", "localhost", "address to bind web server") + flag.StringVar(&webHost, "h", "localhost", "address to bind web server(shorthand)") flag.StringVar(&webDir, "web", "web", "static served web files.") flag.StringVar(&webDir, "w", "web", "static served web files (shorthand)") flag.StringVar(&mapDir, "map", "map", "directory of prerendered tiles") flag.StringVar(&mapDir, "m", "map", "directory of prerendered tiles (shorthand)") + flag.StringVar(&redisHost, "redis-host", "", "address of the backend Redis server") + flag.StringVar(&redisHost, "rh", "", "address of the backend Redis server (shorthand)") + flag.IntVar(&redisPort, "redis-port", 6379, "port of the backend Redis server") + flag.IntVar(&redisPort, "rp", 6379, "port of the backend Redis server (shorthand)") flag.Parse() - r := mux.NewRouter() - r.HandleFunc("/map/{z:[0-9]+}/{x:[0-9]+}/{y:[0-9]+}.png", - func(rw http.ResponseWriter, r *http.Request) { - createTiles(rw, r, mapDir) - }) + router := mux.NewRouter() - r.PathPrefix("/").Handler(http.FileServer(http.Dir(webDir))) - http.Handle("/", r) - addr := fmt.Sprintf("%s:%d", host, port) + subBaseLine := newSubBaseLine(mapDir) + router.Path("/map/{z:[0-9]+}/{x:[0-9]+}/{y:[0-9]+}.png").Handler(subBaseLine) + + if redisHost != "" { + tileUpdater := newTileUpdater(mapDir, redisHost, redisPort) + router.Path("/update").Methods("POST").Handler(tileUpdater) + } + + router.PathPrefix("/").Handler(http.FileServer(http.Dir(webDir))) + http.Handle("/", router) + + addr := fmt.Sprintf("%s:%d", webHost, webPort) if err := http.ListenAndServe(addr, nil); err != nil { log.Fatalf("Starting server failed: %s\n", err) } diff --git a/cmd/mtwebmapper/subbaseline.go b/cmd/mtwebmapper/subbaseline.go index e8ef808..cd35be0 100644 --- a/cmd/mtwebmapper/subbaseline.go +++ b/cmd/mtwebmapper/subbaseline.go @@ -19,39 +19,15 @@ import ( "github.com/gorilla/mux" ) -func blowUp(src image.Image) *image.RGBA { - - // TODO: Fast path for image.RGBA - - dst := image.NewRGBA(image.Rect(0, 0, 256, 256)) - - // fix point numbers x:8 - dx, dy := src.Bounds().Dx(), src.Bounds().Dy() - - bx, by := src.Bounds().Min.X<<8, src.Bounds().Min.Y<<8 - - py := by - for y := 0; y < 256; y++ { - sy := (py >> 8) & 0xff - ox := -1 - px := bx - var col color.Color - for x := 0; x < 256; x++ { - sx := (px >> 8) & 0xff - if sx != ox { // Minimize interface indirection access. - ox = sx - col = src.At(sx, sy) - } - dst.Set(x, y, col) - px += dx - } - py += dy - } - - return dst +type subBaseLine struct { + mapDir string } -func createTiles(rw http.ResponseWriter, r *http.Request, mapDir string) { +func newSubBaseLine(mapDir string) *subBaseLine { + return &subBaseLine{mapDir: mapDir} +} + +func (sb *subBaseLine) ServeHTTP(rw http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) xs := vars["x"] ys := vars["y"] @@ -60,7 +36,7 @@ func createTiles(rw http.ResponseWriter, r *http.Request, mapDir string) { x, y, z := toUint(xs), toUint(ys), toUint(zs) if z < 9 { filename := fmt.Sprintf("%d/%d/%d.png", z, x, y) - http.ServeFile(rw, r, filepath.Join(mapDir, filename)) + http.ServeFile(rw, r, filepath.Join(sb.mapDir, filename)) return } @@ -70,7 +46,7 @@ func createTiles(rw http.ResponseWriter, r *http.Request, mapDir string) { tx := x >> (z - 8) ty := y >> (z - 8) - baseTile := filepath.Join(mapDir, fmt.Sprintf("8/%d/%d.png", tx, ty)) + baseTile := filepath.Join(sb.mapDir, fmt.Sprintf("8/%d/%d.png", tx, ty)) var etag string var err error @@ -138,6 +114,38 @@ func createTiles(rw http.ResponseWriter, r *http.Request, mapDir string) { } } +func blowUp(src image.Image) *image.RGBA { + + // TODO: Fast path for image.RGBA + + dst := image.NewRGBA(image.Rect(0, 0, 256, 256)) + + // fix point numbers x:8 + dx, dy := src.Bounds().Dx(), src.Bounds().Dy() + + bx, by := src.Bounds().Min.X<<8, src.Bounds().Min.Y<<8 + + py := by + for y := 0; y < 256; y++ { + sy := (py >> 8) & 0xff + ox := -1 + px := bx + var col color.Color + for x := 0; x < 256; x++ { + sx := (px >> 8) & 0xff + if sx != ox { // Minimize interface indirection access. + ox = sx + col = src.At(sx, sy) + } + dst.Set(x, y, col) + px += dx + } + py += dy + } + + return dst +} + func createETag(path string) (etag string, err error) { var fi os.FileInfo if fi, err = os.Stat(path); err != nil { diff --git a/cmd/mtwebmapper/tilesupdater.go b/cmd/mtwebmapper/tilesupdater.go new file mode 100644 index 0000000..2f8f75d --- /dev/null +++ b/cmd/mtwebmapper/tilesupdater.go @@ -0,0 +1,25 @@ +// Copyright 2014 by Sascha L. Teichmann +// Use of this source code is governed by the MIT license +// that can be found in the LICENSE file. + +package main + +import "net/http" + +type tileUpdater struct { + mapDir string + redisHost string + redisPort int +} + +func newTileUpdater(mapDir, redisHost string, redisPort int) *tileUpdater { + return &tileUpdater{ + mapDir: mapDir, + redisHost: redisHost, + redisPort: redisPort} + +} + +func (tu *tileUpdater) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + // TODO: Implement me! +}