-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.go
50 lines (41 loc) · 1.17 KB
/
handler.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
package main
import (
"encoding/json"
"net/http"
)
func requestHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
var c pushedContent
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
logger.Printf("Error decoding message: %v", err)
writeResp(w, http.StatusBadRequest, "Invalid Content")
return
}
logger.Printf("Content from PubSub subscription: %v", c.Subscription)
m := &CloudBuildNotification{}
if err := json.Unmarshal(c.Message.Data, &m); err != nil {
logger.Printf("Error decoding message data: %v", err)
writeResp(w, http.StatusBadRequest, "Invalid Content")
return
}
logger.Printf("Message content: %v", m)
if err := send(m); err != nil {
logger.Printf("Failed to send notification %v", err)
writeResp(w, http.StatusInternalServerError, "Internal Error")
return
}
writeResp(w, http.StatusOK, "OK")
return
}
func writeResp(w http.ResponseWriter, status int, msg string) {
w.WriteHeader(status)
json.NewEncoder(w).Encode(msg)
}
type pushedContent struct {
Message struct {
Attributes map[string]string
Data []byte
ID string `json:"message_id"`
}
Subscription string
}