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,6 +13,10 @@ import (
"bitbucket.org/s_l_teichmann/mtsatellite/common" "bitbucket.org/s_l_teichmann/mtsatellite/common"
) )
type duration struct {
time.Duration
}
type config struct { type config struct {
WebPort int `toml:"web_port"` WebPort int `toml:"web_port"`
WebHost string `toml:"web_host"` WebHost string `toml:"web_host"`
@ -30,7 +34,13 @@ type config struct {
PlayersFIFO string `toml:"players"` PlayersFIFO string `toml:"players"`
YMin int `toml:"ymin"` YMin int `toml:"ymin"`
YMax int `toml:"ymax"` YMax int `toml:"ymax"`
ChangeDuration time.Duration `toml:"change_duration"` 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() { 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.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.YMin, "ymin", common.MinHeight, "Minimum y in blocks.")
flag.IntVar(&cfg.YMax, "ymax", common.MaxHeight, "Maximum 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)") "change-duration", time.Second, "Duration to aggregate changes. (PG only)")
} }

View File

@ -96,7 +96,7 @@ func main() {
go tu.doUpdates() go tu.doUpdates()
if pgHost, ok := common.IsPostgreSQL(cfg.RedisHost); btu != nil && ok { if pgHost, ok := common.IsPostgreSQL(cfg.RedisHost); btu != nil && ok {
go tu.listen(pgHost, cfg.ChangeDuration) go tu.listen(pgHost, cfg.ChangeDuration.Duration)
} else { } else {
router.Path("/update").Methods("POST").Handler(tu) router.Path("/update").Methods("POST").Handler(tu)
} }