From 9eca063c9fc11d4b8b7ecb8d18fecd4326969b69 Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Sun, 24 Apr 2022 20:19:03 +0800 Subject: [PATCH] Handle default quotas in config and application --- application/bog.go | 4 +++- application/configuration.go | 37 ++++++++++++++++++++++++++++++++++++ default.toml | 7 ++++++- go.mod | 1 + go.sum | 2 ++ 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/application/bog.go b/application/bog.go index 2e55097..c2f1bb5 100644 --- a/application/bog.go +++ b/application/bog.go @@ -81,7 +81,9 @@ func New(config *Configuration) *Bog { fsBogRepo := buildFileDataRepository(config.File) uaRepo := buildUserAgentRepository(config.Database) - file_service := domain.NewBogFileService(*uaRepo, fsBogRepo) + file_service := domain.NewBogFileService( + uaRepo, fsBogRepo, config.Quota.DefaultSizeBytes(), config.Quota.DefaultDuration(), + ) b.mux = buildHttpMux(file_service) return b } diff --git a/application/configuration.go b/application/configuration.go index a3b7824..a5c02d7 100644 --- a/application/configuration.go +++ b/application/configuration.go @@ -2,28 +2,65 @@ 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 { diff --git a/default.toml b/default.toml index f59008c..c5268e1 100644 --- a/default.toml +++ b/default.toml @@ -1,9 +1,14 @@ +[quota] + default_size = "1GB" + default_duration = "14D" + [server] port = 8002 host = "127.0.0.1" + [file] path = "/tmp/datta2" [database] backend = "sqlite" - connection = "sql.db" \ No newline at end of file + connection = "sql.db" diff --git a/go.mod b/go.mod index 78c3b8e..c2547cd 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/BurntSushi/toml v1.1.0 // indirect + github.com/c2h5oh/datasize v0.0.0-20200825124411-48ed595a09d2 // indirect github.com/mattn/go-sqlite3 v1.14.12 // indirect github.com/spf13/afero v1.8.2 // indirect golang.org/x/text v0.3.4 // indirect diff --git a/go.sum b/go.sum index 9455f43..4ae8b7c 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I= github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/c2h5oh/datasize v0.0.0-20200825124411-48ed595a09d2 h1:t8KYCwSKsOEZBFELI4Pn/phbp38iJ1RRAkDFNin1aak= +github.com/c2h5oh/datasize v0.0.0-20200825124411-48ed595a09d2/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=