-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
78 lines (63 loc) · 1.69 KB
/
serve.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
package ocrmymail
import (
"fmt"
"os"
"os/signal"
"runtime/debug"
"syscall"
"github.com/evalphobia/logrus_sentry"
"github.com/gopistolet/smtp/mta"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
const tmpDir = "./tmp/"
// Serve runs the actual SMTP server and handles all the mails
func (o *OCRMyMail) Serve() {
// Validate config
err := o.config.Validate()
if err != nil {
log.Fatalf("Config file is not valid: %v", err)
}
// Error logging with Sentry
if o.config.SentryDSN != "" {
hook, err := logrus_sentry.NewSentryHook(o.config.SentryDSN, []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
})
hook.StacktraceConfiguration.Enable = true
if err == nil {
log.AddHook(hook)
}
defer func() {
if err := recover(); err != nil {
log.WithField("stacktrace", string(debug.Stack())).Fatalf("panic: %s", err)
}
}()
}
log.WithField("config", fmt.Sprintf("%+v", o.config)).Println("Starting PDF OCR SMTP Gateway ✉️")
if !IsOCRMyPDFInstalled() {
log.Fatalln("OCRmyPDF is not installed. Please install the command: https://github.com/jbarlow83/OCRmyPDF")
}
if o.config.HandleAsync {
log.Warnln("Async mail handling is enabled. Mails might 'disappear' silently on crashes or errors.")
}
// Configure and start GoPistolet SMTP server
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
// Default config
smtpConfig := mta.Config{
Hostname: "localhost",
Port: 25,
}
// create new MTA with SMTP config and OCRMyMail as the email handler
mta := mta.NewDefault(smtpConfig, o)
go func() {
<-sigc
mta.Stop()
}()
err = mta.ListenAndServe()
if err != nil {
log.Fatalln(err)
}
}