1package server
2
3import (
4	"net/http"
5	"sync"
6
7	"github.com/gorilla/mux"
8)
9
10// routerSwapper is an http.Handler that allows you to swap
11// mux routers.
12type routerSwapper struct {
13	mu     sync.Mutex
14	router *mux.Router
15}
16
17// Swap changes the old router with the new one.
18func (rs *routerSwapper) Swap(newRouter *mux.Router) {
19	rs.mu.Lock()
20	rs.router = newRouter
21	rs.mu.Unlock()
22}
23
24// ServeHTTP makes the routerSwapper to implement the http.Handler interface.
25func (rs *routerSwapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
26	rs.mu.Lock()
27	router := rs.router
28	rs.mu.Unlock()
29	router.ServeHTTP(w, r)
30}
31