808a1e9deb
Took way longer than it should had because I was researching exisiting options. As it turns out, nothing nice exists that would handle: - Multiple overridiable config files - Defaults provided in a struct - Output in a struct License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
39 lines
737 B
Go
39 lines
737 B
Go
package config
|
|
|
|
import "time"
|
|
|
|
// Root is starting point of the config
|
|
type Root struct {
|
|
API API
|
|
}
|
|
|
|
// API contains configs for API endpoint
|
|
type API struct {
|
|
ListenAddress string
|
|
Timeout Duration
|
|
}
|
|
|
|
// Default returns the default config
|
|
func Default() *Root {
|
|
def := Root{
|
|
API: API{
|
|
ListenAddress: "/ip6/::1/tcp/1234/http",
|
|
Timeout: Duration(30 * time.Second),
|
|
},
|
|
}
|
|
return &def
|
|
}
|
|
|
|
// Duration is a wrapper type for time.Duration for decoding it from TOML
|
|
type Duration time.Duration
|
|
|
|
// UnmarshalText implements interface for TOML decoding
|
|
func (dur *Duration) UnmarshalText(text []byte) error {
|
|
d, err := time.ParseDuration(string(text))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*dur = Duration(d)
|
|
return err
|
|
}
|