-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (91 loc) · 2.59 KB
/
Copy pathmain.go
File metadata and controls
107 lines (91 loc) · 2.59 KB
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
package main
import (
"flag"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/wadeling/linear-webhook/api/linear"
"github.com/wadeling/linear-webhook/pkg/config"
"github.com/wadeling/linear-webhook/pkg/notify"
_ "github.com/wadeling/linear-webhook/pkg/notify/all"
"github.com/wadeling/linear-webhook/pkg/staff"
"github.com/wadeling/linear-webhook/pkg/webhook"
"net/http"
)
func init() {
flag.StringVar(&config.CfgFullPath, "config", "", "config-file-path")
}
func ping() gin.HandlerFunc {
return func(c *gin.Context) {
log.Info().Msg("recv ping")
c.JSON(200, gin.H{
"message": "pong",
"code": 0,
})
}
}
func webhookHandler() gin.HandlerFunc {
return func(c *gin.Context) {
//get header
delivery := c.GetHeader("Linear-Delivery")
event := c.GetHeader("Linear-Event")
userAgent := c.GetHeader("User-Agent")
log.Info().Str("delivery", delivery).Str("event", event).Str("userAgent", userAgent).Msg("recv webhook")
body := webhook.PayLoad{}
err := c.BindJSON(&body)
if err != nil {
log.Err(err).Msg("bad request")
c.JSON(http.StatusBadRequest, gin.H{
"message": err.Error(),
})
return
}
log.Debug().Interface("body", body).Msg("webhook detail")
go func() {
// send notify
_ = notify.DeliverAll(notify.Config{
Url: config.Config.Feishu.WebhookUrl,
LinConfig: linear.Config{Host: config.Config.Linear.ApiAddr, ApiKeys: config.Config.Linear.ApiKeys},
}, body)
}()
c.JSON(http.StatusOK, gin.H{
"message": "ok",
"code": 0,
})
}
}
func main() {
flag.Parse()
// default log level-debug
zerolog.SetGlobalLevel(zerolog.DebugLevel)
// read config file
err := config.InitConfig(config.CfgFullPath)
if err != nil {
log.Err(err).Msg("failed to read config file")
return
}
// show config param
log.Debug().Interface("config", config.Config).Msg("config content")
// load staff config
if len(config.Config.Feishu.StaffFile) > 0 {
if err := staff.Instance().LoadStaffInfoFromFile(config.Config.Feishu.StaffFile); err != nil {
log.Err(err).Msg("load staff config err")
return
}
}
// set router
r := gin.Default()
r.GET("/ping", ping())
r.POST("/webhook", webhookHandler())
if len(config.Config.Server.Https.CertFile) > 0 && len(config.Config.Server.Https.KeyFile) > 0 {
err := r.RunTLS(config.Config.Server.ListenAddr, config.Config.Server.Https.CertFile, config.Config.Server.Https.KeyFile)
if err != nil {
log.Err(err).Msg("https server exit")
}
} else {
err := r.Run(config.Config.Server.ListenAddr)
if err != nil {
log.Err(err).Msg("http server exit")
}
}
}