forked from vjeantet/bitfan
/
webhook.go
154 lines (129 loc) · 3.62 KB
/
webhook.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package webhook
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/ShowMax/go-fqdn"
"github.com/awillis/bitfan/commons"
"github.com/gosimple/slug"
"github.com/justinas/alice"
)
type webHook struct {
pipelineLabel string
namespace string
Hooks []string
}
type Hook struct {
h http.HandlerFunc
Description string
Namespace string
PipelineUUID string
Url string
}
var webHookMap = new(sync.Map)
var baseURL = ""
var Log commons.Logger
func New(pipelineLabel, nameSpace string) *webHook {
return &webHook{pipelineLabel: pipelineLabel, namespace: nameSpace, Hooks: []string{}}
}
func WebHooks(uuid string) []Hook {
urls := []Hook{}
webHookMap.Range(func(key, value interface{}) bool {
if uuid == value.(*Hook).PipelineUUID {
urls = append(urls, Hook{
Description: value.(*Hook).Description,
Namespace: value.(*Hook).Namespace,
Url: value.(*Hook).Url,
})
}
return true
})
return urls
}
func (w *webHook) buildURL(hookName string) string {
return strings.ToLower("/h/" + slug.Make(w.pipelineLabel) + "/" + slug.Make(hookName))
}
func (w *webHook) buildShortURL(hookName string) string {
return strings.ToLower("/_/" + hookName)
}
// Add a new route to a given http.HandlerFunc
func (w *webHook) AddShort(hookName string, hf http.HandlerFunc) {
hUrl := w.buildShortURL(hookName)
w.Hooks = append(w.Hooks, hookName)
webHookMap.Store(hUrl, &Hook{
h: hf,
Description: hookName,
Namespace: w.namespace,
PipelineUUID: w.pipelineLabel,
Url: hUrl,
})
Log.Infof("Hook [%s - %s] %s", w.pipelineLabel, w.namespace, baseURL+hUrl)
}
// Add a new route to a given http.HandlerFunc
func (w *webHook) Add(hookName string, hf http.HandlerFunc) {
hUrl := w.buildURL(hookName)
w.Hooks = append(w.Hooks, hookName)
webHookMap.Store(hUrl, &Hook{
h: hf,
Description: hookName,
Namespace: w.namespace,
PipelineUUID: w.pipelineLabel,
Url: hUrl,
})
Log.Infof("Hook [%s - %s] %s", w.pipelineLabel, w.namespace, baseURL+hUrl)
}
// Delete a route
func (w *webHook) Delete(hookName string) {
hUrl := w.buildURL(hookName)
webHookMap.Delete(hUrl)
Log.Debugf("WebHook unregisted [%s]", hUrl)
hUrl = w.buildShortURL(hookName)
webHookMap.Delete(hUrl)
Log.Debugf("WebHook unregisted [%s]", hUrl)
}
// Delete all routes belonging to webHook
func (w *webHook) Unregister() {
for _, hookName := range w.Hooks {
w.Delete(hookName)
}
}
func Handler(host string) http.Handler {
addrSpit := strings.Split(host, ":")
if addrSpit[0] == "0.0.0.0" {
addrSpit[0] = fqdn.Get()
}
baseURL = fmt.Sprintf("http://%s:%s", addrSpit[0], addrSpit[1])
commonHandlers := alice.New(loggingHandler, recoverHandler)
return commonHandlers.ThenFunc(routerHandler)
}
func routerHandler(w http.ResponseWriter, r *http.Request) {
hUrl := strings.ToLower(r.URL.Path)
if tHook, ok := webHookMap.Load(hUrl); ok {
Log.Debugf("Webhook found for %s", hUrl)
tHook.(*Hook).h(w, r)
} else {
Log.Warnf("Webhook not found for %s", hUrl)
w.WriteHeader(404)
fmt.Fprint(w, "Not Found !")
}
}
func loggingHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
Log.Debugf("Webhook [%s] %s", r.Method, r.URL.Path)
}
return http.HandlerFunc(fn)
}
func recoverHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
Log.Errorf("Webhook panic [%s] %s : %+v", r.Method, r.URL.Path, err)
http.Error(w, http.StatusText(500), 500)
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}