forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
42 lines (36 loc) · 981 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
package hooks
import (
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/json"
"net/http"
)
type WebhookHandler struct {
}
func New(management *config.ScaledContext) *WebhookHandler {
webhookHandler := &WebhookHandler{}
webhookHandler.initDrivers(management)
return webhookHandler
}
func (h *WebhookHandler) initDrivers(management *config.ScaledContext) {
RegisterDrivers(management)
}
func (h *WebhookHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
for key, driver := range Drivers {
if exist := req.Header.Get(key); exist != "" {
code, err := driver.Execute(req)
if err != nil {
e := map[string]interface{}{
"type": "error",
"code": code,
"message": err.Error(),
}
logrus.Errorf("executing %s driver got error: %v", key, err)
rw.WriteHeader(code)
responseBody, _ := json.Marshal(e)
rw.Write(responseBody)
}
rw.WriteHeader(http.StatusOK)
}
}
}