package application import ( "fmt" "time" "github.com/BurntSushi/toml" "github.com/c2h5oh/datasize" ) type QuotaConfig struct { default_size string default_duration string } func (qc QuotaConfig) DefaultSizeBytes() int64 { var v datasize.ByteSize err := v.UnmarshalText([]byte(qc.default_size)) if err != nil { panic(err) } return int64(v) } func (qc QuotaConfig) DefaultDuration() time.Duration { d, err := time.ParseDuration(qc.default_duration) if err != nil { panic(err) } if d < time.Second { panic("choose a longer default") } return d } 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 }