2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2015-03-06 21:12:33 +02:00
|
|
|
"strings"
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2015-03-15 01:26:44 +02:00
|
|
|
// Context represents context for the current request. It holds request and
|
2015-03-25 19:03:26 +02:00
|
|
|
// response references, path parameters, data and registered handler for
|
|
|
|
// the route.
|
2015-03-01 19:45:13 +02:00
|
|
|
Context struct {
|
2015-03-02 20:19:30 +02:00
|
|
|
Request *http.Request
|
2015-03-06 06:46:17 +02:00
|
|
|
Response *response
|
2015-03-02 20:19:30 +02:00
|
|
|
params Params
|
|
|
|
store map[string]interface{}
|
2015-03-27 23:21:30 +02:00
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
store map[string]interface{}
|
|
|
|
)
|
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// P returns path parameter by index.
|
2015-03-01 19:45:13 +02:00
|
|
|
func (c *Context) P(i uint8) string {
|
|
|
|
return c.params[i].Value
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// Param returns path parameter by name.
|
2015-03-01 19:45:13 +02:00
|
|
|
func (c *Context) Param(n string) string {
|
|
|
|
return c.params.Get(n)
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// Bind decodes the payload into provided type based on Content-Type header.
|
2015-04-03 05:18:34 +02:00
|
|
|
func (c *Context) Bind(i interface{}) (err error) {
|
2015-03-06 21:12:33 +02:00
|
|
|
ct := c.Request.Header.Get(HeaderContentType)
|
2015-03-09 08:58:10 +02:00
|
|
|
if strings.HasPrefix(ct, MIMEJSON) {
|
2015-03-06 21:12:33 +02:00
|
|
|
dec := json.NewDecoder(c.Request.Body)
|
2015-04-03 05:18:34 +02:00
|
|
|
if err = dec.Decode(i); err != nil {
|
|
|
|
err = ErrBindJSON
|
|
|
|
}
|
2015-04-04 00:40:36 +02:00
|
|
|
} else if strings.HasPrefix(ct, MIMEForm) {
|
2015-03-06 21:12:33 +02:00
|
|
|
} else {
|
2015-04-03 05:18:34 +02:00
|
|
|
err = ErrUnsupportedContentType
|
2015-03-06 21:12:33 +02:00
|
|
|
}
|
2015-04-03 05:18:34 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-30 16:38:53 +02:00
|
|
|
// String sends a text/plain response with status code.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (c *Context) String(n int, s string) {
|
|
|
|
c.Response.Header().Set(HeaderContentType, MIMEText+"; charset=utf-8")
|
|
|
|
c.Response.WriteHeader(n)
|
|
|
|
c.Response.Write([]byte(s))
|
|
|
|
}
|
|
|
|
|
2015-03-30 16:38:53 +02:00
|
|
|
// JSON sends an application/json response with status code.
|
2015-04-03 05:18:34 +02:00
|
|
|
func (c *Context) JSON(n int, i interface{}) (err error) {
|
2015-03-06 06:46:17 +02:00
|
|
|
enc := json.NewEncoder(c.Response)
|
2015-03-09 08:58:10 +02:00
|
|
|
c.Response.Header().Set(HeaderContentType, MIMEJSON+"; charset=utf-8")
|
2015-03-06 06:46:17 +02:00
|
|
|
c.Response.WriteHeader(n)
|
|
|
|
if err := enc.Encode(i); err != nil {
|
2015-04-03 05:18:34 +02:00
|
|
|
err = ErrRenderJSON
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-03 05:18:34 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// func (c *Context) File(n int, file, name string) {
|
|
|
|
// }
|
2015-03-09 08:58:10 +02:00
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// Get retrieves data from the context.
|
2015-03-01 19:45:13 +02:00
|
|
|
func (c *Context) Get(key string) interface{} {
|
|
|
|
return c.store[key]
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:26:44 +02:00
|
|
|
// Set saves data in the context.
|
2015-03-01 19:45:13 +02:00
|
|
|
func (c *Context) Set(key string, val interface{}) {
|
|
|
|
c.store[key] = val
|
|
|
|
}
|
2015-03-06 06:46:17 +02:00
|
|
|
|
2015-03-30 16:38:53 +02:00
|
|
|
// Redirect redirects the request using http.Redirect with status code.
|
2015-03-06 06:46:17 +02:00
|
|
|
func (c *Context) Redirect(n int, url string) {
|
|
|
|
http.Redirect(c.Response, c.Request, url, n)
|
|
|
|
}
|
2015-03-06 21:12:33 +02:00
|
|
|
|
2015-04-02 14:02:52 +02:00
|
|
|
func (c *Context) reset(rw http.ResponseWriter, r *http.Request, e *Echo) {
|
2015-03-06 21:12:33 +02:00
|
|
|
c.Response.reset(rw)
|
|
|
|
c.Request = r
|
2015-04-02 14:02:52 +02:00
|
|
|
c.echo = e
|
2015-03-06 21:12:33 +02:00
|
|
|
}
|