forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2015 All Right Reserved, Improbable Worlds Ltd. | ||
|
||
// Improbable custom handlers for stuff. | ||
|
||
package echo | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
// WithMiddleware wraps the given handler directly with a set of middleware (in last to first order). | ||
func HandlerWithMiddleware(h Handler, middlewares ...Middleware) HandlerFunc { | ||
wh := wrapHandler(h) | ||
// Chain middleware with handler in the end | ||
for i := len(middlewares) - 1; i >= 0; i-- { | ||
m := middlewares[i] | ||
wrappedM := wrapMiddleware(m) | ||
wh = wrappedM(wh) | ||
} | ||
return wh | ||
} | ||
|
||
// ParamMap returns a map of all parameters that result from the Path parameter expansion. | ||
func (c *Context) ParamMap() map[string]string { | ||
out := make(map[string]string) | ||
for i, _ := range c.pnames { | ||
out[c.pnames[i]] = c.pvalues[i] | ||
} | ||
return out | ||
} | ||
|
||
// RenderWithContentType renders a template with data and sends the specified content type with | ||
// response and status code. Templates can be registered using `Echo.SetRenderer()`. | ||
func (c *Context) RenderWithContentType(code int, contentType string, name string, data interface{}) (err error) { | ||
if c.echo.renderer == nil { | ||
return ErrRendererNotRegistered | ||
} | ||
buf := new(bytes.Buffer) | ||
if err = c.echo.renderer.Render(buf, name, data); err != nil { | ||
return | ||
} | ||
c.response.Header().Set(ContentType, contentType) | ||
c.response.WriteHeader(code) | ||
c.response.Write(buf.Bytes()) | ||
return | ||
} | ||
|
||
// SetNotFoundHandler registers a custom handlers used when no route was found. | ||
func (e *Echo) SetNotFoundHandler(h HandlerFunc) { | ||
e.notFoundHandler = h | ||
} |