/
handler.go
43 lines (35 loc) · 1003 Bytes
/
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
package grafana
import (
"fmt"
"net/http"
api "github.com/YeHeng/push-webhook/api"
"github.com/YeHeng/push-webhook/app"
"github.com/gin-gonic/gin"
)
func routeHandler(c *gin.Context) {
transformer, e := api.GetTransformer(Grafana)
if e != nil {
app.Logger.Errorf("%v", e)
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("%v", e), "Code": -1})
return
}
msg, e := transformer.Transform(c)
if e != nil {
app.Logger.Errorf("%v", e)
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("%v", e), "Code": -1})
return
}
pushService, e := api.GetPushStrategy(msg.PushChannel)
if e != nil {
app.Logger.Errorf("%v", e)
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("%v", e), "Code": -1})
return
}
result, e := pushService.Push(msg)
if e != nil {
app.Logger.Errorf("%v", e)
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("推送失败,原因:%v", e), "Code": -1})
return
}
c.JSON(http.StatusOK, gin.H{"message": result.Message, "Code": result.Code})
}