2015-07-26 16:33:29 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
2014-09-16 03:29:28 +02:00
|
|
|
// 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-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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-03-01 23:30:31 +01:00
|
|
|
|
2014-09-16 03:29:28 +02:00
|
|
|
var (
|
2022-03-01 23:30:31 +01:00
|
|
|
cfg config
|
|
|
|
cfgFile string
|
|
|
|
version bool
|
2014-09-16 03:29:28 +02:00
|
|
|
)
|
2016-04-23 16:45:33 +02:00
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
flag.StringVar(&cfgFile, "config", "", "configuration file")
|
2015-07-26 22:04:16 +02:00
|
|
|
flag.BoolVar(&version, "version", false, "Print version and exit.")
|
2022-03-01 23:30:31 +01:00
|
|
|
cfg.bindFlags()
|
2014-09-16 03:29:28 +02:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-07-26 22:04:16 +02:00
|
|
|
if version {
|
|
|
|
common.PrintVersionAndExit()
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
if cfgFile != "" {
|
|
|
|
if err := cfg.load(cfgFile); err != nil {
|
|
|
|
log.Fatalf("error: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bg := common.ParseColorDefault(cfg.BGColor, common.BackgroundColor)
|
2016-04-23 16:45:33 +02:00
|
|
|
|
2014-09-18 07:52:37 +02:00
|
|
|
router := mux.NewRouter()
|
2014-09-16 03:29:28 +02:00
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
subBaseLine := newSubBaseLine(cfg.MapDir, bg)
|
2014-09-18 07:52:37 +02:00
|
|
|
router.Path("/map/{z:[0-9]+}/{x:[0-9]+}/{y:[0-9]+}.png").Handler(subBaseLine)
|
|
|
|
|
2015-03-02 13:10:30 +01:00
|
|
|
var btu baseTilesUpdates
|
2015-03-09 13:16:31 +01:00
|
|
|
var wsf *websocketForwarder
|
2015-03-09 13:09:13 +01:00
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
if cfg.Websockets {
|
2015-03-09 13:16:31 +01:00
|
|
|
wsf = newWebsocketForwarder()
|
2015-03-02 14:16:36 +01:00
|
|
|
go wsf.run()
|
|
|
|
router.Path("/ws").Methods("GET").Handler(wsf)
|
|
|
|
btu = wsf
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
if cfg.PlayersFIFO != "" {
|
|
|
|
plys := newPlayers(cfg.PlayersFIFO, wsf)
|
2015-03-09 13:16:31 +01:00
|
|
|
go plys.run()
|
|
|
|
router.Path("/players").Methods("GET").Handler(plys)
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
if cfg.RedisHost != "" {
|
2022-02-27 21:31:55 +01:00
|
|
|
|
2014-09-19 13:06:04 +02:00
|
|
|
var colors *common.Colors
|
|
|
|
var err error
|
2022-03-01 23:30:31 +01:00
|
|
|
if colors, err = common.ParseColors(cfg.ColorsFile); err != nil {
|
2014-09-19 13:06:04 +02:00
|
|
|
log.Fatalf("ERROR: problem loading colors: %s", err)
|
|
|
|
}
|
2015-07-26 11:55:38 +02:00
|
|
|
colors.TransparentDim = common.Clamp32f(
|
2022-03-01 23:30:31 +01:00
|
|
|
float32(cfg.TransparentDim/100.0), 0.0, 100.0)
|
2022-02-27 21:31:55 +01:00
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
dbcf, err := common.CreateDBClientFactory(cfg.RedisHost, cfg.RedisPort)
|
2022-03-01 14:47:14 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: %v\n", err)
|
|
|
|
}
|
|
|
|
defer dbcf.Close()
|
2014-11-15 13:40:39 +01:00
|
|
|
|
|
|
|
var allowedUpdateIps []net.IP
|
2022-03-01 23:30:31 +01:00
|
|
|
if allowedUpdateIps, err = ipsFromHosts(cfg.UpdateHosts); err != nil {
|
2014-11-15 13:40:39 +01:00
|
|
|
log.Fatalf("ERROR: name resolving problem: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tu := newTileUpdater(
|
2022-03-01 23:30:31 +01:00
|
|
|
cfg.MapDir,
|
2022-03-01 14:47:14 +01:00
|
|
|
dbcf,
|
2015-03-02 13:10:30 +01:00
|
|
|
allowedUpdateIps,
|
2016-04-23 16:45:33 +02:00
|
|
|
colors, bg,
|
2022-03-01 23:30:31 +01:00
|
|
|
cfg.YMin, cfg.YMax,
|
|
|
|
cfg.Transparent,
|
|
|
|
cfg.Workers,
|
2015-03-02 13:10:30 +01:00
|
|
|
btu)
|
2014-09-20 15:21:01 +02:00
|
|
|
go tu.doUpdates()
|
2022-02-28 11:07:50 +01:00
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
if pgHost, ok := common.IsPostgreSQL(cfg.RedisHost); btu != nil && ok {
|
2022-03-01 23:45:25 +01:00
|
|
|
go tu.listen(pgHost, cfg.ChangeDuration.Duration)
|
2022-02-28 11:07:50 +01:00
|
|
|
} else {
|
|
|
|
router.Path("/update").Methods("POST").Handler(tu)
|
|
|
|
}
|
2014-09-18 07:52:37 +02:00
|
|
|
}
|
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
router.PathPrefix("/").Handler(http.FileServer(http.Dir(cfg.WebDir)))
|
2014-09-18 07:52:37 +02:00
|
|
|
http.Handle("/", router)
|
|
|
|
|
2022-03-01 23:30:31 +01:00
|
|
|
addr := fmt.Sprintf("%s:%d", cfg.WebHost, cfg.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)
|
|
|
|
}
|
|
|
|
}
|