Move code to pkg
This commit is contained in:
parent
bd4c822670
commit
f1b085fa36
465 changed files with 656 additions and 680 deletions
33
pkg/middlewares/emptybackendhandler/empty_backend_handler.go
Normal file
33
pkg/middlewares/emptybackendhandler/empty_backend_handler.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package emptybackendhandler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/containous/traefik/pkg/healthcheck"
|
||||
)
|
||||
|
||||
// EmptyBackend is a middleware that checks whether the current Backend
|
||||
// has at least one active Server in respect to the healthchecks and if this
|
||||
// is not the case, it will stop the middleware chain and respond with 503.
|
||||
type emptyBackend struct {
|
||||
next healthcheck.BalancerHandler
|
||||
}
|
||||
|
||||
// New creates a new EmptyBackend middleware.
|
||||
func New(lb healthcheck.BalancerHandler) http.Handler {
|
||||
return &emptyBackend{next: lb}
|
||||
}
|
||||
|
||||
// ServeHTTP responds with 503 when there is no active Server and otherwise
|
||||
// invokes the next handler in the middleware chain.
|
||||
func (e *emptyBackend) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
if len(e.next.Servers()) == 0 {
|
||||
rw.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, err := rw.Write([]byte(http.StatusText(http.StatusServiceUnavailable)))
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
e.next.ServeHTTP(rw, req)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue