2016-03-04 11:32:23 +01:00
package middlewares
import (
2016-12-30 09:21:13 +01:00
"net/http"
2020-09-16 15:46:04 +02:00
"github.com/traefik/traefik/v2/pkg/safe"
2016-03-04 11:32:23 +01:00
)
2020-05-11 12:06:07 +02:00
// HTTPHandlerSwitcher allows hot switching of http.ServeMux.
2019-03-14 09:30:04 +01:00
type HTTPHandlerSwitcher struct {
2016-04-13 20:36:23 +02:00
handler * safe . Safe
2016-03-04 11:32:23 +01:00
}
2020-05-11 12:06:07 +02:00
// NewHandlerSwitcher builds a new instance of HTTPHandlerSwitcher.
2019-03-14 09:30:04 +01:00
func NewHandlerSwitcher ( newHandler http . Handler ) ( hs * HTTPHandlerSwitcher ) {
return & HTTPHandlerSwitcher {
2016-04-13 20:36:23 +02:00
handler : safe . New ( newHandler ) ,
2016-03-04 11:32:23 +01:00
}
}
2019-03-14 09:30:04 +01:00
func ( h * HTTPHandlerSwitcher ) ServeHTTP ( rw http . ResponseWriter , req * http . Request ) {
2018-11-14 10:18:03 +01:00
handlerBackup := h . handler . Get ( ) . ( http . Handler )
handlerBackup . ServeHTTP ( rw , req )
2016-03-04 11:32:23 +01:00
}
2020-05-11 12:06:07 +02:00
// GetHandler returns the current http.ServeMux.
2019-03-14 09:30:04 +01:00
func ( h * HTTPHandlerSwitcher ) GetHandler ( ) ( newHandler http . Handler ) {
2018-11-14 10:18:03 +01:00
handler := h . handler . Get ( ) . ( http . Handler )
2016-04-13 20:36:23 +02:00
return handler
2016-03-04 11:32:23 +01:00
}
2020-05-11 12:06:07 +02:00
// UpdateHandler safely updates the current http.ServeMux with a new one.
2019-03-14 09:30:04 +01:00
func ( h * HTTPHandlerSwitcher ) UpdateHandler ( newHandler http . Handler ) {
2018-11-14 10:18:03 +01:00
h . handler . Set ( newHandler )
2016-03-04 11:32:23 +01:00
}