github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/apiv3/route/route.go (about)

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