-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (70 loc) · 2.34 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"crypto/tls"
"fmt"
"github.com/awakari/int-email/api/http/pub"
apiSmtp "github.com/awakari/int-email/api/smtp"
"github.com/awakari/int-email/config"
"github.com/awakari/int-email/service"
"github.com/awakari/int-email/service/converter"
"github.com/awakari/int-email/util"
"github.com/emersion/go-smtp"
"log/slog"
"net/http"
"os"
)
func main() {
// init config
cfg, err := config.NewConfigFromEnv()
if err != nil {
panic(fmt.Sprintf("failed to load the config from env: %s", err))
}
// logger
opts := slog.HandlerOptions{
Level: slog.Level(cfg.Log.Level),
}
log := slog.New(slog.NewTextHandler(os.Stdout, &opts))
log.Info("starting the update for the feeds")
svcPub := pub.NewService(http.DefaultClient, cfg.Api.Writer.Uri, cfg.Api.Token.Internal)
svcPub = pub.NewLogging(svcPub, log)
rcptsPublish := map[string]bool{}
for _, name := range cfg.Api.Smtp.Recipients.Publish {
rcptsPublish[name] = true
}
svcConv := converter.NewConverter(cfg.Api.EventType.Self, util.HtmlPolicy(), cfg.Api.Writer.Internal, rcptsPublish, cfg.Api.Smtp.Data.TruncUrlQueries)
svcConv = converter.NewLogging(svcConv, log)
svc := service.NewService(svcConv, svcPub, cfg.Api.Group, cfg.Api.Writer.Backoff)
svc = service.NewLogging(svc, log)
rcptsInternal := map[string]bool{}
for _, name := range cfg.Api.Smtp.Recipients.Internal {
rcptsInternal[name] = true
}
b := apiSmtp.NewBackend(rcptsPublish, rcptsInternal, int64(cfg.Api.Smtp.Data.Limit), svc)
b = apiSmtp.NewBackendLogging(b, log)
srv := smtp.NewServer(b)
srv.Addr = fmt.Sprintf(":%d", cfg.Api.Smtp.Port)
srv.Domain = cfg.Api.Smtp.Host
srv.MaxMessageBytes = int64(cfg.Api.Smtp.Data.Limit)
srv.MaxRecipients = int(cfg.Api.Smtp.Recipients.Limit)
srv.ReadTimeout = cfg.Api.Smtp.Timeout.Read
srv.WriteTimeout = cfg.Api.Smtp.Timeout.Write
srv.AllowInsecureAuth = false
srv.EnableREQUIRETLS = true
// Load the TLS certificate and key from the mounted volume
var cert tls.Certificate
cert, err = tls.LoadX509KeyPair(cfg.Api.Smtp.Tls.CertPath, cfg.Api.Smtp.Tls.KeyPath)
if err != nil {
panic(err)
}
srv.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{
cert,
},
ClientAuth: cfg.Api.Smtp.Tls.ClientAuthType,
MinVersion: cfg.Api.Smtp.Tls.VersionMin,
}
log.Info("starting to listen for emails...")
if err = srv.ListenAndServe(); err != nil {
panic(err)
}
}