-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (133 loc) · 3.82 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: MIT
// Copyright (c) 2024 Denys Konovalov <[email protected]>
package main
import (
"fmt"
"net/http"
carddav_service "github.com/denyskon/postalion/carddav"
form_service "github.com/denyskon/postalion/form"
mail_service "github.com/denyskon/postalion/mail"
"github.com/denyskon/postalion/version"
"github.com/go-playground/validator/v10"
"github.com/gorilla/pat"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
mail "github.com/xhit/go-simple-mail/v2"
)
type Config struct {
MailAccounts map[string]*mail_service.MailAccount `validate:"dive,required"`
Forms map[string]*form_service.Form `validate:"required,dive,required"`
Port int `validate:"required"`
TemplateDir string `validate:"endsnotwith=/"`
}
var (
log *logrus.Entry
config Config
mailAccounts map[string]*mail.SMTPServer = make(map[string]*mail.SMTPServer)
forms map[string]*form_service.Form = make(map[string]*form_service.Form)
)
func main() {
log = logrus.WithFields(logrus.Fields{
"service": "POSTalion",
})
log.Info("starting up service")
initConfig()
for name, account := range config.MailAccounts {
server, err := account.Init()
if err != nil {
log.Fatal(err)
}
mailAccounts[name] = server
}
for name, form := range config.Forms {
switch form.Type {
case "mail":
{
if mailAccounts[form.MailConfig.Account] == nil {
log.Fatalf("could not find mail account %s for form %s", form.MailConfig.Account, name)
}
form.MailConfig.Template = fmt.Sprintf("%s/%s", config.TemplateDir, form.MailConfig.Template)
}
case "carddav":
{
}
default:
{
log.Fatalf("unknown type %s for form %s", form.Type, name)
}
}
forms[name] = form
}
if len(forms) == 0 {
log.Fatal("no forms defined, exiting...")
}
router := pat.New()
router.Post("/form/{name}", formHandler)
router.Get("/status", func(wr http.ResponseWriter, req *http.Request) {
wr.WriteHeader(http.StatusOK)
wr.Write([]byte(fmt.Sprintf("POSTalion %s", version.Version())))
})
http.Handle("/", router)
log.Infof("listening on port %v", config.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", config.Port), nil))
}
func initConfig() {
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigName("postalion")
v.AddConfigPath(".")
v.AddConfigPath("/etc/postalion")
v.AddConfigPath("/app")
v.SetEnvPrefix("postalion")
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Infoln("config file not found, falling back to evironment variables")
} else {
log.Fatalf("error loading configuration: %v", err)
}
}
err := v.Unmarshal(&config)
if err != nil {
log.Fatal(err)
}
validate := validator.New(validator.WithRequiredStructEnabled())
if err := validate.Struct(config); err != nil {
log.Fatal(err)
}
}
func formHandler(wr http.ResponseWriter, req *http.Request) {
name := req.URL.Query().Get(":name")
form := forms[name]
if form == nil {
wr.WriteHeader(http.StatusNotFound)
wr.Write([]byte(fmt.Sprintf("form with name %s does not exist", name)))
return
} else {
log := log.WithField("form", name)
if form.Honeypot != "" && req.FormValue(form.Honeypot) != "" {
wr.WriteHeader(http.StatusOK)
log.Infof("received bad submission (honeypot = '%s'), ignoring...", req.FormValue(form.Honeypot))
return
}
var err error
switch form.Type {
case "mail":
{
acc := mailAccounts[form.MailConfig.Account]
err = mail_service.HandleForm(form, req, acc)
}
case "carddav":
{
err = carddav_service.HandleForm(form, req)
}
}
if err != nil {
log.Error(err)
wr.WriteHeader(http.StatusInternalServerError)
return
}
}
wr.Header().Add("Location", form.Redirect)
wr.WriteHeader(http.StatusSeeOther)
}