-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
54 lines (45 loc) · 1 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package goworker
import (
"net/http"
"github.com/julienschmidt/httprouter"
"encoding/json"
"encoding/xml"
)
type Context struct {
response http.ResponseWriter
request *http.Request
params httprouter.Params
}
func (c *Context) ByName(name string) string{
return c.params.ByName(name)
}
func (c *Context) BindJson(body []byte , object interface{}) error {
return json.Unmarshal(body, object)
}
func (c *Context) WriteJson(object interface{}) error {
body, err := json.Marshal(object)
if err != nil {
return err
}
c.response.Write(body)
return err
}
func (c *Context) BindXML(body []byte , object interface{}) error {
return xml.Unmarshal(body, object)
}
func (c *Context) WriteXML(object interface{}) error {
body, err := xml.Marshal(object)
if err != nil {
return err
}
c.response.Write(body)
return err
}
func (c *Context)WriteStatusCode(code int) error {
c.response.WriteHeader(code)
return nil
}
func (c *Context)WriteString(body string) error {
c.response.Write([]byte(body))
return nil
}