Deserialize duration from config toml

This commit is contained in:
Sascha L. Teichmann 2022-03-01 23:45:25 +01:00
parent 1155930bc9
commit c17bca9c94
2 changed files with 29 additions and 19 deletions

View File

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

View File

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