forked from teran/relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay.go
57 lines (47 loc) · 1.47 KB
/
relay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"log"
smtp "github.com/emersion/go-smtp"
"github.com/kelseyhightower/envconfig"
"github.com/dapperlabs/relay/backend/mailgun"
)
type config struct {
Addr string `default:":25"`
AllowInsecureAuth bool `default:"true" envconfig:"ALLOW_INSECURE_AUTH"`
AuthDisabled bool `default:"true" envconfig:"AUTH_DISABLED"`
Domain string `required:"true" envconfig:"DOMAIN"`
MailgunPrivateKey string `required:"true" envconfig:"MAILGUN_PRIVATE_KEY"`
MaxIdleSeconds int `default:"300" envconfig:"MAX_IDLE_SECONDS"`
MaxMessageBytes int `default:"1048576" envconfig:"MAX_MESSAGE_BYTES"`
MaxRecipients int `default:"50" envconfig:"MAX_RECIPIENTS"`
MetricsAddr string `default:":8081" envconfig:"METRICS_ADDR"`
}
func main() {
var cfg config
err := envconfig.Process("RELAY", &cfg)
if err != nil {
log.Fatal(err)
}
be, err := mailgun.NewBackend(
cfg.Domain,
cfg.MailgunPrivateKey,
)
if err != nil {
log.Fatal(err)
}
s := smtp.NewServer(be)
s.Addr = cfg.Addr
s.Domain = cfg.Domain
s.MaxIdleSeconds = cfg.MaxIdleSeconds
s.MaxMessageBytes = cfg.MaxMessageBytes
s.MaxRecipients = cfg.MaxRecipients
s.AuthDisabled = cfg.AuthDisabled
s.AllowInsecureAuth = cfg.AllowInsecureAuth
go func(metricsAddr string) {
log.Fatal(be.(*mailgun.Backend).ListenAndServeMetrics(metricsAddr))
}(cfg.MetricsAddr)
log.Println("Starting server at", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
}