-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.go
101 lines (90 loc) · 2.23 KB
/
routing.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package aggregadantur
import (
"crypto/tls"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/orange-cloudfoundry/aggregadantur/models"
log "github.com/sirupsen/logrus"
"github.com/vulcand/oxy/trace"
"net"
"net/http"
"strings"
"time"
)
type Router struct {
rtr *mux.Router
store sessions.Store
tracer bool
}
func NewRouter(store sessions.Store) *Router {
return &Router{
rtr: mux.NewRouter(),
store: store,
tracer: true,
}
}
func (r *Router) DisableTracer() {
r.tracer = false
}
func (r Router) AddMuxRoute(route *models.AggregateRoute) error {
httpClient := makeHttpClient(route.InsecureSkipVerify)
var handler http.Handler
var err error
if route.Upstream.Handler != nil {
handler = route.Upstream.Handler
} else {
handler, err = NewForwardHandler(route)
if err != nil {
return err
}
}
handler = NewAggregateHandler(handler, route, httpClient)
handler = NewAuthHandler(handler, route, httpClient, r.store)
if r.tracer {
handler, err = trace.New(handler, log.StandardLogger().Out)
}
if err != nil {
return err
}
r.rtr.NewRoute().
Name(route.Name).
MatcherFunc(func(req *http.Request, match *mux.RouteMatch) bool {
if route.Path != "/" && !strings.HasPrefix(req.URL.Path, route.Path) {
return false
}
return route.Hosts.Match(req.Host)
}).Handler(handler)
return nil
}
func (r Router) AddMuxRoutes(routes ...*models.AggregateRoute) error {
for _, route := range routes {
err := r.AddMuxRoute(route)
if err != nil {
return fmt.Errorf("Error when adding route %s: %s", route.Name, err.Error())
}
}
return nil
}
func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.rtr.ServeHTTP(w, req)
}
func makeHttpClient(skipSSLValidation bool) *http.Client {
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: skipSSLValidation,
},
},
}
}