mtwebmapper: Cleaned up gorilla mux usage a bit. Added stub for post target to get changes from mtredisalize server.

This commit is contained in:
Sascha L. Teichmann 2014-09-18 07:52:37 +02:00
parent e81051a84c
commit ed9e37e892
3 changed files with 93 additions and 49 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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!
}