-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheaderModPlugin.go
73 lines (57 loc) · 1.63 KB
/
headerModPlugin.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
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func init() {
fmt.Println("headerModPlugin plugin is loaded!")
}
func main() {}
// HandlerRegisterer is the name of the symbol krakend looks up to try and register plugins
var HandlerRegisterer registrable = registrable("headerModPlugin")
type registrable string
const outputHeaderName = "X-Friend-User"
const pluginName = "headerModPlugin"
func (r registrable) RegisterHandlers(f func(
name string,
handler func(
context.Context,
map[string]interface{},
http.Handler) (http.Handler, error),
)) {
f(pluginName, r.registerHandlers)
}
func (r registrable) registerHandlers(ctx context.Context, extra map[string]interface{}, handler http.Handler) (http.Handler, error) {
attachUserID, ok := extra["attachuserid"].(string)
if !ok {
panic(errors.New("incorrect config").Error())
}
client := &http.Client{Timeout: 3 * time.Second}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://api.github.com/users/%v", attachUserID), nil)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rq.Header.Set("Content-Type", "application/json")
rs, err := client.Do(rq)
if err != nil {
http.Error(w, err.Error(), http.StatusNotAcceptable)
return
}
defer rs.Body.Close()
rsBodyBytes, err := ioutil.ReadAll(rs.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusNotAcceptable)
return
}
r2 := new(http.Request)
*r2 = *r
r2.Header.Set(outputHeaderName, string(rsBodyBytes))
handler.ServeHTTP(w, r2)
}), nil
}