-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethodblock.go
More file actions
42 lines (36 loc) · 947 Bytes
/
methodblock.go
File metadata and controls
42 lines (36 loc) · 947 Bytes
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 MethodBlock
import (
"context"
"net/http"
)
type Config struct {
Methods []string `json:"Methods,omitempty" toml:"Methods,omitempty" yaml:"Methods,omitempty" export:"true"`
Message string `json:"Message,omitempty" toml:"Message,omitempty" yaml:"Message,omitempty" export:"true"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Methods: []string{},
Message: "",
}
}
type MethodBlock struct {
cfg *Config
next http.Handler
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &MethodBlock{
cfg: config,
next: next,
}, nil
}
func (m *MethodBlock) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
for _, method := range m.cfg.Methods {
if method == req.Method {
rw.WriteHeader(http.StatusMethodNotAllowed)
rw.Write([]byte(m.cfg.Message))
return
}
}
m.next.ServeHTTP(rw, req)
}