-
Notifications
You must be signed in to change notification settings - Fork 3
/
matcher.go
51 lines (41 loc) · 1.17 KB
/
matcher.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
package internal
import (
"errors"
"net/http"
"reverse_proxy/config"
"strings"
)
func findRoute(request *http.Request, proxyConfig *config.ProxyConfig) *config.Route {
headers := request.Header
for name, _ := range headers {
for _, proxyRoute := range proxyConfig.Routes {
// TODO: Make this section clean. It's not reusable code. So, it should be refactored later!
routeType := strings.ToLower(proxyRoute.Type)
if routeType == "header" {
for key, routeHeader := range proxyRoute.Headers {
if strings.ToLower(key) == strings.ToLower(name) {
for _, value := range routeHeader.Values {
if strings.ToLower(headers.Get(key)) == strings.ToLower(value) {
return &proxyRoute
}
}
}
}
} else if routeType == "path" {
for _, routePath := range proxyRoute.Paths {
if routePath == strings.ToLower(request.URL.Path) {
return &proxyRoute
}
}
}
}
}
return nil
}
func Matcher(request *http.Request, proxyConfig *config.ProxyConfig) (*config.Route, error) {
route := findRoute(request, proxyConfig)
if route == nil {
return route, errors.New("there is no route found")
}
return route, nil
}