2014-09-16 03:29:28 +02:00
|
|
|
// 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"
|
2014-11-15 13:40:39 +01:00
|
|
|
"net"
|
2014-09-16 03:29:28 +02:00
|
|
|
"net/http"
|
2014-11-15 13:40:39 +01:00
|
|
|
"strings"
|
2014-09-16 03:29:28 +02:00
|
|
|
|
2014-10-03 11:59:54 +02:00
|
|
|
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-16 03:29:28 +02:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2014-11-15 13:40:39 +01:00
|
|
|
func ipsFromHosts(hosts string) ([]net.IP, error) {
|
|
|
|
|
|
|
|
ips := []net.IP{}
|
|
|
|
|
|
|
|
if len(hosts) == 0 { // Empty list: allow all hosts.
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, host := range strings.Split(hosts, ";") {
|
|
|
|
if hips, err := net.LookupIP(host); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
ips = append(ips, hips...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 03:29:28 +02:00
|
|
|
func main() {
|
|
|
|
var (
|
2014-10-26 18:36:47 +01:00
|
|
|
webPort int
|
|
|
|
webHost string
|
|
|
|
webDir string
|
|
|
|
mapDir string
|
|
|
|
redisPort int
|
|
|
|
redisHost string
|
|
|
|
colorsFile string
|
|
|
|
workers int
|
|
|
|
transparent bool
|
2014-11-15 13:40:39 +01:00
|
|
|
updateHosts string
|
2014-09-16 03:29:28 +02:00
|
|
|
)
|
2014-09-18 07:52:37 +02:00
|
|
|
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)")
|
2014-09-16 03:29:28 +02:00
|
|
|
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)")
|
2014-11-15 13:40:39 +01:00
|
|
|
flag.StringVar(&updateHosts, "update-hosts", "localhost",
|
|
|
|
"';' separated list of hosts which are allowed to send map update requests")
|
|
|
|
flag.StringVar(&updateHosts, "u", "localhost",
|
|
|
|
"';' separated list of hosts which are allowed to send map update requests (shorthand)")
|
2014-09-18 07:52:37 +02:00
|
|
|
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)")
|
2014-09-19 13:06:04 +02:00
|
|
|
flag.IntVar(&workers, "workers", 1, "number of workers to render tiles")
|
|
|
|
flag.StringVar(&colorsFile, "colors", "colors.txt", "colors used to render map tiles.")
|
|
|
|
flag.StringVar(&colorsFile, "c", "colors.txt", "colors used to render map tiles (shorthand).")
|
2014-10-26 18:36:47 +01:00
|
|
|
flag.BoolVar(&transparent, "transparent", false, "Render transparent blocks.")
|
|
|
|
flag.BoolVar(&transparent, "t", false, "Render transparent blocks (shorthand).")
|
2014-09-16 03:29:28 +02:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2014-09-18 07:52:37 +02:00
|
|
|
router := mux.NewRouter()
|
2014-09-16 03:29:28 +02:00
|
|
|
|
2014-09-18 07:52:37 +02:00
|
|
|
subBaseLine := newSubBaseLine(mapDir)
|
|
|
|
router.Path("/map/{z:[0-9]+}/{x:[0-9]+}/{y:[0-9]+}.png").Handler(subBaseLine)
|
|
|
|
|
|
|
|
if redisHost != "" {
|
2014-09-19 13:06:04 +02:00
|
|
|
var colors *common.Colors
|
|
|
|
var err error
|
|
|
|
if colors, err = common.ParseColors(colorsFile); err != nil {
|
|
|
|
log.Fatalf("ERROR: problem loading colors: %s", err)
|
|
|
|
}
|
|
|
|
redisAddress := fmt.Sprintf("%s:%d", redisHost, redisPort)
|
2014-11-15 13:40:39 +01:00
|
|
|
|
|
|
|
var allowedUpdateIps []net.IP
|
|
|
|
if allowedUpdateIps, err = ipsFromHosts(updateHosts); err != nil {
|
|
|
|
log.Fatalf("ERROR: name resolving problem: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tu := newTileUpdater(
|
|
|
|
mapDir, redisAddress, allowedUpdateIps, colors, transparent, workers)
|
2014-09-20 15:21:01 +02:00
|
|
|
go tu.doUpdates()
|
2014-09-19 13:06:04 +02:00
|
|
|
router.Path("/update").Methods("POST").Handler(tu)
|
2014-09-18 07:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
router.PathPrefix("/").Handler(http.FileServer(http.Dir(webDir)))
|
|
|
|
http.Handle("/", router)
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%d", webHost, webPort)
|
2014-09-16 03:29:28 +02:00
|
|
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
|
|
|
log.Fatalf("Starting server failed: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|