diff --git a/.gitignore b/.gitignore index e43b0f9..1db0b78 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +example/example diff --git a/README.md b/README.md index 872873e..aca956f 100644 --- a/README.md +++ b/README.md @@ -69,11 +69,14 @@ Hello John - Checks JSON Content-Type automatically: ```go +// Data is helper to construct JSON +type Data map[string]interface{} + func main() { r := router.New() r.GET("/api/v1/settings/database/:db", func(c *router.Control) { - data := map[string]map[string]string{ - "Database settings": { + data := Data{ + "Database settings": Data{ "database": c.Get(":db"), "host": "localhost", "port": "3306", @@ -113,8 +116,8 @@ func main() { // Do something - data := map[string]map[string]string{ - "Database settings": { + data := Data{ + "Database settings": Data{ "database": c.Get(":db"), "host": "localhost", "port": "3306", diff --git a/example/example.go b/example/example.go index c683b9c..450cc7d 100644 --- a/example/example.go +++ b/example/example.go @@ -4,6 +4,9 @@ import ( "github.com/takama/router" ) +// Data is helper to construct JSON +type Data map[string]interface{} + func main() { r := router.New() r.GET("/hello/:name", func(c *router.Control) { @@ -15,8 +18,8 @@ func main() { // Do something - data := map[string]map[string]string{ - "Database settings": { + data := Data{ + "Database settings": Data{ "database": c.Get(":db"), "host": "localhost", "port": "3306", diff --git a/router.go b/router.go index 90f045d..0d56d7e 100644 --- a/router.go +++ b/router.go @@ -3,7 +3,7 @@ // license that can be found in the LICENSE file. /* -Package router 0.2.3 provides fast HTTP request router. +Package router 0.2.4 provides fast HTTP request router. The router matches incoming requests by the request method and the path. If a handle is registered for this path and method, the router delegates the @@ -85,12 +85,12 @@ type Router struct { // NotFound is called when unknown HTTP method or a handler not found. // If it is not set, http.NotFound is used. // Please overwrite this if need your own NotFound handler. - NotFound http.HandlerFunc + NotFound Handle // PanicHandler is called when panic happen. // The handler prevents your server from crashing and should be used to return // http status code http.StatusInternalServerError (500) - PanicHandler func(c *Control) + PanicHandler Handle // Logger activates logging for each requests Logger bool @@ -174,8 +174,8 @@ func (r *Router) Listen(hostPort string) { func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { defer func() { if recovery := recover(); recovery != nil { - c := &Control{Request: req, Writer: w} if r.PanicHandler != nil { + c := &Control{Request: req, Writer: w} r.PanicHandler(c) } else { log.Println("Recovered in handler:", req.Method, req.URL.Path) @@ -204,7 +204,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { if len(allowed) == 0 { if r.NotFound != nil { - r.NotFound(w, req) + c := &Control{Request: req, Writer: w} + r.NotFound(c) } else { http.NotFound(w, req) }