2016-03-04 10:32:23 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2016-06-03 15:58:33 +00:00
|
|
|
"github.com/containous/mux"
|
2016-04-13 18:36:23 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
2016-03-04 10:32:23 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HandlerSwitcher allows hot switching of http.ServeMux
|
|
|
|
type HandlerSwitcher struct {
|
2016-04-13 18:36:23 +00:00
|
|
|
handler *safe.Safe
|
2016-03-04 10:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandlerSwitcher builds a new instance of HandlerSwitcher
|
|
|
|
func NewHandlerSwitcher(newHandler *mux.Router) (hs *HandlerSwitcher) {
|
|
|
|
return &HandlerSwitcher{
|
2016-04-13 18:36:23 +00:00
|
|
|
handler: safe.New(newHandler),
|
2016-03-04 10:32:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HandlerSwitcher) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2016-04-13 18:36:23 +00:00
|
|
|
handlerBackup := hs.handler.Get().(*mux.Router)
|
2016-03-04 10:32:23 +00:00
|
|
|
handlerBackup.ServeHTTP(rw, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandler returns the current http.ServeMux
|
|
|
|
func (hs *HandlerSwitcher) GetHandler() (newHandler *mux.Router) {
|
2016-04-13 18:36:23 +00:00
|
|
|
handler := hs.handler.Get().(*mux.Router)
|
|
|
|
return handler
|
2016-03-04 10:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateHandler safely updates the current http.ServeMux with a new one
|
|
|
|
func (hs *HandlerSwitcher) UpdateHandler(newHandler *mux.Router) {
|
2016-04-13 18:36:23 +00:00
|
|
|
hs.handler.Set(newHandler)
|
2016-03-04 10:32:23 +00:00
|
|
|
}
|