Last active
November 24, 2017 17:01
-
-
Save saeid-ir/e9e1fc692721b4f663436c039af0f0b2 to your computer and use it in GitHub Desktop.
this package makes julienschmidt's httprouter support group routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package router | |
import ( | |
"github.com/julienschmidt/httprouter" | |
"net/http" | |
) | |
var h http.Handler | |
// SRouter is a http.Handler that wraps httprouter.Router with additional features. | |
type SRouter struct { | |
path string | |
router *httprouter.Router | |
} | |
// NewRouteGroup returns *SRouter with a new initialized httprouter.Router attached. | |
func NewGroup(r *httprouter.Router, path string) *SRouter { | |
return &SRouter{router: r, path: path} | |
} | |
// joinPath check if path start with '/' or not and return full path | |
func (r *SRouter) joinPath(path string) string { | |
if (r.path + path)[0] != '/' { | |
panic("path should start with '/' in path '" + path + "'.") | |
} | |
return r.path + path | |
} | |
// Group returns new *SRouter with given path and middlewares. | |
// It should be used for handles which have same path prefix or common middlewares. | |
func (r *SRouter) Group(path string) *SRouter { | |
//Strip trailing / (if present) as all added sub paths must start with a / | |
if path[len(path)-1] == '/' { | |
path = path[:len(path)-1] | |
} | |
return &SRouter{ | |
path: r.joinPath(path), | |
router: r.router, | |
} | |
} | |
// Handle registers a new request handle combined with middlewares. | |
func (r *SRouter) Handle(method, path string, handle httprouter.Handle) { | |
r.router.Handle(method, r.joinPath(path), handle) | |
} | |
// Handler is an adapter for http.Handler. | |
func (r *SRouter) Handler(method, path string, handler http.Handler) { | |
handle := func(w http.ResponseWriter, req *http.Request, _ httprouter.Params) { | |
handler.ServeHTTP(w, req) | |
} | |
r.Handle(method, path, handle) | |
} | |
// HandlerFunc is an adapter for http.HandlerFunc. | |
func (r *SRouter) HandlerFunc(method, path string, handler http.HandlerFunc) { | |
r.Handler(method, path, handler) | |
} | |
// ServeHTTP is an adapter for httprouter.ServeHTTP | |
func (r *SRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { | |
h = r.router | |
h.ServeHTTP(w, req) | |
} | |
// GET is a shortcut for SRouter.Handle("GET", path, handle) | |
func (r *SRouter) GET(path string, handle httprouter.Handle) { | |
r.Handle("GET", path, handle) | |
} | |
// HEAD is a shortcut for SRouter.Handle("HEAD", path, handle) | |
func (r *SRouter) HEAD(path string, handle httprouter.Handle) { | |
r.Handle("HEAD", path, handle) | |
} | |
// OPTIONS is a shortcut for SRouter.Handle("OPTIONS", path, handle) | |
func (r *SRouter) OPTIONS(path string, handle httprouter.Handle) { | |
r.Handle("OPTIONS", path, handle) | |
} | |
// POST is a shortcut for SRouter.Handle("POST", path, handle) | |
func (r *SRouter) POST(path string, handle httprouter.Handle) { | |
r.Handle("POST", path, handle) | |
} | |
// PUT is a shortcut for SRouter.Handle("PUT", path, handle) | |
func (r *SRouter) PUT(path string, handle httprouter.Handle) { | |
r.Handle("PUT", path, handle) | |
} | |
// PATCH is a shortcut for SRouter.Handle("PATCH", path, handle) | |
func (r *SRouter) PATCH(path string, handle httprouter.Handle) { | |
r.Handle("PATCH", path, handle) | |
} | |
// DELETE is a shortcut for SRouter.Handle("DELETE", path, handle) | |
func (r *SRouter) DELETE(path string, handle httprouter.Handle) { | |
r.Handle("DELETE", path, handle) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment