Skip to content

Commit 33c3753

Browse files
committed
Merge branch 'hotfix/0.2.10'
2 parents cf5ef39 + e83a6fe commit 33c3753

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ Content-Length: 102
109109

110110
- Use timer to calculate duration of request handling:
111111
```go
112+
// Data is helper to construct JSON
113+
type Data map[string]interface{}
114+
112115
func main() {
113116
r := router.New()
114117
r.GET("/api/v1/settings/database/:db", func(c *router.Control) {
@@ -152,10 +155,14 @@ Content-Length: 143
152155
}
153156
```
154157

155-
- Custom handler with "Access-Control-Allow":
158+
- Custom handler with "Access-Control-Allow" options and compact JSON:
156159
```go
160+
// Data is helper to construct JSON
161+
type Data map[string]interface{}
162+
157163
func baseHandler(handle router.Handle) router.Handle {
158164
return func(c *router.Control) {
165+
c.CompactJSON(true)
159166
if origin := c.Request.Header.Get("Origin"); origin != "" {
160167
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
161168
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
@@ -164,14 +171,18 @@ func baseHandler(handle router.Handle) router.Handle {
164171
}
165172
}
166173

167-
func Hello(c *router.Control) {
168-
c.Body("Hello world")
174+
func Info(c *router.Control) {
175+
data := Data{
176+
"debug": true,
177+
"error": false,
178+
}
179+
c.Body(data)
169180
}
170181

171182
func main() {
172183
r := router.New()
173184
r.CustomHandler = baseHandler
174-
r.GET("/hello", Hello)
185+
r.GET("/info", Info)
175186

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

181192
- Check it:
182193
```sh
183-
curl -i -H 'Origin: http://foo.com' http://localhost:8888/hello/
194+
curl -i -H 'Origin: http://foo.com' http://localhost:8888/info/
184195

185196
HTTP/1.1 200 OK
186197
Access-Control-Allow-Credentials: true
187198
Access-Control-Allow-Origin: http://foo.com
188199
Content-Type: text/plain
189200
Date: Sun, 17 Aug 2014 13:27:10 GMT
190-
Content-Length: 11
201+
Content-Length: 28
191202

192-
Hello world
203+
{"debug":true,"error":false}
193204
```
194205

195206
## Author

control.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ type Control struct {
3030
// Code of HTTP status
3131
code int
3232

33+
// CompactJSON propery defines JSON output format (default is not compact)
34+
compactJSON bool
35+
3336
// Params is set of parameters
3437
Params []Param
3538

@@ -75,6 +78,11 @@ func (c *Control) Code(code int) *Control {
7578
return c
7679
}
7780

81+
// CompactJSON change JSON output format (default mode is false)
82+
func (c *Control) CompactJSON(mode bool) {
83+
c.compactJSON = mode
84+
}
85+
7886
// UseTimer allow caalculate elapsed time of request handling
7987
func (c *Control) UseTimer() {
8088
c.timer = time.Now()
@@ -91,13 +99,17 @@ func (c *Control) Body(data interface{}) {
9199
took := time.Now()
92100
data = &Header{Duration: took.Sub(c.timer), Took: took.Sub(c.timer).String(), Data: data}
93101
}
94-
jsn, err := json.MarshalIndent(data, "", " ")
102+
var err error
103+
if c.compactJSON {
104+
content, err = json.Marshal(data)
105+
} else {
106+
content, err = json.MarshalIndent(data, "", " ")
107+
}
95108
if err != nil {
96109
c.Writer.WriteHeader(http.StatusInternalServerError)
97110
return
98111
}
99112
c.Writer.Header().Add("Content-type", MIMEJSON)
100-
content = jsn
101113
}
102114
if c.code > 0 {
103115
c.Writer.WriteHeader(c.code)

router.go

Lines changed: 16 additions & 5 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.9 provides fast HTTP request router.
6+
Package router 0.2.10 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
@@ -51,6 +51,9 @@ Serve dynamic route with parameter:
5151
5252
Checks JSON Content-Type automatically:
5353
54+
// Data is helper to construct JSON
55+
type Data map[string]interface{}
56+
5457
func main() {
5558
r := router.New()
5659
r.GET("/settings/database/:db", func(c *router.Control) {
@@ -67,10 +70,14 @@ Checks JSON Content-Type automatically:
6770
r.Listen(":8888")
6871
}
6972
70-
Custom handler with "Access-Control-Allow":
73+
Custom handler with "Access-Control-Allow" options and compact JSON:
74+
75+
// Data is helper to construct JSON
76+
type Data map[string]interface{}
7177
7278
func baseHandler(handle router.Handle) router.Handle {
7379
return func(c *router.Control) {
80+
c.CompactJSON(true)
7481
if origin := c.Request.Header.Get("Origin"); origin != "" {
7582
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
7683
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
@@ -79,14 +86,18 @@ Custom handler with "Access-Control-Allow":
7986
}
8087
}
8188
82-
func Hello(c *router.Control) {
83-
c.Body("Hello world")
89+
func Info(c *router.Control) {
90+
data := Data{
91+
"debug": true,
92+
"error": false,
93+
}
94+
c.Body(data)
8495
}
8596
8697
func main() {
8798
r := router.New()
8899
r.CustomHandler = baseHandler
89-
r.GET("/hello", Hello)
100+
r.GET("/info", Info)
90101
91102
// Listen and serve on 0.0.0.0:8888
92103
r.Listen(":8888")

0 commit comments

Comments
 (0)