Skip to content

Commit

Permalink
Merge branch 'hotfix/0.2.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Feb 21, 2015
2 parents cf5ef39 + e83a6fe commit 33c3753
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Content-Length: 102

- Use timer to calculate duration of request handling:
```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) {
Expand Down Expand Up @@ -152,10 +155,14 @@ Content-Length: 143
}
```

- Custom handler with "Access-Control-Allow":
- Custom handler with "Access-Control-Allow" options and compact JSON:
```go
// Data is helper to construct JSON
type Data map[string]interface{}

func baseHandler(handle router.Handle) router.Handle {
return func(c *router.Control) {
c.CompactJSON(true)
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")
Expand All @@ -164,14 +171,18 @@ func baseHandler(handle router.Handle) router.Handle {
}
}

func Hello(c *router.Control) {
c.Body("Hello world")
func Info(c *router.Control) {
data := Data{
"debug": true,
"error": false,
}
c.Body(data)
}

func main() {
r := router.New()
r.CustomHandler = baseHandler
r.GET("/hello", Hello)
r.GET("/info", Info)

// Listen and serve on 0.0.0.0:8888
r.Listen(":8888")
Expand All @@ -180,16 +191,16 @@ func main() {

- Check it:
```sh
curl -i -H 'Origin: http://foo.com' http://localhost:8888/hello/
curl -i -H 'Origin: http://foo.com' http://localhost:8888/info/

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
Content-Length: 28

Hello world
{"debug":true,"error":false}
```

## Author
Expand Down
16 changes: 14 additions & 2 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type Control struct {
// Code of HTTP status
code int

// CompactJSON propery defines JSON output format (default is not compact)
compactJSON bool

// Params is set of parameters
Params []Param

Expand Down Expand Up @@ -75,6 +78,11 @@ func (c *Control) Code(code int) *Control {
return c
}

// CompactJSON change JSON output format (default mode is false)
func (c *Control) CompactJSON(mode bool) {
c.compactJSON = mode
}

// UseTimer allow caalculate elapsed time of request handling
func (c *Control) UseTimer() {
c.timer = time.Now()
Expand All @@ -91,13 +99,17 @@ func (c *Control) Body(data interface{}) {
took := time.Now()
data = &Header{Duration: took.Sub(c.timer), Took: took.Sub(c.timer).String(), Data: data}
}
jsn, err := json.MarshalIndent(data, "", " ")
var err error
if c.compactJSON {
content, err = json.Marshal(data)
} else {
content, err = json.MarshalIndent(data, "", " ")
}
if err != nil {
c.Writer.WriteHeader(http.StatusInternalServerError)
return
}
c.Writer.Header().Add("Content-type", MIMEJSON)
content = jsn
}
if c.code > 0 {
c.Writer.WriteHeader(c.code)
Expand Down
21 changes: 16 additions & 5 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.9 provides fast HTTP request router.
Package router 0.2.10 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 @@ -51,6 +51,9 @@ Serve dynamic route with parameter:
Checks JSON Content-Type automatically:
// Data is helper to construct JSON
type Data map[string]interface{}
func main() {
r := router.New()
r.GET("/settings/database/:db", func(c *router.Control) {
Expand All @@ -67,10 +70,14 @@ Checks JSON Content-Type automatically:
r.Listen(":8888")
}
Custom handler with "Access-Control-Allow":
Custom handler with "Access-Control-Allow" options and compact JSON:
// Data is helper to construct JSON
type Data map[string]interface{}
func baseHandler(handle router.Handle) router.Handle {
return func(c *router.Control) {
c.CompactJSON(true)
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")
Expand All @@ -79,14 +86,18 @@ Custom handler with "Access-Control-Allow":
}
}
func Hello(c *router.Control) {
c.Body("Hello world")
func Info(c *router.Control) {
data := Data{
"debug": true,
"error": false,
}
c.Body(data)
}
func main() {
r := router.New()
r.CustomHandler = baseHandler
r.GET("/hello", Hello)
r.GET("/info", Info)
// Listen and serve on 0.0.0.0:8888
r.Listen(":8888")
Expand Down

0 comments on commit 33c3753

Please sign in to comment.