-
Notifications
You must be signed in to change notification settings - Fork 2
/
queryparameter-to-header.go
55 lines (48 loc) · 1.31 KB
/
queryparameter-to-header.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
// Package plugindemo a demo plugin.
package queryparameter_to_header
import (
"context"
"fmt"
"net/http"
)
// the plugin configuration.
type Config struct {
QueryParameter string `json:"query_parameter"`
Header string `json:"header"`
}
// CreateConfig creates the default plugin configuration
func CreateConfig() *Config {
return &Config{
QueryParameter: "v",
Header: "X-Version",
}
}
type QueryParameterToHeaderMiddleware struct {
next http.Handler
queryParameter string
header string
name string
}
// Creates a new plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.Header) < 1 {
return nil, fmt.Errorf("header cannot be empty string")
}
if len(config.QueryParameter) < 1 {
return nil, fmt.Errorf("query parameter cannot be empty string")
}
return &QueryParameterToHeaderMiddleware{
header: config.Header,
queryParameter: config.QueryParameter,
next: next,
name: name,
}, nil
}
func (m *QueryParameterToHeaderMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
parameterValues := query[m.queryParameter]
if len(parameterValues) > 0 {
req.Header.Set(m.header, parameterValues[0])
}
m.next.ServeHTTP(rw, req)
}