-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
56 lines (48 loc) · 1.18 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
51
52
53
54
55
56
package main
import (
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func healthHandler(c *gin.Context) {
c.String(http.StatusOK, "OK")
}
func defaultHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"release": release,
"request_on": time.Now(),
"request_from": c.Request.RemoteAddr,
})
}
func notifHandler(c *gin.Context) {
var data []byte
data, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
logger.Printf("error capturing notification: %v", err)
}
logger.Println(string(data))
// doing token after data for debugging
token := strings.TrimSpace(c.Query("token"))
if token != accessToken {
logger.Printf("invalid access token. Got:%s Want:%s", token, accessToken)
c.JSON(http.StatusUnauthorized, gin.H{
"message": "Invalid access token",
"status": "Unauthorized",
})
return
}
if e := publish(c.Request.Context(), data); e != nil {
logger.Printf("error publishing notification: %v", e)
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Error handling notification",
"status": "Failure",
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "Notification proccessed",
"status": "OK",
})
}