Merge pull request #285 from containous/add-multiple-rules
Add multiple rules
This commit is contained in:
commit
3ee3daee00
8 changed files with 97 additions and 53 deletions
|
@ -1,16 +1,14 @@
|
||||||
branches:
|
branches:
|
||||||
except:
|
except:
|
||||||
- /^v\d\.\d\.\d.*$/
|
- /^v\d\.\d\.\d.*$/
|
||||||
|
|
||||||
env:
|
env:
|
||||||
REPO: $TRAVIS_REPO_SLUG
|
REPO: $TRAVIS_REPO_SLUG
|
||||||
VERSION: v1.0.0-beta.$TRAVIS_BUILD_NUMBER
|
VERSION: v1.0.0-beta.$TRAVIS_BUILD_NUMBER
|
||||||
|
global:
|
||||||
|
secure: btt4r13t09gQlHb6gYrvGC2yGCMMHfnp1Mz1RQedc4Mpf/FfT8aE6xmK2a2i9CCvskjrP0t/BFaS4yxIURjnFRn+ugQIEa0pLspB9UJArW/vgOSpIWM9/OQ/fg8z5XuMxN6Md4DL1/iLypMNSageA1x0TRdt89+D1N1dALpg5XRCXLFbC84TLi0gjlFuib9ibPKzEhLT+anCRJ6iZMzeupDSoaCVbAtJMoDvXw4+4AcRZ1+k4MybBLyCib5boaEOt4pTT88mz4Kk0YaMwPVJyg9Qv36VqyUcPS09Yd95LuyVQ4+tZt8Y1ccbIzULsK+sLM3hLCzxlmlpN3dQBlZJiiRtQde0mgGAKyC0P0A1XjuDTywcsa5edB+fTk1Dsewz9xZ9V0NmMz8t+UNZnaSsAPga9i86jULbXUUwMVSzVRc+Xgx02liB/8qI1xYC9FM6ilStt7rn7mF0k3KbiWhcptgeXjO6Lah9FjEKd5w4MXsdUSTi/86rQaLo+kj+XdaTrXCTulKHyRyQEUj+8V1w0oVz7pcGjePHd7y5oU9ByifVQy6sytuFBfRZvugM5bKHo+i0pcWvixrZS42DrzwxZJsspANOvqSe5ifVbvOkfUppQdCBIwptxV5N1b49XPKU3W/w34QJ8xGmKp3TFA7WwVCztriFHjPgiRpB3EG99Bg=
|
||||||
sudo: required
|
sudo: required
|
||||||
|
|
||||||
services:
|
services:
|
||||||
- docker
|
- docker
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- sudo service docker stop
|
- sudo service docker stop
|
||||||
- sudo curl https://get.docker.com/builds/Linux/x86_64/docker-1.10.1 -o /usr/bin/docker
|
- sudo curl https://get.docker.com/builds/Linux/x86_64/docker-1.10.1 -o /usr/bin/docker
|
||||||
|
@ -18,16 +16,13 @@ install:
|
||||||
- sudo service docker start
|
- sudo service docker start
|
||||||
- pip install --user mkdocs
|
- pip install --user mkdocs
|
||||||
- pip install --user pymdown-extensions
|
- pip install --user pymdown-extensions
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- make validate
|
- make validate
|
||||||
- make binary
|
- make binary
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- make test-unit
|
- make test-unit
|
||||||
- make test-integration
|
- make test-integration
|
||||||
- make crossbinary
|
- make crossbinary
|
||||||
- make image
|
- make image
|
||||||
|
|
||||||
after_success:
|
after_success:
|
||||||
- make deploy
|
- make deploy
|
Binary file not shown.
|
@ -59,13 +59,14 @@ Here is an example of entrypoints definition:
|
||||||
A frontend is a set of rules that forwards the incoming traffic from an entrypoint to a backend.
|
A frontend is a set of rules that forwards the incoming traffic from an entrypoint to a backend.
|
||||||
Frontends can be defined using the following rules:
|
Frontends can be defined using the following rules:
|
||||||
|
|
||||||
- `Headers`: Headers adds a matcher for request header values. It accepts a sequence of key/value pairs to be matched. For example: `Content-Type, application/json`
|
- `Headers: Content-Type, application/json`: Headers adds a matcher for request header values. It accepts a sequence of key/value pairs to be matched.
|
||||||
- `HeadersRegexp`: Regular expressions can be used with headers as well. It accepts a sequence of key/value pairs, where the value has regex support. For example: `Content-Type, application/(text|json)`
|
- `HeadersRegexp: Content-Type, application/(text|json)`: Regular expressions can be used with headers as well. It accepts a sequence of key/value pairs, where the value has regex support.
|
||||||
- `Host`: Host adds a matcher for the URL host. It accepts a template with zero or more URL variables enclosed by `{}`. Variables can define an optional regexp pattern to be matched: `www.traefik.io`, `{subdomain:[a-z]+}.traefik.io`
|
- `Host: traefik.io, www.traefik.io`: Match request host with given host list.
|
||||||
- `Methods`: Methods adds a matcher for HTTP methods. It accepts a sequence of one or more methods to be matched, e.g.: `GET`, `POST`, `PUT`
|
- `HostRegexp: traefik.io, {subdomain:[a-z]+}.traefik.io`: Adds a matcher for the URL hosts. It accepts templates with zero or more URL variables enclosed by `{}`. Variables can define an optional regexp pattern to be matched.
|
||||||
- `Path`: Path adds a matcher for the URL path. It accepts a template with zero or more URL variables enclosed by `{}`. The template must start with a `/`. For exemple `/products/` `/articles/{category}/{id:[0-9]+}`
|
- `Method: GET, POST, PUT`: Methods adds a matcher for HTTP methods. It accepts a sequence of one or more methods to be matched.
|
||||||
|
- `Path: /products/, /articles/{category}/{id:[0-9]+}`: Path adds a matcher for the URL paths. It accepts templates with zero or more URL variables enclosed by `{}`.
|
||||||
- `PathStrip`: Same as `Path` but strip the given prefix from the request URL's Path.
|
- `PathStrip`: Same as `Path` but strip the given prefix from the request URL's Path.
|
||||||
- `PathPrefix`: PathPrefix adds a matcher for the URL path prefix. This matches if the given template is a prefix of the full URL path.
|
- `PathPrefix`: PathPrefix adds a matcher for the URL path prefixes. This matches if the given template is a prefix of the full URL path.
|
||||||
- `PathPrefixStrip`: Same as `PathPrefix` but strip the given prefix from the request URL's Path.
|
- `PathPrefixStrip`: Same as `PathPrefix` but strip the given prefix from the request URL's Path.
|
||||||
|
|
||||||
You can optionally enable `passHostHeader` to forward client `Host` header to the backend.
|
You can optionally enable `passHostHeader` to forward client `Host` header to the backend.
|
||||||
|
@ -77,21 +78,21 @@ Here is an example of frontends definition:
|
||||||
[frontends.frontend1]
|
[frontends.frontend1]
|
||||||
backend = "backend2"
|
backend = "backend2"
|
||||||
[frontends.frontend1.routes.test_1]
|
[frontends.frontend1.routes.test_1]
|
||||||
rule = "Host:test.localhost"
|
rule = "Host: test.localhost, test2.localhost"
|
||||||
[frontends.frontend2]
|
[frontends.frontend2]
|
||||||
backend = "backend1"
|
backend = "backend1"
|
||||||
passHostHeader = true
|
passHostHeader = true
|
||||||
entrypoints = ["https"] # overrides defaultEntryPoints
|
entrypoints = ["https"] # overrides defaultEntryPoints
|
||||||
[frontends.frontend2.routes.test_1]
|
[frontends.frontend2.routes.test_1]
|
||||||
rule = "Host:{subdomain:[a-z]+}.localhost"
|
rule = "Host: localhost, {subdomain:[a-z]+}.localhost"
|
||||||
[frontends.frontend3]
|
[frontends.frontend3]
|
||||||
backend = "backend2"
|
backend = "backend2"
|
||||||
rule = "Path:/test"
|
rule = "Path:/test"
|
||||||
```
|
```
|
||||||
|
|
||||||
- Three frontends are defined: `frontend1`, `frontend2` and `frontend3`
|
- Three frontends are defined: `frontend1`, `frontend2` and `frontend3`
|
||||||
- `frontend1` will forward the traffic to the `backend2` if the rule `Host:test.localhost` is matched
|
- `frontend1` will forward the traffic to the `backend2` if the rule `Host: test.localhost, test2.localhost` is matched
|
||||||
- `frontend2` will forward the traffic to the `backend1` if the rule `Host:{subdomain:[a-z]+}.localhost` is matched (forwarding client `Host` header to the backend)
|
- `frontend2` will forward the traffic to the `backend1` if the rule `Host: localhost, {subdomain:[a-z]+}.localhost` is matched (forwarding client `Host` header to the backend)
|
||||||
- `frontend3` will forward the traffic to the `backend2` if the rule `Path:/test` is matched
|
- `frontend3` will forward the traffic to the `backend2` if the rule `Path:/test` is matched
|
||||||
|
|
||||||
## Backends
|
## Backends
|
||||||
|
|
|
@ -8,17 +8,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 {
|
||||||
|
if p := strings.TrimPrefix(r.URL.Path, strings.TrimSpace(prefix)); len(p) < len(r.URL.Path) {
|
||||||
r.URL.Path = p
|
r.URL.Path = p
|
||||||
r.RequestURI = r.URL.RequestURI()
|
r.RequestURI = r.URL.RequestURI()
|
||||||
s.Handler.ServeHTTP(w, r)
|
s.Handler.ServeHTTP(w, r)
|
||||||
} else {
|
return
|
||||||
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,7 +10,7 @@ fi
|
||||||
|
|
||||||
# load ssh key
|
# load ssh key
|
||||||
echo "Loading key..."
|
echo "Loading key..."
|
||||||
openssl aes-256-cbc -K $encrypted_27087ae1f4db_key -iv $encrypted_27087ae1f4db_iv -in .travis/traefik.id_rsa.enc -out ~/.ssh/traefik.id_rsa -d
|
openssl aes-256-cbc -d -k "$pass" -in .travis/traefik.id_rsa.enc -out ~/.ssh/traefik.id_rsa
|
||||||
eval "$(ssh-agent -s)"
|
eval "$(ssh-agent -s)"
|
||||||
chmod 600 ~/.ssh/traefik.id_rsa
|
chmod 600 ~/.ssh/traefik.id_rsa
|
||||||
ssh-add ~/.ssh/traefik.id_rsa
|
ssh-add ~/.ssh/traefik.id_rsa
|
||||||
|
|
|
@ -58,7 +58,7 @@ 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.
|
||||||
|
@ -425,7 +425,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
|
||||||
}
|
}
|
||||||
// retry ?
|
// retry ?
|
||||||
if globalConfiguration.Retry != nil {
|
if globalConfiguration.Retry != nil {
|
||||||
retries := len(configuration.Backends[frontend.Backend].Servers) - 1
|
retries := len(configuration.Backends[frontend.Backend].Servers)
|
||||||
if globalConfiguration.Retry.Attempts > 0 {
|
if globalConfiguration.Retry.Attempts > 0 {
|
||||||
retries = globalConfiguration.Retry.Attempts
|
retries = globalConfiguration.Retry.Attempts
|
||||||
}
|
}
|
||||||
|
@ -471,9 +471,9 @@ 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 {
|
||||||
|
|
|
@ -513,7 +513,7 @@
|
||||||
# [frontends.frontend1]
|
# [frontends.frontend1]
|
||||||
# backend = "backend2"
|
# backend = "backend2"
|
||||||
# [frontends.frontend1.routes.test_1]
|
# [frontends.frontend1.routes.test_1]
|
||||||
# rule = "Host:test.localhost"
|
# rule = "Host: test.localhost, other.localhost"
|
||||||
# [frontends.frontend2]
|
# [frontends.frontend2]
|
||||||
# backend = "backend1"
|
# backend = "backend1"
|
||||||
# passHostHeader = true
|
# passHostHeader = true
|
||||||
|
@ -523,4 +523,4 @@
|
||||||
# [frontends.frontend3]
|
# [frontends.frontend3]
|
||||||
# entrypoints = ["http", "https"] # overrides defaultEntryPoints
|
# entrypoints = ["http", "https"] # overrides defaultEntryPoints
|
||||||
# backend = "backend2"
|
# backend = "backend2"
|
||||||
# rule = "Path:/test"
|
# rule = "Path: /test, /other"
|
Loading…
Reference in a new issue