mirror of
				https://github.com/traefik/traefik.git
				synced 2025-11-04 10:21:15 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			36 lines
		
	
	
		
			948 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			948 B
		
	
	
	
		
			Go
		
	
	
	
	
	
package middlewares
 | 
						|
 | 
						|
import (
 | 
						|
	"net/http"
 | 
						|
 | 
						|
	"github.com/traefik/traefik/v3/pkg/safe"
 | 
						|
)
 | 
						|
 | 
						|
// HTTPHandlerSwitcher allows hot switching of http.ServeMux.
 | 
						|
type HTTPHandlerSwitcher struct {
 | 
						|
	handler *safe.Safe
 | 
						|
}
 | 
						|
 | 
						|
// NewHandlerSwitcher builds a new instance of HTTPHandlerSwitcher.
 | 
						|
func NewHandlerSwitcher(newHandler http.Handler) (hs *HTTPHandlerSwitcher) {
 | 
						|
	return &HTTPHandlerSwitcher{
 | 
						|
		handler: safe.New(newHandler),
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func (h *HTTPHandlerSwitcher) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
 | 
						|
	handlerBackup := h.handler.Get().(http.Handler)
 | 
						|
	handlerBackup.ServeHTTP(rw, req)
 | 
						|
}
 | 
						|
 | 
						|
// GetHandler returns the current http.ServeMux.
 | 
						|
func (h *HTTPHandlerSwitcher) GetHandler() (newHandler http.Handler) {
 | 
						|
	handler := h.handler.Get().(http.Handler)
 | 
						|
	return handler
 | 
						|
}
 | 
						|
 | 
						|
// UpdateHandler safely updates the current http.ServeMux with a new one.
 | 
						|
func (h *HTTPHandlerSwitcher) UpdateHandler(newHandler http.Handler) {
 | 
						|
	h.handler.Set(newHandler)
 | 
						|
}
 |