-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandler.go
51 lines (42 loc) · 1.13 KB
/
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
44
45
46
47
48
49
50
51
package main
import (
"encoding/json"
"net/http"
)
type EventHandler struct {
channel chan<- PullRequestEvent
}
func (e EventHandler) Handle() http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var event PullRequestEvent
err := json.NewDecoder(request.Body).Decode(&event)
if err != nil || event.PullRequest == nil {
writer.WriteHeader(http.StatusBadRequest)
return
}
// take only merged state
if event.PullRequest.State != Merged {
writer.WriteHeader(http.StatusUnprocessableEntity)
return
}
// notify the channel
select {
case e.channel <- event:
writer.WriteHeader(http.StatusCreated)
default:
writer.WriteHeader(http.StatusTooManyRequests)
}
})
}
func (e EventHandler) CheckToken(token string, next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if token != request.URL.Query().Get("token") {
writer.WriteHeader(http.StatusForbidden)
return
}
next.ServeHTTP(writer, request)
})
}
func NewEventHandler(c chan PullRequestEvent) *EventHandler {
return &EventHandler{channel: c}
}