Add multiple rules
Signed-off-by: Emile Vauge <emile@vauge.com>
This commit is contained in:
parent
ef72d355d6
commit
8486766a60
3 changed files with 78 additions and 31 deletions
|
@ -7,18 +7,19 @@ import (
|
||||||
|
|
||||||
// StripPrefix is a middleware used to strip prefix from an URL request
|
// StripPrefix is a middleware used to strip prefix from an URL request
|
||||||
type StripPrefix struct {
|
type StripPrefix struct {
|
||||||
Handler http.Handler
|
Handler http.Handler
|
||||||
Prefix string
|
Prefixes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StripPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *StripPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if p := strings.TrimPrefix(r.URL.Path, s.Prefix); len(p) < len(r.URL.Path) {
|
for _, prefix := range s.Prefixes {
|
||||||
r.URL.Path = p
|
if p := strings.TrimPrefix(r.URL.Path, strings.TrimSpace(prefix)); len(p) < len(r.URL.Path) {
|
||||||
r.RequestURI = r.URL.RequestURI()
|
r.URL.Path = p
|
||||||
s.Handler.ServeHTTP(w, r)
|
r.RequestURI = r.URL.RequestURI()
|
||||||
} else {
|
s.Handler.ServeHTTP(w, r)
|
||||||
http.NotFound(w, r)
|
}
|
||||||
}
|
}
|
||||||
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHandler sets handler
|
// SetHandler sets handler
|
||||||
|
|
82
rules.go
82
rules.go
|
@ -3,7 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,26 +14,65 @@ type Rules struct {
|
||||||
route *serverRoute
|
route *serverRoute
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rules) host(host string) *mux.Route {
|
func (r *Rules) host(hosts ...string) *mux.Route {
|
||||||
return r.route.route.Host(host)
|
return r.route.route.MatcherFunc(func(req *http.Request, route *mux.RouteMatch) bool {
|
||||||
|
for _, host := range hosts {
|
||||||
|
if strings.EqualFold(req.Host, strings.TrimSpace(host)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rules) path(path string) *mux.Route {
|
func (r *Rules) hostRegexp(hosts ...string) *mux.Route {
|
||||||
return r.route.route.Path(path)
|
router := r.route.route.Subrouter()
|
||||||
|
for _, host := range hosts {
|
||||||
|
router.Host(strings.TrimSpace(host))
|
||||||
|
}
|
||||||
|
return r.route.route
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rules) pathPrefix(path string) *mux.Route {
|
func (r *Rules) path(paths ...string) *mux.Route {
|
||||||
return r.route.route.PathPrefix(path)
|
router := r.route.route.Subrouter()
|
||||||
|
for _, path := range paths {
|
||||||
|
router.Path(strings.TrimSpace(path))
|
||||||
|
}
|
||||||
|
return r.route.route
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rules) pathStrip(path string) *mux.Route {
|
func (r *Rules) pathPrefix(paths ...string) *mux.Route {
|
||||||
r.route.stripPrefix = path
|
router := r.route.route.Subrouter()
|
||||||
return r.route.route.Path(path)
|
for _, path := range paths {
|
||||||
|
router.PathPrefix(strings.TrimSpace(path))
|
||||||
|
}
|
||||||
|
return r.route.route
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rules) pathPrefixStrip(path string) *mux.Route {
|
type bySize []string
|
||||||
r.route.stripPrefix = path
|
|
||||||
return r.route.route.PathPrefix(path)
|
func (a bySize) Len() int { return len(a) }
|
||||||
|
func (a bySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a bySize) Less(i, j int) bool { return len(a[i]) > len(a[j]) }
|
||||||
|
|
||||||
|
func (r *Rules) pathStrip(paths ...string) *mux.Route {
|
||||||
|
sort.Sort(bySize(paths))
|
||||||
|
r.route.stripPrefixes = paths
|
||||||
|
router := r.route.route.Subrouter()
|
||||||
|
for _, path := range paths {
|
||||||
|
router.Path(strings.TrimSpace(path))
|
||||||
|
}
|
||||||
|
return r.route.route
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Rules) pathPrefixStrip(paths ...string) *mux.Route {
|
||||||
|
sort.Sort(bySize(paths))
|
||||||
|
r.route.stripPrefixes = paths
|
||||||
|
router := r.route.route.Subrouter()
|
||||||
|
for _, path := range paths {
|
||||||
|
router.PathPrefix(strings.TrimSpace(path))
|
||||||
|
}
|
||||||
|
return r.route.route
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rules) methods(methods ...string) *mux.Route {
|
func (r *Rules) methods(methods ...string) *mux.Route {
|
||||||
|
@ -50,32 +91,33 @@ func (r *Rules) headersRegexp(headers ...string) *mux.Route {
|
||||||
func (r *Rules) Parse(expression string) (*mux.Route, error) {
|
func (r *Rules) Parse(expression string) (*mux.Route, error) {
|
||||||
functions := map[string]interface{}{
|
functions := map[string]interface{}{
|
||||||
"Host": r.host,
|
"Host": r.host,
|
||||||
|
"HostRegexp": r.hostRegexp,
|
||||||
"Path": r.path,
|
"Path": r.path,
|
||||||
"PathStrip": r.pathStrip,
|
"PathStrip": r.pathStrip,
|
||||||
"PathPrefix": r.pathPrefix,
|
"PathPrefix": r.pathPrefix,
|
||||||
"PathPrefixStrip": r.pathPrefixStrip,
|
"PathPrefixStrip": r.pathPrefixStrip,
|
||||||
"Methods": r.methods,
|
"Method": r.methods,
|
||||||
"Headers": r.headers,
|
"Headers": r.headers,
|
||||||
"HeadersRegexp": r.headersRegexp,
|
"HeadersRegexp": r.headersRegexp,
|
||||||
}
|
}
|
||||||
f := func(c rune) bool {
|
f := func(c rune) bool {
|
||||||
return c == ':' || c == '='
|
return c == ':'
|
||||||
}
|
}
|
||||||
// get function
|
// get function
|
||||||
parsedFunctions := strings.FieldsFunc(expression, f)
|
parsedFunctions := strings.FieldsFunc(expression, f)
|
||||||
if len(parsedFunctions) != 2 {
|
if len(parsedFunctions) == 0 {
|
||||||
return nil, errors.New("Error parsing rule: " + expression)
|
return nil, errors.New("Error parsing rule: " + expression)
|
||||||
}
|
}
|
||||||
parsedFunction, ok := functions[parsedFunctions[0]]
|
parsedFunction, ok := functions[parsedFunctions[0]]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("Error parsing rule: " + expression + ". Unknow function: " + parsedFunctions[0])
|
return nil, errors.New("Error parsing rule: " + expression + ". Unknow function: " + parsedFunctions[0])
|
||||||
}
|
}
|
||||||
|
parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...)
|
||||||
fargs := func(c rune) bool {
|
fargs := func(c rune) bool {
|
||||||
return c == ',' || c == ';'
|
return c == ',' || c == ';'
|
||||||
}
|
}
|
||||||
// get function
|
// get function
|
||||||
parsedArgs := strings.FieldsFunc(parsedFunctions[1], fargs)
|
parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs)
|
||||||
if len(parsedArgs) == 0 {
|
if len(parsedArgs) == 0 {
|
||||||
return nil, errors.New("Error parsing args from rule: " + expression)
|
return nil, errors.New("Error parsing args from rule: " + expression)
|
||||||
}
|
}
|
||||||
|
@ -86,7 +128,11 @@ func (r *Rules) Parse(expression string) (*mux.Route, error) {
|
||||||
}
|
}
|
||||||
method := reflect.ValueOf(parsedFunction)
|
method := reflect.ValueOf(parsedFunction)
|
||||||
if method.IsValid() {
|
if method.IsValid() {
|
||||||
return method.Call(inputs)[0].Interface().(*mux.Route), nil
|
resultRoute := method.Call(inputs)[0].Interface().(*mux.Route)
|
||||||
|
if resultRoute.GetError() != nil {
|
||||||
|
return nil, resultRoute.GetError()
|
||||||
|
}
|
||||||
|
return resultRoute, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("Method not found: " + parsedFunctions[0])
|
return nil, errors.New("Method not found: " + parsedFunctions[0])
|
||||||
}
|
}
|
||||||
|
|
10
server.go
10
server.go
|
@ -57,8 +57,8 @@ type serverEntryPoint struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverRoute struct {
|
type serverRoute struct {
|
||||||
route *mux.Route
|
route *mux.Route
|
||||||
stripPrefix string
|
stripPrefixes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer returns an initialized Server.
|
// NewServer returns an initialized Server.
|
||||||
|
@ -471,10 +471,10 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
|
||||||
|
|
||||||
func (server *Server) wireFrontendBackend(serverRoute *serverRoute, handler http.Handler) {
|
func (server *Server) wireFrontendBackend(serverRoute *serverRoute, handler http.Handler) {
|
||||||
// strip prefix
|
// strip prefix
|
||||||
if len(serverRoute.stripPrefix) > 0 {
|
if len(serverRoute.stripPrefixes) > 0 {
|
||||||
serverRoute.route.Handler(&middlewares.StripPrefix{
|
serverRoute.route.Handler(&middlewares.StripPrefix{
|
||||||
Prefix: serverRoute.stripPrefix,
|
Prefixes: serverRoute.stripPrefixes,
|
||||||
Handler: handler,
|
Handler: handler,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
serverRoute.route.Handler(handler)
|
serverRoute.route.Handler(handler)
|
||||||
|
|
Loading…
Reference in a new issue