-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
147 lines (124 loc) · 3.94 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
package web
import (
"encoding/json"
"github.com/JaKu01/GoNotify/internal"
"html/template"
"io"
"log"
"net/http"
"os"
)
func handleIndex(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
generateAndSendResponse(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if r.URL.Path != "/" {
// Serve static file
handleStatic(w, r)
return
}
// Parse and execute template
tmpl, err := template.ParseFiles("./template/index.html")
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Template parsing error: %v", err)
return
}
// Execute the template with the single string
err = tmpl.Execute(w, internal.VapidPublicKey)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Template execution error: %v", err)
}
}
func handleStatic(w http.ResponseWriter, r *http.Request) {
// Serve static file
filePath := "./static" + r.URL.Path
if _, err := os.Stat(filePath); err == nil {
http.ServeFile(w, r, filePath)
return
} else {
// File not found, return 404
generateAndSendResponse(w, "The requested static file was not found.", http.StatusNotFound)
return
}
}
func handleSubscribe(w http.ResponseWriter, r *http.Request) {
// extract body from request as string
subscription, err := io.ReadAll(r.Body)
if err != nil {
generateAndSendResponse(w, "Invalid request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
err = internal.SaveSubscription(subscription)
if err != nil {
generateAndSendResponse(w, "An error occurred while saving the subscription", http.StatusInternalServerError)
return
}
generateAndSendResponse(w, "Subscription saved successfully", http.StatusOK)
}
func handleDeleteSubscribe(w http.ResponseWriter, r *http.Request) {
// extract body from request as string
var req internal.WebPushUnsubscriptionRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
generateAndSendResponse(w, "Invalid request body", http.StatusBadRequest)
return
}
err = internal.RemoveSubscription(req)
if err != nil {
generateAndSendResponse(w, "An error occurred while deleting the subscription", http.StatusInternalServerError)
return
}
generateAndSendResponse(w, "Subscription saved successfully", http.StatusOK)
}
func handleWebPush(w http.ResponseWriter, r *http.Request) {
var req internal.NotificationRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
generateAndSendResponse(w, "Invalid request body", http.StatusBadRequest)
return
}
err = internal.SendToAllSubscribers(req)
if err != nil {
generateAndSendResponse(w, "Failed to send web push notifications", http.StatusInternalServerError)
return
}
generateAndSendResponse(w, "Web push notifications sent", http.StatusOK)
}
func handleMail(w http.ResponseWriter, r *http.Request) {
// read the request body
var req internal.NotificationRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
generateAndSendResponse(w, "Invalid request body", http.StatusBadRequest)
return
}
// send the email
err = internal.SendMail(req)
if err != nil {
generateAndSendResponse(w, "Failed to send email", http.StatusInternalServerError)
return
}
generateAndSendResponse(w, "Email sent successfully", http.StatusOK)
}
func handleAll(w http.ResponseWriter, r *http.Request) {
generateAndSendResponse(w, "Not yet implemented", http.StatusNotImplemented)
}
func generateAndSendResponse(w http.ResponseWriter, message string, statusCode int) {
response := internal.NotificationResponse{Message: message}
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Printf("Error encoding JSON: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
_, err = w.Write(jsonResponse)
if err != nil {
log.Printf("Error: failed to write response %v", err)
}
}