-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstaticresponse.go
More file actions
69 lines (59 loc) · 1.6 KB
/
staticresponse.go
File metadata and controls
69 lines (59 loc) · 1.6 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
60
61
62
63
64
65
66
67
68
69
// Package staticresponse a demo plugin.
package staticresponse
import (
"context"
"fmt"
"net/http"
)
// Config the plugin configuration.
type Config struct {
StatusCode int `json:"statusCode,omitempty"`
Body string `json:"body,omitempty"`
Headers http.Header `json:"headers,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
StatusCode: http.StatusOK,
Body: "",
Headers: http.Header{},
}
}
// StaticResponse plugin.
type StaticResponse struct {
next http.Handler
statusCode int
body string
headers http.Header
name string
}
// New created a new StaticResponse plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if config.StatusCode < 100 || config.StatusCode > 999 {
return nil, fmt.Errorf("invalid response status code")
}
// Ensure headers are not nil if left unconfigured
if config.Headers == nil {
config.Headers = http.Header{}
}
return &StaticResponse{
next: next,
name: name,
statusCode: config.StatusCode,
body: config.Body,
headers: config.Headers,
}, nil
}
// ServeHTTP function required to make StaticResponse comply with http.Handler interface.
func (s *StaticResponse) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Set headers first before sending the response
for key, values := range s.headers {
for _, value := range values {
rw.Header().Add(key, value)
}
}
rw.WriteHeader(s.statusCode)
if s.body != "" {
_, _ = rw.Write([]byte(s.body))
}
}