From c17bca9c94d3eec139c18c79be242938c79cf695 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Tue, 1 Mar 2022 23:45:25 +0100 Subject: [PATCH] Deserialize duration from config toml --- cmd/mtwebmapper/config.go | 46 ++++++++++++++++++++++++--------------- cmd/mtwebmapper/main.go | 2 +- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/cmd/mtwebmapper/config.go b/cmd/mtwebmapper/config.go index 63407c7..f4c5f0a 100644 --- a/cmd/mtwebmapper/config.go +++ b/cmd/mtwebmapper/config.go @@ -13,24 +13,34 @@ import ( "bitbucket.org/s_l_teichmann/mtsatellite/common" ) +type duration struct { + time.Duration +} + type config struct { - WebPort int `toml:"web_port"` - WebHost string `toml:"web_host"` - WebDir string `toml:"web"` - MapDir string `toml:"map"` - RedisPort int `toml:"redis_port"` - RedisHost string `toml:"redis_host"` - ColorsFile string `toml:"update_hosts"` - BGColor string `toml:"background"` - Workers int `toml:"workers"` - Transparent bool `toml:"transparent"` - TransparentDim float64 `toml:"transparent_dim"` - UpdateHosts string `toml:"update_hosts"` - Websockets bool `toml:"websockets"` - PlayersFIFO string `toml:"players"` - YMin int `toml:"ymin"` - YMax int `toml:"ymax"` - ChangeDuration time.Duration `toml:"change_duration"` + WebPort int `toml:"web_port"` + WebHost string `toml:"web_host"` + WebDir string `toml:"web"` + MapDir string `toml:"map"` + RedisPort int `toml:"redis_port"` + RedisHost string `toml:"redis_host"` + ColorsFile string `toml:"update_hosts"` + BGColor string `toml:"background"` + Workers int `toml:"workers"` + Transparent bool `toml:"transparent"` + TransparentDim float64 `toml:"transparent_dim"` + UpdateHosts string `toml:"update_hosts"` + Websockets bool `toml:"websockets"` + PlayersFIFO string `toml:"players"` + YMin int `toml:"ymin"` + YMax int `toml:"ymax"` + ChangeDuration duration `toml:"change_duration"` +} + +func (d *duration) UnmarshalText(text []byte) error { + var err error + d.Duration, err = time.ParseDuration(string(text)) + return err } func (cfg *config) bindFlags() { @@ -71,7 +81,7 @@ func (cfg *config) bindFlags() { flag.StringVar(&cfg.PlayersFIFO, "ps", "", "Path to FIFO file to read active players from (shorthand).") flag.IntVar(&cfg.YMin, "ymin", common.MinHeight, "Minimum y in blocks.") flag.IntVar(&cfg.YMax, "ymax", common.MaxHeight, "Maximum y in blocks.") - flag.DurationVar(&cfg.ChangeDuration, + flag.DurationVar(&cfg.ChangeDuration.Duration, "change-duration", time.Second, "Duration to aggregate changes. (PG only)") } diff --git a/cmd/mtwebmapper/main.go b/cmd/mtwebmapper/main.go index 1f4ba41..64c465f 100644 --- a/cmd/mtwebmapper/main.go +++ b/cmd/mtwebmapper/main.go @@ -96,7 +96,7 @@ func main() { go tu.doUpdates() if pgHost, ok := common.IsPostgreSQL(cfg.RedisHost); btu != nil && ok { - go tu.listen(pgHost, cfg.ChangeDuration) + go tu.listen(pgHost, cfg.ChangeDuration.Duration) } else { router.Path("/update").Methods("POST").Handler(tu) }