github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/rest/route/route.go (about)

     1  package route
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/evergreen-ci/evergreen/rest/data"
     7  	"github.com/gorilla/mux"
     8  )
     9  
    10  // routeManagerFactory is a function type used to create RouteManagers and used to register handlders.
    11  type routeManagerFactory func(string, int) *RouteManager
    12  
    13  // Route defines all of the functioning of a particular API route. It contains
    14  // implementations of the various API methods that are defined on this endpoint.
    15  type RouteManager struct {
    16  	// Methods is a slice containing all of the http methods (PUT, GET, DELETE, etc.)
    17  	// for this route.
    18  	Methods []MethodHandler
    19  
    20  	// Route is the path in the url that this resource handles.
    21  	Route string
    22  
    23  	// Version is the version number of the API that this route is associated with.
    24  	Version int
    25  }
    26  
    27  // Register builds http handlers for each of the defined methods and attaches
    28  // these to the given router.
    29  func (rm *RouteManager) Register(r *mux.Router, sc data.Connector) {
    30  	for _, method := range rm.Methods {
    31  		routeHandlerFunc := makeHandler(method, sc)
    32  		sr := r.PathPrefix(fmt.Sprintf("/%s/v%d/", sc.GetPrefix(), rm.Version)).Subrouter().StrictSlash(true)
    33  
    34  		sr.HandleFunc(rm.Route, routeHandlerFunc).Methods(method.MethodType)
    35  	}
    36  }