Skip to content

Commit 39bade8

Browse files
ainsAinsley Escorce-Jones
authored andcommitted
Add improbable helpers
1 parent 5cd194b commit 39bade8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

improbable_helpers.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2015 All Right Reserved, Improbable Worlds Ltd.
2+
3+
// Improbable custom handlers for stuff.
4+
5+
package echo
6+
7+
import (
8+
"bytes"
9+
)
10+
11+
// WithMiddleware wraps the given handler directly with a set of middleware (in last to first order).
12+
func HandlerWithMiddleware(h Handler, middlewares ...Middleware) HandlerFunc {
13+
wh := wrapHandler(h)
14+
// Chain middleware with handler in the end
15+
for i := len(middlewares) - 1; i >= 0; i-- {
16+
m := middlewares[i]
17+
wrappedM := wrapMiddleware(m)
18+
wh = wrappedM(wh)
19+
}
20+
return wh
21+
}
22+
23+
// ParamMap returns a map of all parameters that result from the Path parameter expansion.
24+
func (c *Context) ParamMap() map[string]string {
25+
out := make(map[string]string)
26+
for i, _ := range c.pnames {
27+
out[c.pnames[i]] = c.pvalues[i]
28+
}
29+
return out
30+
}
31+
32+
// RenderWithContentType renders a template with data and sends the specified content type with
33+
// response and status code. Templates can be registered using `Echo.SetRenderer()`.
34+
func (c *Context) RenderWithContentType(code int, contentType string, name string, data interface{}) (err error) {
35+
if c.echo.renderer == nil {
36+
return ErrRendererNotRegistered
37+
}
38+
buf := new(bytes.Buffer)
39+
if err = c.echo.renderer.Render(buf, name, data); err != nil {
40+
return
41+
}
42+
c.response.Header().Set(ContentType, contentType)
43+
c.response.WriteHeader(code)
44+
c.response.Write(buf.Bytes())
45+
return
46+
}
47+
48+
// SetNotFoundHandler registers a custom handlers used when no route was found.
49+
func (e *Echo) SetNotFoundHandler(h HandlerFunc) {
50+
e.notFoundHandler = h
51+
}

0 commit comments

Comments
 (0)