-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
171 lines (144 loc) · 4.06 KB
/
app.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package plexhooks
import (
"encoding/json"
"fmt"
"net"
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/acamilleri/go-plexhooks/plex"
)
// Definition app configuration
type Definition struct {
ListenAddr *net.TCPAddr
Actions *Actions
Logger LoggerDefinition
}
// App struct with listenAddr and actions
type App struct {
listenAddr *net.TCPAddr
actions *Actions
log *logrus.Logger
}
// New create app from Definition values
func New(definition Definition) *App {
log := setupLogger(definition.Logger)
return &App{
listenAddr: definition.ListenAddr,
actions: definition.Actions,
log: log,
}
}
// Run running app with http server
func (a *App) Run() error {
if a.log.GetLevel() == logrus.DebugLevel {
for hook, actions := range *a.actions {
for _, action := range actions {
a.log.Debugf("action %s registered for hook %s", action.Name(), hook)
}
}
}
http.HandleFunc("/events", a.handler())
http.Handle("/metrics", promhttp.Handler())
a.log.Infof("running server on %s", a.listenAddr)
return http.ListenAndServe(a.listenAddr.String(), nil)
}
func (a *App) handler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var statusCode int = http.StatusOK
defer r.Body.Close()
trackRequestDuration := newTrackRequestDuration(r.Method, "/events")
if r.Method != http.MethodPost {
statusCode = http.StatusMethodNotAllowed
w.WriteHeader(statusCode)
time.Sleep(time.Second * 5)
httpRequestTotal.With(prometheus.Labels{
"handler": "/events",
"method": r.Method,
"code": strconv.Itoa(statusCode),
}).Inc()
trackRequestDuration.Finish()
return
}
err := a.handleRequest(r)
if err != nil {
statusCode = http.StatusInternalServerError
w.WriteHeader(statusCode)
trackRequestDuration.Finish()
httpRequestTotal.With(prometheus.Labels{
"handler": "/events",
"method": r.Method,
"code": strconv.Itoa(statusCode),
}).Inc()
return
}
w.WriteHeader(statusCode)
trackRequestDuration.Finish()
httpRequestTotal.With(prometheus.Labels{
"handler": "/events",
"method": r.Method,
"code": strconv.Itoa(statusCode),
}).Inc()
}
}
func (a *App) handleRequest(r *http.Request) error {
event, err := parseRequest(r)
if err != nil {
a.log.WithError(err).Error("failed to parse request")
return err
}
a.log.Infof("%s event handled", event.Name)
err = a.triggerActionsOnEvent(event)
if err != nil {
a.log.WithError(err).Errorf("%s event actions failed", event.Name)
return err
}
return nil
}
func (a *App) triggerActionsOnEvent(event plex.Event) error {
eventsReceivedTotal.With(prometheus.Labels{"event": event.Name.String()}).Inc()
hookName := event.Name
actions := a.actions.GetByHook(hookName)
if len(actions) == 0 {
return fmt.Errorf("no actions registered for %s hook", hookName)
}
for _, action := range actions {
name := action.Name()
actionDuration := newTrackActionDuration(event, action)
a.log.Debugf("action %s triggered", name)
err := action.Execute(event)
if err != nil {
a.log.WithError(err).Errorf("action %s failed", name)
actionsErrorTotal.With(
prometheus.Labels{"event": event.Name.String(), "action": action.Name()},
).Inc()
actionDuration.Finish()
continue
}
a.log.Infof("action %s success", name)
actionDuration.Finish()
actionsSuccessTotal.With(
prometheus.Labels{"event": event.Name.String(), "action": action.Name()},
).Inc()
}
return nil
}
func parseRequest(r *http.Request) (plex.Event, error) {
err := r.ParseMultipartForm(64)
if err != nil {
return plex.Event{}, fmt.Errorf("parse multipart request failed: %v", err)
}
payloadReq, ok := r.MultipartForm.Value["payload"]
if !ok {
return plex.Event{}, fmt.Errorf("read payload failed: %v", err)
}
var event plex.Event
err = json.Unmarshal([]byte(payloadReq[0]), &event)
if err != nil {
return plex.Event{}, fmt.Errorf("decode payload failed: %v", err)
}
return event, nil
}