mtsatellite/cmd/mtwebmapper/main.go

58 lines
1.8 KiB
Go

// 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 (
"flag"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
var (
webPort int
webHost string
webDir string
mapDir string
redisPort int
redisHost string
)
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()
router := mux.NewRouter()
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)
}
}