-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.go
169 lines (143 loc) · 3.19 KB
/
handlers.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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"net/http"
)
func ConfirmHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
vars := mux.Vars(r)
key := vars["id"]
user, err := FindUserKey(key)
if err != nil || user == nil {
sendError(w, notFound)
return
}
if user.Key == key && user.Confirmed {
response := requestResponse{
Email: user.Email,
Message: "email address has already been confirmed",
}
sendJSON(w, response)
return
}
if user.Key == key {
user.Confirmed = true
user.Update()
}
response := requestResponse{
Email: user.Email,
Message: "email address is now confirmed",
}
confirmations++
sendJSON(w, response)
}
func UnsubscribeHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
email := r.URL.Query().Get("email")
user, err := FindUser(email)
if err != nil || user == nil {
sendError(w, notFound)
return
}
user.Delete()
status := &requestResponse{
Email: user.Email,
Message: "email has been unsubscribed",
}
unsubscribed++
sendJSON(w, status)
}
func ResendHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
email := r.URL.Query().Get("email")
user, err := FindUser(email)
if err != nil || user == nil {
sendError(w, notFound)
return
}
status := &requestResponse{
Email: user.Email,
}
if user.Confirmed {
status.Message = "email has already been confirmed"
} else {
status.Message = "email has not been confirmed yet"
}
sendJSON(w, status)
}
func CheckHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
email := r.URL.Query().Get("email")
user, err := FindUser(email)
if err != nil || user == nil {
sendError(w, notFound)
return
}
status := &requestResponse{
Email: user.Email,
}
if user.Confirmed {
status.Message = "email has been confirmed"
} else {
status.Message = "email has not been confirmed yet " + user.Key
}
sendJSON(w, status)
}
func SendHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
user, err := FindUser("info@socialeck.com")
if err != nil || user == nil {
sendError(w, notFound)
return
}
err = SendEmail(user)
if err != nil {
emailErrors++
sendError(w, err)
return
}
emailsSent++
}
func RequestHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var req requestJSON
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&req); err != nil {
sendError(w, err)
return
}
user, err := FindUser(req.Email)
if err != nil {
sendError(w, notFound)
return
}
if user != nil {
response := &requestResponse{
Email: user.Email,
Message: "this email need to be confirmed",
}
sendJSON(w, response)
return
}
user = &User{
Email: req.Email,
Key: RandomString(32),
Sent: 0,
}
user.Create()
response := &requestResponse{
Email: user.Email,
Message: "check email",
}
requests++
sendJSON(w, response)
}
func sendJSON(w http.ResponseWriter, obj interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(obj)
}
func sendError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&errorResponse{err.Error()})
}