Handle default quotas in config and application

master
Caj Larsson 3 years ago
parent 0a569805ea
commit 9eca063c9f

@ -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
}

@ -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 {

@ -1,6 +1,11 @@
[quota]
default_size = "1GB"
default_duration = "14D"
[server]
port = 8002
host = "127.0.0.1"
[file]
path = "/tmp/datta2"

@ -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

@ -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=

Loading…
Cancel
Save