From 5d06896f9432e86c31e9449774fb27380a115a17 Mon Sep 17 00:00:00 2001 From: Igor Dolzhikov Date: Tue, 17 Feb 2015 17:07:44 +0600 Subject: [PATCH 1/3] Add possibility to call custom handler --- router.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/router.go b/router.go index a6cd9ad..0507638 100644 --- a/router.go +++ b/router.go @@ -92,6 +92,9 @@ type Router struct { // http status code http.StatusInternalServerError (500) PanicHandler Handle + // CustomHandler is called allways if defined + CustomHandler func(Handle) Handle + // Logger activates logging user function for each requests Logger Handle } @@ -192,7 +195,11 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { if len(params) > 0 { c.Params = append(c.Params, params...) } - handle(c) + if r.CustomHandler != nil { + r.CustomHandler(handle)(c) + } else { + handle(c) + } return } } From 10d1777da27e2b042fd4d5a2e58770fc89e354c1 Mon Sep 17 00:00:00 2001 From: Igor Dolzhikov Date: Tue, 17 Feb 2015 17:23:47 +0600 Subject: [PATCH 2/3] extend comments with example --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ router.go | 25 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/README.md b/README.md index f89b127..a986836 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,46 @@ Content-Length: 143 } ``` +- Custom handler with "Access-Control-Allow": +```go +func baseHandler(handle router.Handle) router.Handle { + return func(c *router.Control) { + if origin := c.Request.Header.Get("Origin"); origin != "" { + c.Writer.Header().Set("Access-Control-Allow-Origin", origin) + c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") + } + handle(c) + } +} + +func Hello(c *router.Control) { + c.Body("Hello world") +} + +func main() { + r := router.New() + r.CustomHandler = baseHandler + r.GET("/hello", Hello) + + // Listen and serve on 0.0.0.0:8888 + r.Listen(":8888") +} +``` + +- Check it: +```sh +curl -i -H 'Origin: http://foo.com' http://localhost:8888/hello/ + +HTTP/1.1 200 OK +Access-Control-Allow-Credentials: true +Access-Control-Allow-Origin: http://foo.com +Content-Type: text/plain +Date: Sun, 17 Aug 2014 13:27:10 GMT +Content-Length: 11 + +Hello world +``` + ## Author [Igor Dolzhikov](https://github.com/takama) diff --git a/router.go b/router.go index 0507638..80259f5 100644 --- a/router.go +++ b/router.go @@ -67,6 +67,31 @@ Checks JSON Content-Type automatically: r.Listen(":8888") } +Custom handler with "Access-Control-Allow": + + func baseHandler(handle router.Handle) router.Handle { + return func(c *router.Control) { + if origin := c.Request.Header.Get("Origin"); origin != "" { + c.Writer.Header().Set("Access-Control-Allow-Origin", origin) + c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") + } + handle(c) + } + } + + func Hello(c *router.Control) { + c.Body("Hello world") + } + + func main() { + r := router.New() + r.CustomHandler = baseHandler + r.GET("/hello", Hello) + + // Listen and serve on 0.0.0.0:8888 + r.Listen(":8888") + } + Go Router */ package router From a3ad170ff010a626e3342b57e28c6c03ce5e27f1 Mon Sep 17 00:00:00 2001 From: Igor Dolzhikov Date: Tue, 17 Feb 2015 17:26:10 +0600 Subject: [PATCH 3/3] Bumped version number to 0.2.9 --- router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router.go b/router.go index 80259f5..7e8cabe 100644 --- a/router.go +++ b/router.go @@ -3,7 +3,7 @@ // license that can be found in the LICENSE file. /* -Package router 0.2.8 provides fast HTTP request router. +Package router 0.2.9 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