2015-03-27 14:21:30 -07:00
|
|
|
package echo
|
2015-03-01 09:45:13 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2015-05-20 14:38:51 -07:00
|
|
|
|
|
|
|
"golang.org/x/net/websocket"
|
2015-03-01 09:45:13 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2015-03-14 16:26:44 -07:00
|
|
|
// Context represents context for the current request. It holds request and
|
2015-05-17 22:54:29 -07:00
|
|
|
// response objects, path parameters, data and registered handler.
|
2015-03-01 09:45:13 -08:00
|
|
|
Context struct {
|
2015-05-22 04:40:01 -07:00
|
|
|
request *http.Request
|
|
|
|
response *Response
|
|
|
|
socket *websocket.Conn
|
2015-04-25 22:32:20 -07:00
|
|
|
pnames []string
|
|
|
|
pvalues []string
|
2015-04-05 14:21:03 -07:00
|
|
|
store store
|
2015-03-27 14:21:30 -07:00
|
|
|
echo *Echo
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
store map[string]interface{}
|
|
|
|
)
|
|
|
|
|
2015-05-08 11:52:06 -07:00
|
|
|
func NewContext(req *http.Request, res *Response, e *Echo) *Context {
|
|
|
|
return &Context{
|
2015-05-22 04:40:01 -07:00
|
|
|
request: req,
|
|
|
|
response: res,
|
2015-05-08 11:52:06 -07:00
|
|
|
echo: e,
|
|
|
|
pnames: make([]string, e.maxParam),
|
|
|
|
pvalues: make([]string, e.maxParam),
|
|
|
|
store: make(store),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 04:40:01 -07:00
|
|
|
// Request returns *http.Request.
|
|
|
|
func (c *Context) Request() *http.Request {
|
|
|
|
return c.request
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response returns *Response.
|
|
|
|
func (c *Context) Response() *Response {
|
|
|
|
return c.response
|
|
|
|
}
|
|
|
|
|
|
|
|
// Socket returns *websocket.Conn.
|
|
|
|
func (c *Context) Socket() *websocket.Conn {
|
|
|
|
return c.socket
|
|
|
|
}
|
|
|
|
|
2015-03-14 16:26:44 -07:00
|
|
|
// P returns path parameter by index.
|
2015-04-28 18:53:57 -07:00
|
|
|
func (c *Context) P(i uint8) (value string) {
|
|
|
|
l := uint8(len(c.pnames))
|
|
|
|
if i <= l {
|
2015-04-25 22:32:20 -07:00
|
|
|
value = c.pvalues[i]
|
|
|
|
}
|
|
|
|
return
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-03-14 16:26:44 -07:00
|
|
|
// Param returns path parameter by name.
|
2015-04-05 20:08:52 -07:00
|
|
|
func (c *Context) Param(name string) (value string) {
|
2015-04-26 09:48:49 -07:00
|
|
|
l := len(c.pnames)
|
2015-04-25 22:32:20 -07:00
|
|
|
for i, n := range c.pnames {
|
2015-04-26 09:48:49 -07:00
|
|
|
if n == name && i <= l {
|
2015-04-25 22:32:20 -07:00
|
|
|
value = c.pvalues[i]
|
|
|
|
break
|
2015-04-05 20:08:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-04-15 10:10:05 -07:00
|
|
|
// Bind binds the request body into specified type v. Default binder does it
|
|
|
|
// based on Content-Type header.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) Bind(i interface{}) error {
|
2015-05-22 04:40:01 -07:00
|
|
|
return c.echo.binder(c.request, i)
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-04-15 10:10:05 -07:00
|
|
|
// Render invokes the registered HTML template renderer and sends a text/html
|
2015-04-18 16:47:48 -07:00
|
|
|
// response with status code.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) Render(code int, name string, data interface{}) error {
|
2015-04-10 21:48:26 -07:00
|
|
|
if c.echo.renderer == nil {
|
2015-05-20 14:38:51 -07:00
|
|
|
return RendererNotRegistered
|
2015-04-10 21:48:26 -07:00
|
|
|
}
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.Header().Set(ContentType, TextHTML+"; charset=utf-8")
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
return c.echo.renderer.Render(c.response, name, data)
|
2015-03-29 23:35:08 -07:00
|
|
|
}
|
|
|
|
|
2015-03-30 07:38:53 -07:00
|
|
|
// JSON sends an application/json response with status code.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) JSON(code int, i interface{}) error {
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.Header().Set(ContentType, ApplicationJSON+"; charset=utf-8")
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
return json.NewEncoder(c.response).Encode(i)
|
2015-04-05 14:21:03 -07:00
|
|
|
}
|
|
|
|
|
2015-04-06 07:52:00 -07:00
|
|
|
// String sends a text/plain response with status code.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) String(code int, s string) error {
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.Header().Set(ContentType, TextPlain+"; charset=utf-8")
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
_, err := c.response.Write([]byte(s))
|
2015-05-20 14:38:51 -07:00
|
|
|
return err
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-04-07 13:02:23 -07:00
|
|
|
// HTML sends a text/html response with status code.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) HTML(code int, html string) error {
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.Header().Set(ContentType, TextHTML+"; charset=utf-8")
|
|
|
|
c.response.WriteHeader(code)
|
|
|
|
_, err := c.response.Write([]byte(html))
|
2015-05-20 14:38:51 -07:00
|
|
|
return err
|
2015-04-05 14:21:03 -07:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:47:48 -07:00
|
|
|
// NoContent sends a response with no body and a status code.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) NoContent(code int) error {
|
2015-05-22 04:40:01 -07:00
|
|
|
c.response.WriteHeader(code)
|
2015-04-18 16:47:48 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-21 14:02:29 -07:00
|
|
|
// Error invokes the registered HTTP error handler. Usually used by middleware.
|
2015-05-20 14:38:51 -07:00
|
|
|
func (c *Context) Error(err error) {
|
|
|
|
c.echo.httpErrorHandler(err, c)
|
2015-04-19 16:00:23 -07:00
|
|
|
}
|
2015-03-08 23:58:10 -07:00
|
|
|
|
2015-03-14 16:26:44 -07:00
|
|
|
// Get retrieves data from the context.
|
2015-03-01 09:45:13 -08:00
|
|
|
func (c *Context) Get(key string) interface{} {
|
|
|
|
return c.store[key]
|
|
|
|
}
|
|
|
|
|
2015-03-14 16:26:44 -07:00
|
|
|
// Set saves data in the context.
|
2015-03-01 09:45:13 -08:00
|
|
|
func (c *Context) Set(key string, val interface{}) {
|
|
|
|
c.store[key] = val
|
|
|
|
}
|
2015-03-05 20:46:17 -08:00
|
|
|
|
2015-03-30 07:38:53 -07:00
|
|
|
// Redirect redirects the request using http.Redirect with status code.
|
2015-04-05 14:21:03 -07:00
|
|
|
func (c *Context) Redirect(code int, url string) {
|
2015-05-22 04:40:01 -07:00
|
|
|
http.Redirect(c.response, c.request, url, code)
|
2015-03-05 20:46:17 -08:00
|
|
|
}
|
2015-03-06 11:12:33 -08:00
|
|
|
|
2015-04-07 13:02:23 -07:00
|
|
|
func (c *Context) reset(w http.ResponseWriter, r *http.Request, e *Echo) {
|
2015-05-22 04:40:01 -07:00
|
|
|
c.request = r
|
|
|
|
c.response.reset(w)
|
2015-04-02 05:02:52 -07:00
|
|
|
c.echo = e
|
2015-03-06 11:12:33 -08:00
|
|
|
}
|