-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
46 lines (37 loc) · 1023 Bytes
/
router.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
package traefik_routing_plugin
import (
"context"
"fmt"
"log"
"net/http"
)
type Router struct {
// Required by Traefik
next http.Handler
name string
// Our custom configuration
routes map[string]string
}
// Function needed for Traefik to recognize this module as a plugin
// Uses a generic http.Handler type from golang that we can use to work with the request
// by overriding different functions of the interface
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
log.Println("NEW FUNCTION CALLED")
if len(config.Routes) == 0 {
return nil, fmt.Errorf("routes cannot be empty")
}
return &Router{
routes: config.Routes,
next: next,
name: name,
}, nil
}
func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
log.Println("SERVE HTTP")
log.Println(req.URL)
for key, value := range a.routes {
log.Println(fmt.Sprintf("Setting header: %s : %s", key, value))
req.Header.Set(key, value)
}
a.next.ServeHTTP(rw, req)
}