forked from go-chi/chi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Method and MethodFunc to Router, supporting string defined …
…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
Showing
4 changed files
with
58 additions
and
10 deletions.
There are no files selected for viewing
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
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 | ||
} |
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
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
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