-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (74 loc) · 2.78 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
package main
import (
"flag"
"main/components/platform/encryption"
"main/components/platform/postgresmanager"
"main/components/scheduler"
"main/components/user"
"main/confidential"
"main/server"
"os"
"time"
"github.com/golang-jwt/jwt"
"github.com/pterm/pterm"
)
type jwtCustomClaims struct {
Value string
jwt.StandardClaims
}
func main() {
banner()
dbuserPtr := flag.String("dbuser", "postgres", "PostgreSQL Database user")
dbpassPtr := flag.String("dbpass", "password", "PostgreSQL Database password")
dbnamePtr := flag.String("dbname", "postgres", "PostgreSQL Database name")
portPtr := flag.String("httpport", "8080", "Port to run the server on")
sendingEmailPtr := flag.String("email", "", "Email to send messages from")
sendingEmailPasswordPtr := flag.String("pass", "", "Password for the sending email")
smtpServerPtr := flag.String("domain", "", "SMTP server to send messages from")
smtpPortPtr := flag.Int("smtpport", 0, "SMTP port to send messages from")
loggerPtr := flag.Bool("log", false, "Enable HTTP request logging")
signingKeyPtr := flag.String("signingkey", "", "Signing key for JWT")
encryptionKeyPtr := flag.String("encryptionkey", "", "Encryption key for password encryption")
flag.Parse()
if *sendingEmailPtr == "" || *sendingEmailPasswordPtr == "" || *smtpServerPtr == "" || *smtpPortPtr == 0 || *signingKeyPtr == "" || *encryptionKeyPtr == "" {
pterm.Error.Println("Please provide all the required parameters: email, password, smtp domain, smtp port, signing key, encryption key")
os.Exit(1)
}
confidential.SigningKey = []byte(*signingKeyPtr)
confidential.EncryptionKey = []byte(*encryptionKeyPtr)
encryption.Encrypt("test")
sclaims := jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Second * 30).Unix(),
}
claims := &jwtCustomClaims{
Value: "test",
StandardClaims: sclaims,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
_, err := token.SignedString(confidential.SigningKey)
if err != nil {
pterm.Error.Println("Error signing token: ", err)
os.Exit(1)
}
err = postgresmanager.Open(*dbnamePtr, *dbuserPtr, *dbpassPtr)
if err != nil {
pterm.Error.Println("Error opening database: ", err)
os.Exit(1)
}
err = postgresmanager.AutoCreateStruct(&user.User{})
if err != nil {
pterm.Error.Println("Error creating user table: ", err)
os.Exit(1)
}
user.SendingEmail = *sendingEmailPtr
user.SendingPassword = *sendingEmailPasswordPtr
user.SMTPServer = *smtpServerPtr
user.SMTPPort = *smtpPortPtr
scheduler.Cleanup()
scheduler.ScheduleTasks()
server.Start(*portPtr, *loggerPtr)
}
func banner() {
pterm.DefaultCenter.Print(pterm.DefaultHeader.WithFullWidth().WithBackgroundStyle(pterm.NewStyle(pterm.BgLightBlue)).WithMargin(10).Sprint("MailDruid"))
pterm.Info.Println("Made by Akhil Datla and Alexander Ott")
}