From 25b4b553deff52fd7259893a91055ece9d04b340 Mon Sep 17 00:00:00 2001 From: Andrew Hare Date: Tue, 8 Nov 2016 17:19:15 -0700 Subject: [PATCH 1/2] Export Router.addRoute --- router_setup.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/router_setup.go b/router_setup.go index c248dfb..ea42a9d 100644 --- a/router_setup.go +++ b/router_setup.go @@ -185,40 +185,40 @@ func (r *Router) OptionsHandler(fn interface{}) *Router { // Get will add a route to the router that matches on GET requests and the specified path. func (r *Router) Get(path string, fn interface{}) *Router { - return r.addRoute(httpMethodGet, path, fn) + return r.AddRoute(httpMethodGet, path, fn) } // Post will add a route to the router that matches on POST requests and the specified path. func (r *Router) Post(path string, fn interface{}) *Router { - return r.addRoute(httpMethodPost, path, fn) + return r.AddRoute(httpMethodPost, path, fn) } // Put will add a route to the router that matches on PUT requests and the specified path. func (r *Router) Put(path string, fn interface{}) *Router { - return r.addRoute(httpMethodPut, path, fn) + return r.AddRoute(httpMethodPut, path, fn) } // Delete will add a route to the router that matches on DELETE requests and the specified path. func (r *Router) Delete(path string, fn interface{}) *Router { - return r.addRoute(httpMethodDelete, path, fn) + return r.AddRoute(httpMethodDelete, path, fn) } // Patch will add a route to the router that matches on PATCH requests and the specified path. func (r *Router) Patch(path string, fn interface{}) *Router { - return r.addRoute(httpMethodPatch, path, fn) + return r.AddRoute(httpMethodPatch, path, fn) } // Head will add a route to the router that matches on HEAD requests and the specified path. func (r *Router) Head(path string, fn interface{}) *Router { - return r.addRoute(httpMethodHead, path, fn) + return r.AddRoute(httpMethodHead, path, fn) } // Options will add a route to the router that matches on OPTIONS requests and the specified path. func (r *Router) Options(path string, fn interface{}) *Router { - return r.addRoute(httpMethodOptions, path, fn) + return r.AddRoute(httpMethodOptions, path, fn) } -func (r *Router) addRoute(method httpMethod, path string, fn interface{}) *Router { +func (r *Router) AddRoute(method httpMethod, path string, fn interface{}) *Router { vfn := reflect.ValueOf(fn) validateHandler(vfn, r.contextType) fullPath := appendPath(r.pathPrefix, path) From 97b0e09f53a379f028d5b522a5c6ef27b1966b81 Mon Sep 17 00:00:00 2001 From: Andrew Hare Date: Wed, 9 Nov 2016 14:39:51 -0700 Subject: [PATCH 2/2] Export httpMethod --- router_serve.go | 4 ++-- router_setup.go | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/router_serve.go b/router_serve.go index 8f07909..24e0024 100644 --- a/router_serve.go +++ b/router_serve.go @@ -75,7 +75,7 @@ func middlewareStack(closure *middlewareClosure) NextMiddlewareFunc { // We could also 404 at this point: if so, run NotFound handlers and return. theRoute, wildcardMap := calculateRoute(closure.RootRouter, req) - if theRoute == nil && httpMethod(req.Method) == httpMethodOptions { + if theRoute == nil && HTTPMethod(req.Method) == httpMethodOptions { methods := make([]string, 0, len(httpMethods)) var lastLeaf *pathLeaf for _, method := range httpMethods { @@ -168,7 +168,7 @@ func (mw *middlewareHandler) invoke(ctx reflect.Value, rw ResponseWriter, req *R func calculateRoute(rootRouter *Router, req *Request) (*route, map[string]string) { var leaf *pathLeaf var wildcardMap map[string]string - method := httpMethod(req.Method) + method := HTTPMethod(req.Method) tree, ok := rootRouter.root[method] if ok { leaf, wildcardMap = tree.Match(req.URL.Path) diff --git a/router_setup.go b/router_setup.go index ea42a9d..0fc6aab 100644 --- a/router_setup.go +++ b/router_setup.go @@ -5,19 +5,19 @@ import ( "strings" ) -type httpMethod string +type HTTPMethod string const ( - httpMethodGet = httpMethod("GET") - httpMethodPost = httpMethod("POST") - httpMethodPut = httpMethod("PUT") - httpMethodDelete = httpMethod("DELETE") - httpMethodPatch = httpMethod("PATCH") - httpMethodHead = httpMethod("HEAD") - httpMethodOptions = httpMethod("OPTIONS") + httpMethodGet = HTTPMethod("GET") + httpMethodPost = HTTPMethod("POST") + httpMethodPut = HTTPMethod("PUT") + httpMethodDelete = HTTPMethod("DELETE") + httpMethodPatch = HTTPMethod("PATCH") + httpMethodHead = HTTPMethod("HEAD") + httpMethodOptions = HTTPMethod("OPTIONS") ) -var httpMethods = []httpMethod{httpMethodGet, httpMethodPost, httpMethodPut, httpMethodDelete, httpMethodPatch, httpMethodHead, httpMethodOptions} +var httpMethods = []HTTPMethod{httpMethodGet, httpMethodPost, httpMethodPut, httpMethodDelete, httpMethodPatch, httpMethodHead, httpMethodOptions} // Router implements net/http's Handler interface and is what you attach middleware, routes/handlers, and subrouters to. type Router struct { @@ -37,7 +37,7 @@ type Router struct { routes []*route // The root pathnode is the same for a tree of Routers - root map[httpMethod]*pathNode + root map[HTTPMethod]*pathNode // This can can be set on any router. The target's ErrorHandler will be invoked if it exists errorHandler reflect.Value @@ -66,7 +66,7 @@ type GenericHandler func(ResponseWriter, *Request) type route struct { Router *Router - Method httpMethod + Method HTTPMethod Path string Handler *actionHandler } @@ -95,7 +95,7 @@ func New(ctx interface{}) *Router { r.contextType = reflect.TypeOf(ctx) r.pathPrefix = "/" r.maxChildrenDepth = 1 - r.root = make(map[httpMethod]*pathNode) + r.root = make(map[HTTPMethod]*pathNode) for _, method := range httpMethods { r.root[method] = newPathNode() } @@ -218,7 +218,7 @@ func (r *Router) Options(path string, fn interface{}) *Router { return r.AddRoute(httpMethodOptions, path, fn) } -func (r *Router) AddRoute(method httpMethod, path string, fn interface{}) *Router { +func (r *Router) AddRoute(method HTTPMethod, path string, fn interface{}) *Router { vfn := reflect.ValueOf(fn) validateHandler(vfn, r.contextType) fullPath := appendPath(r.pathPrefix, path)