github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/server/router_swapper.go (about)

     1  package server
     2  
     3  import (
     4  	"net/http"
     5  	"sync"
     6  
     7  	"github.com/gorilla/mux"
     8  )
     9  
    10  // routerSwapper is an http.Handler that allow you to swap
    11  // mux routers.
    12  type routerSwapper struct {
    13  	mu     sync.Mutex
    14  	router *mux.Router
    15  }
    16  
    17  // Swap changes the old router with the new one.
    18  func (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.
    25  func (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  }