github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/agent/replica/rest/router.go (about)

     1  package rest
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gorilla/mux"
     7  
     8  	"github.com/rancher/go-rancher/api"
     9  	"github.com/rancher/go-rancher/client"
    10  )
    11  
    12  func HandleError(s *client.Schemas, t func(http.ResponseWriter, *http.Request) error) http.Handler {
    13  	return api.ApiHandler(s, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
    14  		if err := t(rw, req); err != nil {
    15  			apiContext := api.GetApiContext(req)
    16  			apiContext.WriteErr(err)
    17  		}
    18  	}))
    19  }
    20  
    21  func NewRouter() *mux.Router {
    22  	s := &server{}
    23  	schemas := newSchema()
    24  	router := mux.NewRouter().StrictSlash(true)
    25  	f := HandleError
    26  
    27  	// API framework routes
    28  	router.Methods("GET").Path("/").Handler(api.VersionsHandler(schemas, "v1"))
    29  	router.Methods("GET").Path("/v1/schemas").Handler(api.SchemasHandler(schemas))
    30  	router.Methods("GET").Path("/v1/schemas/{id}").Handler(api.SchemaHandler(schemas))
    31  	router.Methods("GET").Path("/v1").Handler(api.VersionHandler(schemas, "v1"))
    32  
    33  	// Backup targets
    34  	router.Methods("POST").Path("/v1/backuptargets").Handler(f(schemas, s.ConfigureBackupTarget))
    35  
    36  	return router
    37  }
    38  
    39  type server struct{}