-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.go
More file actions
59 lines (45 loc) · 1.09 KB
/
Copy pathplugin.go
File metadata and controls
59 lines (45 loc) · 1.09 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
package middleware_sample
import (
"net/http"
"github.com/roadrunner-server/api/v2/plugins/config"
"github.com/roadrunner-server/errors"
"go.uber.org/zap"
)
const name = "my_middleware_name"
type Plugin struct {
log *zap.Logger
cfg *Config
}
func (p *Plugin) Init(cfg config.Configurer, log *zap.Logger) error {
// check if we need to init this middleware
if !cfg.Has(name) {
return errors.E(errors.Disabled)
}
// populate configuration
p.cfg = &Config{}
err := cfg.UnmarshalKey(name, p.cfg)
if err != nil {
return err
}
// init default values
p.cfg.InitDefaults()
// init logger
p.log = new(zap.Logger)
*p.log = *log
return nil
}
// Middleware is our actual http middleware
func (p *Plugin) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get("https://google.com")
if err != nil {
p.log.Error("third-party api call", zap.Error(err))
return
}
p.log.Info("response", zap.Int("http_code", resp.StatusCode))
next.ServeHTTP(w, r)
})
}
func (p *Plugin) Name() string {
return name
}