mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
parent
1ae7ef40e0
commit
d077efe91a
@ -109,7 +109,7 @@ func (c *Context) Bind(i interface{}) error {
|
|||||||
|
|
||||||
// Render renders a template with data and sends a text/html response with status
|
// Render renders a template with data and sends a text/html response with status
|
||||||
// code. Templates can be registered using `Echo.SetRenderer()`.
|
// code. Templates can be registered using `Echo.SetRenderer()`.
|
||||||
func (c *Context) Render(code int, name string, data interface{}) (err error) {
|
func (c *Context) Render(code int, name string, data interface{}) error {
|
||||||
if c.echo.renderer == nil {
|
if c.echo.renderer == nil {
|
||||||
return RendererNotRegistered
|
return RendererNotRegistered
|
||||||
}
|
}
|
||||||
@ -123,7 +123,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
|
|||||||
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
|
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
|
||||||
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
_, err = fmt.Fprintf(c.response, format, a...)
|
_, err = fmt.Fprintf(c.response, format, a...)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ func (c *Context) String(code int, format string, a ...interface{}) (err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JSON sends a JSON response with status code.
|
// JSON sends a JSON response with status code.
|
||||||
func (c *Context) JSON(code int, i interface{}) (err error) {
|
func (c *Context) JSON(code int, i interface{}) error {
|
||||||
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
return json.NewEncoder(c.response).Encode(i)
|
return json.NewEncoder(c.response).Encode(i)
|
||||||
@ -156,7 +156,7 @@ func (c *Context) JSONP(code int, callback string, i interface{}) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// XML sends an XML response with status code.
|
// XML sends an XML response with status code.
|
||||||
func (c *Context) XML(code int, i interface{}) (err error) {
|
func (c *Context) XML(code int, i interface{}) error {
|
||||||
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
c.response.Write([]byte(xml.Header))
|
c.response.Write([]byte(xml.Header))
|
||||||
|
19
echo.go
19
echo.go
@ -137,18 +137,6 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
methods = [...]string{
|
|
||||||
CONNECT,
|
|
||||||
DELETE,
|
|
||||||
GET,
|
|
||||||
HEAD,
|
|
||||||
OPTIONS,
|
|
||||||
PATCH,
|
|
||||||
POST,
|
|
||||||
PUT,
|
|
||||||
TRACE,
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------
|
//--------
|
||||||
// Errors
|
// Errors
|
||||||
//--------
|
//--------
|
||||||
@ -156,6 +144,7 @@ var (
|
|||||||
UnsupportedMediaType = errors.New("echo ⇒ unsupported media type")
|
UnsupportedMediaType = errors.New("echo ⇒ unsupported media type")
|
||||||
RendererNotRegistered = errors.New("echo ⇒ renderer not registered")
|
RendererNotRegistered = errors.New("echo ⇒ renderer not registered")
|
||||||
InvalidRedirectCode = errors.New("echo ⇒ invalid redirect status code")
|
InvalidRedirectCode = errors.New("echo ⇒ invalid redirect status code")
|
||||||
|
|
||||||
//----------------
|
//----------------
|
||||||
// Error handlers
|
// Error handlers
|
||||||
//----------------
|
//----------------
|
||||||
@ -222,7 +211,11 @@ func (e *Echo) Router() *Router {
|
|||||||
|
|
||||||
// ColoredLog enable/disable colored log.
|
// ColoredLog enable/disable colored log.
|
||||||
func (e *Echo) ColoredLog(on bool) {
|
func (e *Echo) ColoredLog(on bool) {
|
||||||
color.Disable()
|
if on {
|
||||||
|
color.Enable()
|
||||||
|
} else {
|
||||||
|
color.Disable()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP2 enable/disable HTTP2 support.
|
// HTTP2 enable/disable HTTP2 support.
|
||||||
|
19
router.go
19
router.go
@ -39,10 +39,8 @@ const (
|
|||||||
mtype
|
mtype
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRouter(e *Echo) (r *Router) {
|
func NewRouter(e *Echo) *Router {
|
||||||
r = &Router{
|
return &Router{
|
||||||
routes: []Route{},
|
|
||||||
echo: e,
|
|
||||||
connectTree: new(node),
|
connectTree: new(node),
|
||||||
deleteTree: new(node),
|
deleteTree: new(node),
|
||||||
getTree: new(node),
|
getTree: new(node),
|
||||||
@ -52,8 +50,9 @@ func NewRouter(e *Echo) (r *Router) {
|
|||||||
postTree: new(node),
|
postTree: new(node),
|
||||||
putTree: new(node),
|
putTree: new(node),
|
||||||
traceTree: new(node),
|
traceTree: new(node),
|
||||||
|
routes: []Route{},
|
||||||
|
echo: e,
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
||||||
@ -307,10 +306,12 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
// Search order static > param > match-any
|
// Search order static > param > match-any
|
||||||
for {
|
for {
|
||||||
if search == "" {
|
if search == "" {
|
||||||
// Found
|
if cn.handler != nil {
|
||||||
ctx.pnames = cn.pnames
|
// Found
|
||||||
h = cn.handler
|
ctx.pnames = cn.pnames
|
||||||
e = cn.echo
|
h = cn.handler
|
||||||
|
e = cn.echo
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,9 +316,6 @@ func TestRouterTwoParam(t *testing.T) {
|
|||||||
assert.Equal(t, "1", c.P(0))
|
assert.Equal(t, "1", c.P(0))
|
||||||
assert.Equal(t, "1", c.P(1))
|
assert.Equal(t, "1", c.P(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
h, _ = r.Find(GET, "/users/1", c)
|
|
||||||
assert.Nil(t, h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterMatchAny(t *testing.T) {
|
func TestRouterMatchAny(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user