Skip to content

Commit cf5ef39

Browse files
committed
Merge branch 'hotfix/0.2.9'
2 parents 65b3a2e + a3ad170 commit cf5ef39

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,46 @@ Content-Length: 143
152152
}
153153
```
154154

155+
- Custom handler with "Access-Control-Allow":
156+
```go
157+
func baseHandler(handle router.Handle) router.Handle {
158+
return func(c *router.Control) {
159+
if origin := c.Request.Header.Get("Origin"); origin != "" {
160+
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
161+
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
162+
}
163+
handle(c)
164+
}
165+
}
166+
167+
func Hello(c *router.Control) {
168+
c.Body("Hello world")
169+
}
170+
171+
func main() {
172+
r := router.New()
173+
r.CustomHandler = baseHandler
174+
r.GET("/hello", Hello)
175+
176+
// Listen and serve on 0.0.0.0:8888
177+
r.Listen(":8888")
178+
}
179+
```
180+
181+
- Check it:
182+
```sh
183+
curl -i -H 'Origin: http://foo.com' http://localhost:8888/hello/
184+
185+
HTTP/1.1 200 OK
186+
Access-Control-Allow-Credentials: true
187+
Access-Control-Allow-Origin: http://foo.com
188+
Content-Type: text/plain
189+
Date: Sun, 17 Aug 2014 13:27:10 GMT
190+
Content-Length: 11
191+
192+
Hello world
193+
```
194+
155195
## Author
156196

157197
[Igor Dolzhikov](https://github.com/takama)

router.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
/*
6-
Package router 0.2.8 provides fast HTTP request router.
6+
Package router 0.2.9 provides fast HTTP request router.
77
88
The router matches incoming requests by the request method and the path.
99
If a handle is registered for this path and method, the router delegates the
@@ -67,6 +67,31 @@ Checks JSON Content-Type automatically:
6767
r.Listen(":8888")
6868
}
6969
70+
Custom handler with "Access-Control-Allow":
71+
72+
func baseHandler(handle router.Handle) router.Handle {
73+
return func(c *router.Control) {
74+
if origin := c.Request.Header.Get("Origin"); origin != "" {
75+
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
76+
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
77+
}
78+
handle(c)
79+
}
80+
}
81+
82+
func Hello(c *router.Control) {
83+
c.Body("Hello world")
84+
}
85+
86+
func main() {
87+
r := router.New()
88+
r.CustomHandler = baseHandler
89+
r.GET("/hello", Hello)
90+
91+
// Listen and serve on 0.0.0.0:8888
92+
r.Listen(":8888")
93+
}
94+
7095
Go Router
7196
*/
7297
package router
@@ -92,6 +117,9 @@ type Router struct {
92117
// http status code http.StatusInternalServerError (500)
93118
PanicHandler Handle
94119

120+
// CustomHandler is called allways if defined
121+
CustomHandler func(Handle) Handle
122+
95123
// Logger activates logging user function for each requests
96124
Logger Handle
97125
}
@@ -192,7 +220,11 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
192220
if len(params) > 0 {
193221
c.Params = append(c.Params, params...)
194222
}
195-
handle(c)
223+
if r.CustomHandler != nil {
224+
r.CustomHandler(handle)(c)
225+
} else {
226+
handle(c)
227+
}
196228
return
197229
}
198230
}

0 commit comments

Comments
 (0)