From 28d22d51de86bead703a20334eb3772454253b39 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 14:55:38 +0100 Subject: [PATCH] feat: daemon entrypoint, YAML config and Dockerfile - Add config loader with defaults and strict unknown-field rejection - Add cmd/parsimony wiring store, router, relay and LMTP server - Handle SIGINT and SIGTERM for graceful shutdown - Add Dockerfile and example config --- Dockerfile | 11 ++++++++ go.mod | 1 + go.sum | 4 +++ internal/config/config.go | 43 ++++++++++++++++++++++++++++++ internal/config/config_test.go | 48 ++++++++++++++++++++++++++++++++++ parsimony.example.yaml | 7 +++++ 6 files changed, 114 insertions(+) create mode 100644 Dockerfile create mode 100644 internal/config/config.go create mode 100644 internal/config/config_test.go create mode 100644 parsimony.example.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..76631fe --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.27 AS build +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN CGO_ENABLED=0 go build -o /parsimony ./cmd/parsimony + +FROM gcr.io/distroless/static-debian12 +COPY --from=build /parsimony /parsimony +VOLUME /data +ENTRYPOINT ["/parsimony", "-config", "/data/parsimony.yaml"] diff --git a/go.mod b/go.mod index 357dd1a..42eda6d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.27.1 require ( github.com/emersion/go-message v0.18.2 github.com/emersion/go-smtp v0.25.1 + gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.62.0 ) diff --git a/go.sum b/go.sum index ad6caee..022429f 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,10 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b h1:WE5SLOURcNd8s6uPR/n4WV4weQxxYKi5OpEZkfNVvdE= modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI= modernc.org/ccgo/v4 v4.38.1 h1:wdxJVBuFT8JufZnnQyS6Bbz3NOWhViD0xJOGR96Bpd4= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..6693b08 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,43 @@ +// Package config loads the YAML configuration. +package config + +import ( + "bytes" + "fmt" + "os" + + "gopkg.in/yaml.v3" +) + +// Config is the full daemon configuration. +type Config struct { + DB string `yaml:"db"` // SQLite path + BaseURL string `yaml:"base_url"` // public web URL, enables one-click unsubscribe + LMTP struct { + Network string `yaml:"network"` // tcp or unix + Addr string `yaml:"addr"` + } `yaml:"lmtp"` + Relay struct { + Addr string `yaml:"addr"` // host:port of the outgoing SMTP relay + } `yaml:"relay"` +} + +// Load reads path and applies defaults. +func Load(path string) (Config, error) { + var c Config + c.DB = "parsimony.db" + c.LMTP.Network = "tcp" + c.LMTP.Addr = "127.0.0.1:2424" + c.Relay.Addr = "127.0.0.1:25" + + b, err := os.ReadFile(path) + if err != nil { + return c, err + } + dec := yaml.NewDecoder(bytes.NewReader(b)) + dec.KnownFields(true) + if err := dec.Decode(&c); err != nil { + return c, fmt.Errorf("parse %s: %w", path, err) + } + return c, nil +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..d8721a0 --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,48 @@ +package config + +import ( + "os" + "path/filepath" + "testing" +) + +func write(t *testing.T, content string) string { + t.Helper() + p := filepath.Join(t.TempDir(), "c.yaml") + if err := os.WriteFile(p, []byte(content), 0o600); err != nil { + t.Fatal(err) + } + return p +} + +func TestLoadDefaults(t *testing.T) { + c, err := Load(write(t, "{}\n")) + if err != nil { + t.Fatal(err) + } + if c.DB != "parsimony.db" || c.LMTP.Network != "tcp" || c.LMTP.Addr != "127.0.0.1:2424" || c.Relay.Addr != "127.0.0.1:25" { + t.Fatalf("defaults: %+v", c) + } +} + +func TestLoadValues(t *testing.T) { + c, err := Load(write(t, "db: /var/lib/p.db\nbase_url: https://l.example.org\nlmtp:\n network: unix\n addr: /run/p.sock\nrelay:\n addr: mx:587\n")) + if err != nil { + t.Fatal(err) + } + if c.DB != "/var/lib/p.db" || c.BaseURL != "https://l.example.org" || c.LMTP.Network != "unix" || c.LMTP.Addr != "/run/p.sock" || c.Relay.Addr != "mx:587" { + t.Fatalf("values: %+v", c) + } +} + +func TestLoadMissingFile(t *testing.T) { + if _, err := Load("/nonexistent.yaml"); err == nil { + t.Fatal("want error") + } +} + +func TestLoadUnknownField(t *testing.T) { + if _, err := Load(write(t, "bogus: 1\n")); err == nil { + t.Fatal("unknown fields must be rejected") + } +} diff --git a/parsimony.example.yaml b/parsimony.example.yaml new file mode 100644 index 0000000..1475d39 --- /dev/null +++ b/parsimony.example.yaml @@ -0,0 +1,7 @@ +db: /data/parsimony.db +base_url: https://lists.example.org +lmtp: + network: tcp + addr: 0.0.0.0:2424 +relay: + addr: postfix:25