Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Externalize Træfik rules in a dedicated package #2933

Merged
merged 1 commit into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions server/rules.go → rules/rules.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package rules

import (
"errors"
Expand All @@ -16,12 +16,12 @@ import (

// Rules holds rule parsing and configuration
type Rules struct {
route *serverRoute
Route *types.ServerRoute
err error
}

func (r *Rules) host(hosts ...string) *mux.Route {
return r.route.route.MatcherFunc(func(req *http.Request, route *mux.RouteMatch) bool {
return r.Route.Route.MatcherFunc(func(req *http.Request, route *mux.RouteMatch) bool {
reqHost, _, err := net.SplitHostPort(req.Host)
if err != nil {
reqHost = req.Host
Expand All @@ -36,27 +36,27 @@ func (r *Rules) host(hosts ...string) *mux.Route {
}

func (r *Rules) hostRegexp(hosts ...string) *mux.Route {
router := r.route.route.Subrouter()
router := r.Route.Route.Subrouter()
for _, host := range hosts {
router.Host(types.CanonicalDomain(host))
}
return r.route.route
return r.Route.Route
}

func (r *Rules) path(paths ...string) *mux.Route {
router := r.route.route.Subrouter()
router := r.Route.Route.Subrouter()
for _, path := range paths {
router.Path(strings.TrimSpace(path))
}
return r.route.route
return r.Route.Route
}

func (r *Rules) pathPrefix(paths ...string) *mux.Route {
router := r.route.route.Subrouter()
router := r.Route.Route.Subrouter()
for _, path := range paths {
buildPath(path, router)
}
return r.route.route
return r.Route.Route
}

func buildPath(path string, router *mux.Router) {
Expand Down Expand Up @@ -88,75 +88,75 @@ 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()
r.Route.StripPrefixes = paths
router := r.Route.Route.Subrouter()
for _, path := range paths {
router.Path(strings.TrimSpace(path))
}
return r.route.route
return r.Route.Route
}

func (r *Rules) pathStripRegex(paths ...string) *mux.Route {
sort.Sort(bySize(paths))
r.route.stripPrefixesRegex = paths
router := r.route.route.Subrouter()
r.Route.StripPrefixesRegex = paths
router := r.Route.Route.Subrouter()
for _, path := range paths {
router.Path(strings.TrimSpace(path))
}
return r.route.route
return r.Route.Route
}

func (r *Rules) replacePath(paths ...string) *mux.Route {
for _, path := range paths {
r.route.replacePath = path
r.Route.ReplacePath = path
}
return r.route.route
return r.Route.Route
}

func (r *Rules) replacePathRegex(paths ...string) *mux.Route {
for _, path := range paths {
r.route.replacePathRegex = path
r.Route.ReplacePathRegex = path
}
return r.route.route
return r.Route.Route
}

func (r *Rules) addPrefix(paths ...string) *mux.Route {
for _, path := range paths {
r.route.addPrefix = path
r.Route.AddPrefix = path
}
return r.route.route
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()
r.Route.StripPrefixes = paths
router := r.Route.Route.Subrouter()
for _, path := range paths {
buildPath(path, router)
}
return r.route.route
return r.Route.Route
}

func (r *Rules) pathPrefixStripRegex(paths ...string) *mux.Route {
sort.Sort(bySize(paths))
r.route.stripPrefixesRegex = paths
router := r.route.route.Subrouter()
r.Route.StripPrefixesRegex = paths
router := r.Route.Route.Subrouter()
for _, path := range paths {
router.PathPrefix(strings.TrimSpace(path))
}
return r.route.route
return r.Route.Route
}

func (r *Rules) methods(methods ...string) *mux.Route {
return r.route.route.Methods(methods...)
return r.Route.Route.Methods(methods...)
}

func (r *Rules) headers(headers ...string) *mux.Route {
return r.route.route.Headers(headers...)
return r.Route.Route.Headers(headers...)
}

func (r *Rules) headersRegexp(headers ...string) *mux.Route {
return r.route.route.HeadersRegexp(headers...)
return r.Route.Route.HeadersRegexp(headers...)
}

func (r *Rules) query(query ...string) *mux.Route {
Expand All @@ -165,7 +165,7 @@ func (r *Rules) query(query ...string) *mux.Route {
queries = append(queries, strings.Split(elem, "=")...)
}

return r.route.route.Queries(queries...)
return r.Route.Route.Queries(queries...)
}

func (r *Rules) parseRules(expression string, onRule func(functionName string, function interface{}, arguments []string) error) error {
Expand Down
23 changes: 12 additions & 11 deletions server/rules_test.go → rules/rules_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package rules

import (
"net/http"
Expand All @@ -7,15 +7,16 @@ import (

"github.com/containous/mux"
"github.com/containous/traefik/testhelpers"
"github.com/containous/traefik/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseOneRule(t *testing.T) {
router := mux.NewRouter()
route := router.NewRoute()
serverRoute := &serverRoute{route: route}
rules := &Rules{route: serverRoute}
serverRoute := &types.ServerRoute{Route: route}
rules := &Rules{Route: serverRoute}

expression := "Host:foo.bar"
routeResult, err := rules.Parse(expression)
Expand All @@ -30,8 +31,8 @@ func TestParseOneRule(t *testing.T) {
func TestParseTwoRules(t *testing.T) {
router := mux.NewRouter()
route := router.NewRoute()
serverRoute := &serverRoute{route: route}
rules := &Rules{route: serverRoute}
serverRoute := &types.ServerRoute{Route: route}
rules := &Rules{Route: serverRoute}

expression := "Host: Foo.Bar ; Path:/FOObar"
routeResult, err := rules.Parse(expression)
Expand Down Expand Up @@ -90,7 +91,7 @@ func TestParseDomains(t *testing.T) {
func TestPriorites(t *testing.T) {
router := mux.NewRouter()
router.StrictSlash(true)
rules := &Rules{route: &serverRoute{route: router.NewRoute()}}
rules := &Rules{Route: &types.ServerRoute{Route: router.NewRoute()}}
expression01 := "PathPrefix:/foo"

routeFoo, err := rules.Parse(expression01)
Expand All @@ -105,7 +106,7 @@ func TestPriorites(t *testing.T) {
routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/fo"}}, &mux.RouteMatch{})
assert.False(t, routeMatch, "Error matching route")

multipleRules := &Rules{route: &serverRoute{route: router.NewRoute()}}
multipleRules := &Rules{Route: &types.ServerRoute{Route: router.NewRoute()}}
expression02 := "PathPrefix:/foobar"

routeFoobar, err := multipleRules.Parse(expression02)
Expand Down Expand Up @@ -172,8 +173,8 @@ func TestHostRegexp(t *testing.T) {
t.Parallel()

rls := &Rules{
route: &serverRoute{
route: &mux.Route{},
Route: &types.ServerRoute{
Route: &mux.Route{},
},
}

Expand Down Expand Up @@ -239,8 +240,8 @@ func TestPathPrefix(t *testing.T) {
t.Parallel()

rls := &Rules{
route: &serverRoute{
route: &mux.Route{},
Route: &types.ServerRoute{
Route: &mux.Route{},
},
}

Expand Down
52 changes: 22 additions & 30 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/containous/traefik/middlewares/redirect"
"github.com/containous/traefik/middlewares/tracing"
"github.com/containous/traefik/provider"
"github.com/containous/traefik/rules"
"github.com/containous/traefik/safe"
"github.com/containous/traefik/server/cookie"
traefikTls "github.com/containous/traefik/tls"
Expand Down Expand Up @@ -84,15 +85,6 @@ type serverEntryPoint struct {
certs safe.Safe
}

type serverRoute struct {
route *mux.Route
stripPrefixes []string
stripPrefixesRegex []string
addPrefix string
replacePath string
replacePathRegex string
}

// NewServer returns an initialized Server.
func NewServer(globalConfiguration configuration.GlobalConfiguration, provider provider.Provider) *Server {
server := new(Server)
Expand Down Expand Up @@ -524,7 +516,7 @@ func (s *Server) postLoadConfiguration() {

if acmeEnabled {
for _, route := range frontend.Routes {
rules := Rules{}
rules := rules.Rules{}
domains, err := rules.ParseDomains(route.Rule)
if err != nil {
log.Errorf("Error parsing domains: %v", err)
Expand Down Expand Up @@ -899,7 +891,7 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
for _, entryPointName := range frontend.EntryPoints {
log.Debugf("Wiring frontend %s to entryPoint %s", frontendName, entryPointName)

newServerRoute := &serverRoute{route: serverEntryPoints[entryPointName].httpRouter.GetHandler().NewRoute().Name(frontendName)}
newServerRoute := &types.ServerRoute{Route: serverEntryPoints[entryPointName].httpRouter.GetHandler().NewRoute().Name(frontendName)}
for routeName, route := range frontend.Routes {
err := getRoute(newServerRoute, &route)
if err != nil {
Expand Down Expand Up @@ -1177,11 +1169,11 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
log.Debugf("Reusing backend %s", frontend.Backend)
}
if frontend.Priority > 0 {
newServerRoute.route.Priority(frontend.Priority)
newServerRoute.Route.Priority(frontend.Priority)
}
s.wireFrontendBackend(newServerRoute, backends[entryPointName+providerName+frontend.Backend])

err := newServerRoute.route.GetError()
err := newServerRoute.Route.GetError()
if err != nil {
log.Errorf("Error building route: %s", err)
}
Expand Down Expand Up @@ -1236,48 +1228,48 @@ func configureIPWhitelistMiddleware(whitelistSourceRanges []string) (negroni.Han
return nil, nil
}

func (s *Server) wireFrontendBackend(serverRoute *serverRoute, handler http.Handler) {
func (s *Server) wireFrontendBackend(serverRoute *types.ServerRoute, handler http.Handler) {
// path replace - This needs to always be the very last on the handler chain (first in the order in this function)
// -- Replacing Path should happen at the very end of the Modifier chain, after all the Matcher+Modifiers ran
if len(serverRoute.replacePath) > 0 {
if len(serverRoute.ReplacePath) > 0 {
handler = &middlewares.ReplacePath{
Path: serverRoute.replacePath,
Path: serverRoute.ReplacePath,
Handler: handler,
}
}

if len(serverRoute.replacePathRegex) > 0 {
sp := strings.Split(serverRoute.replacePathRegex, " ")
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)
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 {
if len(serverRoute.AddPrefix) > 0 {
handler = &middlewares.AddPrefix{
Prefix: serverRoute.addPrefix,
Prefix: serverRoute.AddPrefix,
Handler: handler,
}
}

// strip prefix
if len(serverRoute.stripPrefixes) > 0 {
if len(serverRoute.StripPrefixes) > 0 {
handler = &middlewares.StripPrefix{
Prefixes: serverRoute.stripPrefixes,
Prefixes: serverRoute.StripPrefixes,
Handler: handler,
}
}

// strip prefix with regex
if len(serverRoute.stripPrefixesRegex) > 0 {
handler = middlewares.NewStripPrefixRegex(handler, serverRoute.stripPrefixesRegex)
if len(serverRoute.StripPrefixesRegex) > 0 {
handler = middlewares.NewStripPrefixRegex(handler, serverRoute.StripPrefixesRegex)
}

serverRoute.route.Handler(handler)
serverRoute.Route.Handler(handler)
}

func (s *Server) buildRedirectHandler(srcEntryPointName string, opt *types.Redirect) (negroni.Handler, error) {
Expand Down Expand Up @@ -1335,14 +1327,14 @@ func parseHealthCheckOptions(lb healthcheck.LoadBalancer, backend string, hc *ty
}
}

func getRoute(serverRoute *serverRoute, route *types.Route) error {
rules := Rules{route: serverRoute}
func getRoute(serverRoute *types.ServerRoute, route *types.Route) error {
rules := rules.Rules{Route: serverRoute}
newRoute, err := rules.Parse(route.Rule)
if err != nil {
return err
}
newRoute.Priority(serverRoute.route.GetPriority() + len(route.Rule))
serverRoute.route = newRoute
newRoute.Priority(serverRoute.Route.GetPriority() + len(route.Rule))
serverRoute.Route = newRoute
return nil
}

Expand Down
Loading