package http import ( "encoding/json" "net/http" ) var ( binder = &DefaultBinder{} ) type Context struct { request *http.Request response http.ResponseWriter params map[string]string } func (c *Context) Reset(r *http.Request, w http.ResponseWriter, params map[string]string) { c.request = r c.response = w c.params = params } func (c *Context) Request() *http.Request { return c.request } func (c *Context) Response() http.ResponseWriter { return c.response } func (c *Context) FormValue(name string) string { return c.request.FormValue(name) } func (c *Context) Bind(i interface{}) (err error) { return binder.Bind(i, c.Request()) } func (c *Context) Success(val interface{}) (err error) { var ( buf []byte ) if buf, err = json.Marshal(&Response{Result: val}); err == nil { c.response.Header().Set("Content-Type", "application/json") _, err = c.response.Write(buf) } return } func (c *Context) Error(code int, message string) (err error) { var ( buf []byte ) if buf, err = json.Marshal(&Response{Code: code, Message: message}); err == nil { c.response.Header().Set("Content-Type", "application/json") _, err = c.response.Write(buf) } return } func (c *Context) SetParam(name string,value string) { if c.params == nil{ c.params = make(map[string]string) } c.params[name] = value } func (c *Context) ParamValue(name string) string { if c.params != nil { return c.params[name] } return "" }