Skip to content

Commit

Permalink
Add printer driver for debug purposes (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
teran committed Jun 18, 2024
1 parent e46f833 commit 945e7be
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
46 changes: 41 additions & 5 deletions cmd/relay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import (
"time"

smtp "github.com/emersion/go-smtp"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/kelseyhightower/envconfig"
mg "github.com/mailgun/mailgun-go/v4"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

"github.com/teran/relay/driver"
"github.com/teran/relay/driver/mailgun"
"github.com/teran/relay/driver/printer"
smtpWrapper "github.com/teran/relay/smtp"
)

Expand All @@ -24,20 +29,34 @@ var (
type config struct {
LogLevel log.Level `envconfig:"LOG_LEVEL" default:"INFO"`
Addr string `envconfig:"ADDR" default:":25"`
Driver string `envconfig:"DRIVER" required:"true"`
AllowInsecureAuth bool `envconfig:"ALLOW_INSECURE_AUTH"`
AuthDisabled bool `envconfig:"AUTH_DISABLED"`
Domain string `envconfig:"DOMAIN" required:"true"`
MailgunAPIKey string `envconfig:"MAILGUN_API_KEY" required:"true"`
MailgunURL string `envconfig:"MAILGUN_URL" required:"true"`
MailgunAPIKey string `envconfig:"MAILGUN_API_KEY"`
MailgunURL string `envconfig:"MAILGUN_URL"`
MaxMessageBytes int64 `default:"1048576" envconfig:"MAX_MESSAGE_BYTES"`
MaxRecipients int `default:"50" envconfig:"MAX_RECIPIENTS"`
MetricsAddr string `envconfig:"METRICS_ADDR" default:":8081" `
}

func (c config) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.Driver, validation.Required, validation.In("mailgun", "printer")),
validation.Field(&c.Domain, validation.Required, is.Domain),
validation.Field(&c.MailgunAPIKey, validation.When(c.Driver == "mailgun", validation.Required)),
validation.Field(&c.MailgunURL, validation.When(c.Driver == "mailgun", validation.Required, is.URL)),
)
}

func main() {
var cfg config
envconfig.MustProcess("RELAY", &cfg)

if err := cfg.Validate(); err != nil {
panic(err)
}

ctx := context.Background()

log.SetLevel(cfg.LogLevel)
Expand All @@ -51,10 +70,11 @@ func main() {
"build_timestamp": buildTimestamp,
}).Infof("initializing application")

mgCli := mg.NewMailgun(cfg.Domain, cfg.MailgunAPIKey)
mgCli.SetAPIBase(cfg.MailgunURL)
dr, err := newDriver(cfg)
if err != nil {
panic(err)
}

dr := mailgun.New(mgCli)
be := smtpWrapper.NewBackend(ctx, dr)

s := smtp.NewServer(be)
Expand Down Expand Up @@ -82,3 +102,19 @@ func main() {
panic(err)
}
}

func newDriver(cfg config) (driver.Driver, error) {
log.Tracef("mailing driver `%s` requested ...", cfg.Driver)

switch cfg.Driver {
case "mailgun":
mgCli := mg.NewMailgun(cfg.Domain, cfg.MailgunAPIKey)
mgCli.SetAPIBase(cfg.MailgunURL)

return mailgun.New(mgCli), nil
case "printer":
return printer.New(), nil
default:
return nil, errors.Errorf("unexpected driver: `%s`", cfg.Driver)
}
}
2 changes: 2 additions & 0 deletions driver/mailgun/mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/teran/relay/driver"
)

var _ driver.Driver = (*mailgun)(nil)

type MailgunImpl interface {
NewMIMEMessage(body io.ReadCloser, to ...string) *mg.Message
Send(ctx context.Context, m *mg.Message) (string, string, error)
Expand Down
33 changes: 33 additions & 0 deletions driver/printer/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package printer

import (
"context"
"io"

log "github.com/sirupsen/logrus"
"github.com/teran/relay/driver"
)

var _ driver.Driver = (*printer)(nil)

type printer struct{}

func New() driver.Driver {
return &printer{}
}

func (u *printer) Send(ctx context.Context, from string, to []string, r io.Reader) error {
message, err := io.ReadAll(r)
if err != nil {
return err
}

log.WithFields(log.Fields{
"driver": "printer",
"from": from,
"to": to,
"message": string(message),
}).Infof("Fake sending email")

return nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.4

require (
github.com/emersion/go-smtp v0.21.2
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/mailgun/mailgun-go/v4 v4.12.0
github.com/pkg/errors v0.9.1
Expand All @@ -14,6 +15,7 @@ require (
)

require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand All @@ -20,6 +22,8 @@ github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpm
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -61,6 +65,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
Expand All @@ -74,6 +79,7 @@ google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 945e7be

Please sign in to comment.