@@ -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+
112115func 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+
157163func 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
171182func 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
185196HTTP/1.1 200 OK
186197Access-Control-Allow-Credentials: true
187198Access-Control-Allow-Origin: http://foo.com
188199Content-Type: text/plain
189200Date: 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
0 commit comments