(next http.Handler)
| 75 | } |
| 76 | |
| 77 | func (hr HeaderRouter) Handler(next http.Handler) http.Handler { |
| 78 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 79 | if len(hr) == 0 { |
| 80 | // skip if no routes set |
| 81 | next.ServeHTTP(w, r) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | // find first matching header route, and continue |
| 86 | for header, matchers := range hr { |
| 87 | headerValue := r.Header.Get(header) |
| 88 | if headerValue == "" { |
| 89 | continue |
| 90 | } |
| 91 | headerValue = strings.ToLower(headerValue) |
| 92 | for _, matcher := range matchers { |
| 93 | if matcher.IsMatch(headerValue) { |
| 94 | matcher.Middleware(next).ServeHTTP(w, r) |
| 95 | return |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // if no match, check for "*" default route |
| 101 | matcher, ok := hr["*"] |
| 102 | if !ok || matcher[0].Middleware == nil { |
| 103 | next.ServeHTTP(w, r) |
| 104 | return |
| 105 | } |
| 106 | matcher[0].Middleware(next).ServeHTTP(w, r) |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | type HeaderRoute struct { |
| 111 | Middleware func(next http.Handler) http.Handler |
nothing calls this directly
no test coverage detected