-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaildoor.go
189 lines (154 loc) · 4.22 KB
/
maildoor.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package maildoor
import (
"bytes"
"embed"
"html/template"
"io"
"log/slog"
"net/http"
"path"
"strings"
"time"
)
var (
//go:embed *.html *.txt
templates embed.FS
//go:embed *.png
assets embed.FS
)
// attempt is a struct to hold the email and error message.
// used across different views.
type atempt struct {
Logo string
Icon string
ProductName string
Email string
Error string
Code string
}
// New maildoor handler with the passed options.
func New(options ...option) http.Handler {
s := &maildoor{
mux: http.NewServeMux(),
productName: "Maildoor",
logoURL: "https://raw.githubusercontent.com/wawandco/maildoor/508ff43/assets/images/maildoor_logo.png",
iconURL: "https://raw.githubusercontent.com/wawandco/maildoor/508ff43/assets/images/maildoor_icon.png",
afterLogin: func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Logged in!"))
},
emailValidator: func(email string) error {
// All emails are valid by default
return nil
},
logout: func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
},
}
for _, opt := range options {
opt(s)
}
s.HandleFunc("GET /login", s.handleLogin)
s.HandleFunc("POST /email", s.handleEmail)
s.HandleFunc("POST /code", s.handleCode)
s.HandleFunc("DELETE /logout", s.handleLogout)
// Adding the static assets handler
ah := http.StripPrefix(s.patternPrefix, http.FileServer(http.FS(assets)))
s.Handle("GET /*", ah)
return s
}
type maildoor struct {
mux *http.ServeMux
productName string
logoURL string
iconURL string
patternPrefix string
afterLogin http.HandlerFunc
logout http.HandlerFunc
emailValidator func(email string) error
emailSender func(email, html, txt string) error
}
func (m *maildoor) HandleFunc(pattern string, handler http.HandlerFunc) {
// prefix the pattens with the routesPrefix
parts := strings.Split(pattern, " ")
pattern = path.Join(m.patternPrefix, parts[0])
if len(parts) == 2 {
pattern = path.Join(m.patternPrefix, parts[1])
pattern = parts[0] + " " + pattern
}
// Adding the handler to the ServeMux
m.mux.HandleFunc(pattern, handler)
}
func (m *maildoor) Handle(pattern string, handler http.Handler) {
// prefix the pattens with the routesPrefix
parts := strings.Split(pattern, " ")
pattern = path.Join(m.patternPrefix, parts[0])
if len(parts) == 2 {
pattern = path.Join(m.patternPrefix, parts[1])
pattern = parts[0] + " " + pattern
}
// Adding the handler to the ServeMux
m.mux.Handle(pattern, handler)
}
func (m *maildoor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Adding common things here, loggers and other things.
t := time.Now()
// Parsing form
err := r.ParseForm()
if err != nil {
m.httpError(w, err)
return
}
// Correcting method based on _method field
if r.Method == "POST" && r.FormValue("_method") != "" {
r.Method = r.FormValue("_method")
}
m.mux.ServeHTTP(w, r)
slog.Info(">", "method", r.Method, "path", r.URL.Path, "duration", time.Since(t))
}
// render a template with the passed data and partials using
// the templates FS. if using layout it should go first.
func (m *maildoor) render(w io.Writer, data any, partials ...string) error {
if len(partials) == 0 {
return nil
}
tt := template.New(partials[0]).Funcs(template.FuncMap{
"prefixedPath": func(p string) string {
return path.Join(m.patternPrefix, p)
},
})
tt, err := tt.ParseFS(templates, partials...)
if err != nil {
return err
}
return tt.Execute(w, data)
}
func (m *maildoor) httpError(w http.ResponseWriter, err error) {
slog.Error("*", "error", err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
func (m *maildoor) mailBodies(code string) (string, string, error) {
data := struct {
Code string
Logo string
Product string
Year string
}{
Code: code,
Logo: m.logoURL,
Product: m.productName,
Year: time.Now().Format("2006"),
}
sw := bytes.NewBuffer([]byte{})
err := m.render(sw, data, "message.html")
if err != nil {
return "", "", err
}
html := sw.String()
sw = bytes.NewBuffer([]byte{})
err = m.render(sw, data, "message.txt")
if err != nil {
return "", "", err
}
txt := sw.String()
return html, txt, nil
}