forked from labstack/armor
/
cube.go
41 lines (35 loc) · 767 Bytes
/
cube.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package plugin
import (
"net/http"
"github.com/labstack/cubex/go/cube"
"github.com/labstack/echo"
)
type (
Cube struct {
middleware *cube.Cube
Base `json:",squash"`
Path string `json:"path"`
Key string `json:"key"`
}
)
func (c *Cube) Init() (err error) {
m := cube.New()
m.Skipper = func(r *http.Request) bool {
return r.URL.Path == c.Path
}
c.middleware = m
c.Middleware = m.Middleware
c.Echo.GET(c.Path, func(ctx echo.Context) error {
if ctx.Request().Header.Get("X-Cube-Key") != c.Key {
return echo.ErrUnauthorized
}
return ctx.JSON(http.StatusOK, m.Data())
})
return
}
func (*Cube) Priority() int {
return -1
}
func (c *Cube) Process(next echo.HandlerFunc) echo.HandlerFunc {
return c.Middleware(next)
}