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