-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
97 lines (87 loc) · 3.05 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"embed"
"fmt"
"os"
"strconv"
"strings"
. "github.com/mjarkk/mini-mail-dev/go"
"github.com/spf13/pflag"
)
//go:embed dist
var dist embed.FS
func getenv(key string) string {
return strings.TrimSpace(os.Getenv(key))
}
func main() {
ifEmptyNotRequired := ", if empty no credentials required"
defaultMaxEmails := uint16(200)
defaultSMTPAddr := "0.0.0.0:1025"
defaultHTTPAddr := "0.0.0.0:1080"
defaultSMPTDomain := "localhost"
argMaxEmails := pflag.Uint16("max-emails", defaultMaxEmails, "The max amount of emails to keep")
argSMTPAddr := pflag.String("smtp", defaultSMTPAddr, "SMTP server address")
argHTTPAddr := pflag.String("http", defaultHTTPAddr, "HTTP server address")
argSMTPDomain := pflag.String("smtp-domain", defaultSMPTDomain, "SMTP server domain")
argSMTPUsername := pflag.String("smtp-incoming-user", "", "SMTP server username"+ifEmptyNotRequired)
argSMTPPassword := pflag.String("smtp-incoming-pass", "", "SMTP server password"+ifEmptyNotRequired)
argHTTPUsername := pflag.String("http-user", "", "HTTP server username"+ifEmptyNotRequired)
argHTTPPassword := pflag.String("http-pass", "", "HTTP server address"+ifEmptyNotRequired)
argDisableWeb := pflag.Bool("disable-web", false, "Disable the web interface")
pflag.Parse()
envMaxEmails := getenv("MAX_EMAILS")
envSMTPAddr := getenv("SMTP_ADDR")
envHTTPAddr := getenv("HTTP_ADDR")
envSMTPDomain := getenv("SMTP_DOMAIN")
envSMTPUsername := getenv("SMTP_INCOMING_USER")
envSMTPPassword := getenv("SMTP_INCOMING_PASS")
envHTTPUsername := getenv("HTTP_USER")
envHTTPPassword := getenv("HTTP_PASS")
envDisableWeb := strings.ToLower(getenv("DISABLE_WEB")) == "true"
if *argMaxEmails == defaultMaxEmails && envMaxEmails != "" {
parsedEnvMaxEmails, err := strconv.Atoi(envMaxEmails)
if err == nil && parsedEnvMaxEmails >= 0 && parsedEnvMaxEmails < 65535 {
*argMaxEmails = uint16(parsedEnvMaxEmails)
}
}
if *argSMTPAddr == defaultSMTPAddr && envSMTPAddr != "" {
*argSMTPAddr = envSMTPAddr
}
if *argHTTPAddr == defaultHTTPAddr && envHTTPAddr != "" {
*argHTTPAddr = envHTTPAddr
}
if *argSMTPDomain == defaultSMPTDomain && envSMTPDomain != "" {
*argSMTPDomain = envSMTPDomain
}
if *argSMTPUsername == "" && envSMTPUsername != "" {
*argSMTPUsername = envSMTPUsername
}
if *argSMTPPassword == "" && envSMTPPassword != "" {
*argSMTPPassword = envSMTPPassword
}
if *argHTTPUsername == "" && envHTTPUsername != "" {
*argHTTPUsername = envHTTPUsername
}
if *argHTTPPassword == "" && envHTTPPassword != "" {
*argHTTPPassword = envHTTPPassword
}
if !*argDisableWeb && envDisableWeb {
*argDisableWeb = envDisableWeb
}
if *argDisableWeb {
fmt.Println("Not starting webserver as --disable-web is set")
} else {
go StartWebserver(dist, StartWebserverOptions{
Addr: *argHTTPAddr,
BasicAuthUsername: *argHTTPUsername,
BasicAuthPassword: *argHTTPPassword,
})
}
StartEmailServer(StartEmailServerOptions{
Addr: *argSMTPAddr,
Domain: *argSMTPDomain,
Username: *argSMTPUsername,
Password: *argSMTPPassword,
MaxEmails: *argMaxEmails,
})
}