package server import ( "fmt" "time" "github.com/BurntSushi/toml" "github.com/c2h5oh/datasize" ) func (qc QuotaConfig) ParsedSizeBytes() int64 { var v datasize.ByteSize err := v.UnmarshalText([]byte(qc.DefaultSize)) if err != nil { panic(err) } return int64(v) } func (qc QuotaConfig) ParsedDuration() time.Duration { fmt.Sprintf("duration str: %s", qc.DefaultDuration) d, err := time.ParseDuration(qc.DefaultDuration) if err != nil { panic(err) } if d < time.Second { panic("choose a longer default") } return d } type QuotaConfig struct { DefaultSize string `toml:"default_size"` DefaultDuration string `toml:"default_duration"` } type ServerConfig struct { Port int64 Host string } type FileConfig struct { Path string } type DatabaseConfig struct { Backend string Connection string } type Configuration struct { Server ServerConfig File FileConfig Database DatabaseConfig Quota QuotaConfig } func (c *Configuration) bindAddress() string { return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port) } func ConfigFromToml(toml_data string) (*Configuration, error) { var config Configuration _, err := toml.Decode(toml_data, &config) if err != nil { return nil, err } return &config, nil }