Skip to content

Commit

Permalink
Merge branch 'hotfix/0.2.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Feb 17, 2015
2 parents 65b3a2e + a3ad170 commit cf5ef39
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 34 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -92,6 +117,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
}
Expand Down Expand Up @@ -192,7 +220,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
}
}
Expand Down

0 comments on commit cf5ef39

Please sign in to comment.