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

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