-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
request_handler.go
51 lines (43 loc) · 1.46 KB
/
request_handler.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 muxie
import (
"net/http"
"strings"
)
type (
// RequestHandler is the matcher and handler link interface.
// It is used inside the `Mux` to handle requests based on end-developer's custom logic.
// If a "Matcher" passed then the "Handler" is executing and the rest of the Mux' routes will be ignored.
RequestHandler interface {
http.Handler
Matcher
}
simpleRequestHandler struct {
http.Handler
Matcher
}
// Matcher is the interface that all Matchers should be implemented
// in order to be registered into the Mux via the `Mux#AddRequestHandler/Match/MatchFunc` functions.
//
// Look the `Mux#AddRequestHandler` for more.
Matcher interface {
Match(*http.Request) bool
}
// MatcherFunc is a shortcut of the Matcher, as a function.
// See `Matcher`.
MatcherFunc func(*http.Request) bool
)
// Match returns the result of the "fn" matcher.
// Implementing the `Matcher` interface.
func (fn MatcherFunc) Match(r *http.Request) bool {
return fn(r)
}
// Host is a Matcher for hostlines.
// It can accept exact hosts line like "mysubdomain.localhost:8080"
// or a suffix, i.e ".localhost:8080" will work as a wildcard subdomain for our root domain.
// The domain and the port should match exactly the request's data.
type Host string
// Match validates the host, implementing the `Matcher` interface.
func (h Host) Match(r *http.Request) bool {
s := string(h)
return r.Host == s || (s[0] == '.' && strings.HasSuffix(r.Host, s)) || s == WildcardParamStart
}