Skip to content

Instantly share code, notes, and snippets.

@cybersiddhu
Created April 24, 2017 20:07
Show Gist options
  • Select an option

  • Save cybersiddhu/830a6594a6bcc5450bbf23e2eee2a7f1 to your computer and use it in GitHub Desktop.

Select an option

Save cybersiddhu/830a6594a6bcc5450bbf23e2eee2a7f1 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"net/http"
"github.com/julienschmidt/httprouter"
)
const params = "params"
// Wrapper for httprouter
type RouterWrapper struct {
Router *httprouter.Router
}
// Creates a new instance
func NewRouter() *RouterWrapper {
return &RouterWrapper{Router: httprouter.New()}
}
// Delete is a shortcut for router.Handle("DELETE", path, handle)
func (r *RouterWrapper) Delete(path string, fn http.HandlerFunc) {
r.Router.DELETE(path, HandlerFunc(fn))
}
// Get is a shortcut for router.Handle("GET", path, handle)
func (r *RouterWrapper) Get(path string, fn http.HandlerFunc) {
r.Router.GET(path, HandlerFunc(fn))
}
// Head is a shortcut for router.Handle("HEAD", path, handle)
func (r *RouterWrapper) Head(path string, fn http.HandlerFunc) {
r.Router.HEAD(path, HandlerFunc(fn))
}
// Options is a shortcut for router.Handle("OPTIONS", path, handle)
func (r *RouterWrapper) Options(path string, fn http.HandlerFunc) {
r.Router.OPTIONS(path, HandlerFunc(fn))
}
// Patch is a shortcut for router.Handle("PATCH", path, handle)
func (r *RouterWrapper) Patch(path string, fn http.HandlerFunc) {
r.Router.PATCH(path, HandlerFunc(fn))
}
// Post is a shortcut for router.Handle("POST", path, handle)
func (r *RouterWrapper) Post(path string, fn http.HandlerFunc) {
r.Router.POST(path, HandlerFunc(fn))
}
// Put is a shortcut for router.Handle("PUT", path, handle)
func (r *RouterWrapper) Put(path string, fn http.HandlerFunc) {
r.Router.PUT(path, HandlerFunc(fn))
}
// HandlerFunc accepts the name of a function so you don't have to wrap it with http.HandlerFunc
// Example: r.GET("/", httprouterwrapper.HandlerFunc(controller.Index))
// Source: http://nicolasmerouze.com/guide-routers-golang/
func HandlerFunc(fn http.HandlerFunc) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ctx := context.WithValue(r.Context(), params, p)
fn(w, r.WithContext(ctx))
}
}
// Context returns the URL parameters
func Params(r *http.Request) httprouter.Params {
return r.Context().Value(params).(httprouter.Params)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment