-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
91 lines (78 loc) · 2.17 KB
/
main.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
package main
import (
"github.com/PhysicsEngine/huawei-alert-server/config"
"github.com/PhysicsEngine/huawei-alert-server/matcher"
"github.com/PhysicsEngine/huawei-alert-server/notification"
"github.com/gin-gonic/gin"
_ "github.com/heroku/x/hmetrics/onload"
"go.uber.org/zap"
"log"
"net/http"
"os"
)
type Request struct {
macAddresses []string `json:"mac_addresses" binding:"required"`
notification string `json:"notification" binding:"required"`
deviceId string `json:"device_id" binding:"required"`
}
func main() {
// setup logger
zapLogger, _ := zap.NewProduction()
logger := zapLogger.Sugar()
defer func() {
err := zapLogger.Sync()
log.Fatal(err)
}()
env, err := config.ReadFromEnv()
if err != nil {
logger.Errorf("Failed to read env vars: %s", err)
os.Exit(1)
}
handler := notification.CreateHandler(logger, env)
port := env.Port
router := gin.New()
router.Use(gin.Logger())
router.LoadHTMLGlob("templates/*.tmpl.html")
router.Static("/static", "static")
matcher, err := matcher.CreateHuaweiMatcher(logger, "matcher")
if err != nil {
logger.Errorf("Failed to create HuaweiMatcher: %s", err)
os.Exit(1)
}
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl.html", nil)
})
router.POST("/api/notification", func(c *gin.Context) {
// TODO: Call plugin with parameter
var req Request
// Restore the io.ReadCloser to its original state
if err := c.ShouldBindJSON(&req); err != nil {
logger.Errorf("get error request:: %s, err::%s", req, err)
// mac address can't be found
c.JSON(400, gin.H{"status": "Invalid Request"})
return
}
isMatched := false
for _, addr := range req.macAddresses {
logger.Infof("%s found", addr)
if matcher.Match(addr) {
isMatched = true
break
}
}
if isMatched {
notify := req.notification
if handler.Contains(notify) {
handler.Send(notify)
c.JSON(200, gin.H{"status": "send notification to slack"})
return
} else {
logger.Errorf("not defined notification channel")
c.JSON(400, gin.H{"status": "notfication channel not found"})
return
}
}
c.JSON(200, gin.H{"status": "target device not found"})
})
router.Run(":" + port)
}