Add ServeHTTP() method to swappableMux struct

Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
This commit is contained in:
pancho horrillo
2019-10-08 15:06:08 +02:00
parent 3fa2357f71
commit 3b405bd9f6
2 changed files with 89 additions and 12 deletions
+8 -3
View File
@@ -1,6 +1,7 @@
package user
import (
"net/http"
"sync"
"github.com/gorilla/mux"
@@ -8,18 +9,22 @@ import (
type swappableMux struct {
m sync.RWMutex
root mux.Router
root *mux.Router
}
func (sm *swappableMux) get() mux.Router {
func (sm *swappableMux) get() *mux.Router {
sm.m.RLock()
defer sm.m.RUnlock()
return sm.root
}
func (sm *swappableMux) set(mux mux.Router) {
func (sm *swappableMux) set(mux *mux.Router) {
sm.m.Lock()
sm.root = mux
sm.m.Unlock()
}
func (sm *swappableMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sm.get().ServeHTTP(w, r)
}