From 44aee5cb0d2a2365b5640295147c20ce413d232d Mon Sep 17 00:00:00 2001 From: ItsNotGoodName Date: Wed, 13 Sep 2023 14:32:55 -0700 Subject: [PATCH] refactor: retention policy config --- README.md | 12 +++++----- config/config.go | 57 ++++++++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index d8b3475..0dc79cb 100644 --- a/README.md +++ b/README.md @@ -99,12 +99,12 @@ python_executable: python3 # Retention policy for envelopes and attachment files retention: - # # Retention policy for envelopes in database - # envelope_count: # (0, 100, 250, ...) - # envelope_age: # (5m, 5h45m, ...) + # Retention policy for envelopes in database + envelope_count: # (0, 100, 250, ...) + envelope_age: # (5m, 5h45m, ...) - # # Retention policy for attachment files on disk - # attachment_size: # (100 MB, 1 GB, ...) + # Retention policy for attachment files on disk + attachment_size: # (100 MB, 1 GB, ...) # HTTP server http: @@ -276,6 +276,6 @@ make dev-web - feat: read [mbox](https://access.redhat.com/articles/6167512) files - feat: CRUD endpoints - feat: IMAP for viewing mail -- feat: JSON API +- feat: OpenAPI - feat: Windows installer - fix: chrome keeps thinking some HTTP pages are French diff --git a/config/config.go b/config/config.go index 252d3b3..d603f5b 100644 --- a/config/config.go +++ b/config/config.go @@ -55,25 +55,25 @@ type Config struct { } type Raw struct { - Debug bool `koanf:"debug"` - TimeFormat string `koanf:"time_format"` - MaxPayloadSize string `koanf:"max_payload_size"` - DataDirectory string `koanf:"data_directory"` - PythonExecutable string `koanf:"python_executable"` - RetentionEnvelopeCount *int `koanf:"retention.envelope_count"` - RetentionEnvelopeAge *string `koanf:"retention.envelope_age"` - RetentionAttachmentSize *string `koanf:"retention.attachment_size"` - SMTPDisable bool `koanf:"smtp.disable"` - SMTPHost string `koanf:"smtp.host"` - SMTPPort uint16 `koanf:"smtp.port"` - SMTPUsername string `koanf:"smtp.username"` - SMTPPassword string `koanf:"smtp.password"` - HTTPDisable bool `koanf:"http.disable"` - HTTPHost string `koanf:"http.host"` - HTTPPort uint16 `koanf:"http.port"` - HTTPUsername string `koanf:"http.username"` - HTTPPassword string `koanf:"http.password"` - HTTPURL string `koanf:"http.url"` + Debug bool `koanf:"debug"` + TimeFormat string `koanf:"time_format"` + MaxPayloadSize string `koanf:"max_payload_size"` + DataDirectory string `koanf:"data_directory"` + PythonExecutable string `koanf:"python_executable"` + RetentionEnvelopeCount string `koanf:"retention.envelope_count"` + RetentionEnvelopeAge string `koanf:"retention.envelope_age"` + RetentionAttachmentSize string `koanf:"retention.attachment_size"` + SMTPDisable bool `koanf:"smtp.disable"` + SMTPHost string `koanf:"smtp.host"` + SMTPPort uint16 `koanf:"smtp.port"` + SMTPUsername string `koanf:"smtp.username"` + SMTPPassword string `koanf:"smtp.password"` + HTTPDisable bool `koanf:"http.disable"` + HTTPHost string `koanf:"http.host"` + HTTPPort uint16 `koanf:"http.port"` + HTTPUsername string `koanf:"http.username"` + HTTPPassword string `koanf:"http.password"` + HTTPURL string `koanf:"http.url"` // IMAPDisable bool `koanf:"imap.disable"` // IMAPHost string `koanf:"imap.host"` // IMAPPort uint16 `koanf:"imap.port"` @@ -187,17 +187,26 @@ func (p Parser) Parse(raw Raw) (Config, error) { var config *models.Config { + var envelopeCount *int + if raw.RetentionEnvelopeCount != "" { + count, err := strconv.Atoi(raw.RetentionEnvelopeCount) + if err != nil { + return Config{}, err + } + + envelopeCount = &count + } var attachmentsSize *int64 - if raw.RetentionAttachmentSize != nil { - size, err := bytes.Parse(*raw.RetentionAttachmentSize) + if raw.RetentionAttachmentSize != "" { + size, err := bytes.Parse(raw.RetentionAttachmentSize) if err != nil { return Config{}, err } attachmentsSize = &size } var envelopeAge *time.Duration - if raw.RetentionEnvelopeAge != nil { - age, err := time.ParseDuration(*raw.RetentionEnvelopeAge) + if raw.RetentionEnvelopeAge != "" { + age, err := time.ParseDuration(raw.RetentionEnvelopeAge) if err != nil { return Config{}, err } @@ -206,7 +215,7 @@ func (p Parser) Parse(raw Raw) (Config, error) { config = &models.Config{ RetentionPolicy: models.ConfigRetentionPolicy{ - EnvelopeCount: raw.RetentionEnvelopeCount, + EnvelopeCount: envelopeCount, AttachmentSize: attachmentsSize, EnvelopeAge: envelopeAge, MinAge: 5 * time.Minute,