2016-03-04 10:32:23 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2016-12-30 08:21:13 +00:00
|
|
|
"net/http"
|
|
|
|
|
2016-04-13 18:36:23 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
2016-03-04 10:32:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2018-11-14 09:18:03 +00:00
|
|
|
func NewHandlerSwitcher(newHandler http.Handler) (hs *HandlerSwitcher) {
|
2016-03-04 10:32:23 +00:00
|
|
|
return &HandlerSwitcher{
|
2016-04-13 18:36:23 +00:00
|
|
|
handler: safe.New(newHandler),
|
2016-03-04 10:32:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (h *HandlerSwitcher) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
handlerBackup := h.handler.Get().(http.Handler)
|
|
|
|
handlerBackup.ServeHTTP(rw, req)
|
2016-03-04 10:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandler returns the current http.ServeMux
|
2018-11-14 09:18:03 +00:00
|
|
|
func (h *HandlerSwitcher) GetHandler() (newHandler http.Handler) {
|
|
|
|
handler := h.handler.Get().(http.Handler)
|
2016-04-13 18:36:23 +00:00
|
|
|
return handler
|
2016-03-04 10:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateHandler safely updates the current http.ServeMux with a new one
|
2018-11-14 09:18:03 +00:00
|
|
|
func (h *HandlerSwitcher) UpdateHandler(newHandler http.Handler) {
|
|
|
|
h.handler.Set(newHandler)
|
2016-03-04 10:32:23 +00:00
|
|
|
}
|