Skip to content

Commit

Permalink
Introduce Method and MethodFunc to Router, supporting string defined …
Browse files Browse the repository at this point in the history
…http methods, along with example for custom http handler support
  • Loading branch information
Peter Kieltyka authored and Peter Kieltyka committed Jun 21, 2017
1 parent 8f717ba commit 26633cb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
35 changes: 35 additions & 0 deletions _examples/custom-handler/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"errors"
"net/http"

"github.com/pressly/chi"
)

type Handler func(w http.ResponseWriter, r *http.Request) error

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
// handle returned error here.
w.WriteHeader(503)
w.Write([]byte("bad"))
}
}

func main() {
r := chi.NewRouter()
r.Method("GET", "/", Handler(fooHandler))
http.ListenAndServe(":3333", r)
}

func fooHandler(w http.ResponseWriter, r *http.Request) error {
q := r.URL.Query().Get("err")

if q != "" {
return errors.New(q)
}

w.Write([]byte("foo"))
return nil
}
11 changes: 4 additions & 7 deletions chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ type Router interface {
Handle(pattern string, h http.Handler)
HandleFunc(pattern string, h http.HandlerFunc)

// TODO: support user-defined http method by string
// Handle(method, pattern string, h http.Handler)
// HandleFunc(method, pattern string, h http.HandlerFunc)

// TODO: considering having a catch-all http method route
// like how Handle() currently behaves in chi v2
// Any(pattern string, h http.HandlerFunc)
// Method and MethodFunc adds routes for `pattern` that matches
// the `method` HTTP method.
Method(method, pattern string, h http.Handler)
MethodFunc(method, pattern string, h http.HandlerFunc)

// HTTP-method routing along `pattern`
Connect(pattern string, h http.HandlerFunc)
Expand Down
16 changes: 16 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ func (mx *Mux) HandleFunc(pattern string, handlerFn http.HandlerFunc) {
mx.handle(mALL, pattern, handlerFn)
}

// Method adds the route `pattern` that matches `method` http method to
// execute the `handler` http.Handler.
func (mx *Mux) Method(method, pattern string, handler http.Handler) {
m, ok := methodMap[strings.ToUpper(method)]
if !ok {
panic(fmt.Sprintf("chi: '%s' http method is not supported.", method))
}
mx.handle(m, pattern, handler)
}

// MethodFunc adds the route `pattern` that matches `method` http method to
// execute the `handlerFn` http.HandlerFunc.
func (mx *Mux) MethodFunc(method, pattern string, handlerFn http.HandlerFunc) {
mx.Method(method, pattern, handlerFn)
}

// Connect adds the route `pattern` that matches a CONNECT http method to
// execute the `handlerFn` http.HandlerFunc.
func (mx *Mux) Connect(pattern string, handlerFn http.HandlerFunc) {
Expand Down
6 changes: 3 additions & 3 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func TestMuxBasic(t *testing.T) {
m.Use(exmw)
m.Use(logmw)
m.Get("/", cxindex)
m.Get("/ping", ping)
m.Get("/pingall", pingAll)
m.Get("/ping/all", pingAll)
m.Method("GET", "/ping", http.HandlerFunc(ping))
m.MethodFunc("GET", "/pingall", pingAll)
m.MethodFunc("get", "/ping/all", pingAll)
m.Get("/ping/all2", pingAll2)

m.Head("/ping", headPing)
Expand Down

0 comments on commit 26633cb

Please sign in to comment.