From e9fc40038952a5806b0b81ac1f09b75decee68ef Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Sun, 24 Apr 2022 21:16:50 +0800 Subject: [PATCH] Fix quota from config --- application/bog.go | 3 ++- application/configuration.go | 20 ++++++++++++-------- default.toml | 8 ++++---- main.go | 3 +++ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/application/bog.go b/application/bog.go index c2f1bb5..9bf3a5c 100644 --- a/application/bog.go +++ b/application/bog.go @@ -81,8 +81,9 @@ func New(config *Configuration) *Bog { fsBogRepo := buildFileDataRepository(config.File) uaRepo := buildUserAgentRepository(config.Database) + file_service := domain.NewBogFileService( - uaRepo, fsBogRepo, config.Quota.DefaultSizeBytes(), config.Quota.DefaultDuration(), + uaRepo, fsBogRepo, config.Quota.ParsedSizeBytes(), config.Quota.ParsedDuration(), ) b.mux = buildHttpMux(file_service) return b diff --git a/application/configuration.go b/application/configuration.go index a5c02d7..4b78718 100644 --- a/application/configuration.go +++ b/application/configuration.go @@ -8,15 +8,11 @@ import ( ) -type QuotaConfig struct { - default_size string - default_duration string -} -func (qc QuotaConfig) DefaultSizeBytes() int64 { +func (qc QuotaConfig) ParsedSizeBytes() int64 { var v datasize.ByteSize - err := v.UnmarshalText([]byte(qc.default_size)) + err := v.UnmarshalText([]byte(qc.DefaultSize)) if err != nil { panic(err) @@ -25,8 +21,10 @@ func (qc QuotaConfig) DefaultSizeBytes() int64 { return int64(v) } -func (qc QuotaConfig) DefaultDuration() time.Duration { - d, err := time.ParseDuration(qc.default_duration) +func (qc QuotaConfig) ParsedDuration() time.Duration { + fmt.Sprintf("duration str: %s", qc.DefaultDuration) + + d, err := time.ParseDuration(qc.DefaultDuration) if err != nil { panic(err) @@ -39,6 +37,12 @@ func (qc QuotaConfig) DefaultDuration() time.Duration { return d } +type QuotaConfig struct { + DefaultSize string `toml:"default_size"` + DefaultDuration string `toml:"default_duration"` +} + + type ServerConfig struct { Port int64 Host string diff --git a/default.toml b/default.toml index c5268e1..33a699b 100644 --- a/default.toml +++ b/default.toml @@ -1,7 +1,3 @@ -[quota] - default_size = "1GB" - default_duration = "14D" - [server] port = 8002 host = "127.0.0.1" @@ -12,3 +8,7 @@ [database] backend = "sqlite" connection = "sql.db" + +[quota] + default_size = "1GB" + default_duration = "72h" diff --git a/main.go b/main.go index 4b8fa3e..34cd1a4 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "caj-larsson/bog/application" "io/ioutil" + "fmt" ) func main() { @@ -17,6 +18,8 @@ func main() { panic(err) } + fmt.Printf("%+v\n", config) + bog := application.New(config) bog.Run() }