package server import ( "fmt" "github.com/BurntSushi/toml" "github.com/c2h5oh/datasize" "time" ) 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 LoggingConfig struct { Level string } type Configuration struct { Server ServerConfig Admin ServerConfig File FileConfig Database DatabaseConfig Quota QuotaConfig Logging LoggingConfig } func (c ServerConfig) bindAddress() string { return fmt.Sprintf("%s:%d", c.Host, c.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 }