1
0
Fork 0

Added ReplacePathRegex middleware

This commit is contained in:
Tiscs Sun 2017-10-30 19:54:03 +08:00 committed by Traefiker
parent e8633d17e8
commit 5042c5bf40
5 changed files with 138 additions and 0 deletions

View file

@ -92,6 +92,13 @@ func (r *Rules) replacePath(paths ...string) *mux.Route {
return r.route.route
}
func (r *Rules) replacePathRegex(paths ...string) *mux.Route {
for _, path := range paths {
r.route.replacePathRegex = path
}
return r.route.route
}
func (r *Rules) addPrefix(paths ...string) *mux.Route {
for _, path := range paths {
r.route.addPrefix = path
@ -155,6 +162,7 @@ func (r *Rules) parseRules(expression string, onRule func(functionName string, f
"HeadersRegexp": r.headersRegexp,
"AddPrefix": r.addPrefix,
"ReplacePath": r.replacePath,
"ReplacePathRegex": r.replacePathRegex,
"Query": r.query,
}

View file

@ -16,6 +16,7 @@ import (
"reflect"
"regexp"
"sort"
"strings"
"sync"
"time"
@ -81,6 +82,7 @@ type serverRoute struct {
stripPrefixesRegex []string
addPrefix string
replacePath string
replacePathRegex string
}
// NewServer returns an initialized Server.
@ -1065,6 +1067,15 @@ func (server *Server) wireFrontendBackend(serverRoute *serverRoute, handler http
}
}
if len(serverRoute.replacePathRegex) > 0 {
sp := strings.Split(serverRoute.replacePathRegex, " ")
if len(sp) == 2 {
handler = middlewares.NewReplacePathRegexHandler(sp[0], sp[1], handler)
} else {
log.Warnf("Invalid syntax for ReplacePathRegex: %s. Separate the regular expression and the replacement by a space.", serverRoute.replacePathRegex)
}
}
// add prefix - This needs to always be right before ReplacePath on the chain (second in order in this function)
// -- Adding Path Prefix should happen after all *Strip Matcher+Modifiers ran, but before Replace (in case it's configured)
if len(serverRoute.addPrefix) > 0 {