1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Wrap method for using third party middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-03-21 14:59:09 -07:00
parent 16d1c799d6
commit 99124cda0a
8 changed files with 72 additions and 28 deletions

24
.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

View File

@ -2,9 +2,9 @@
Bolt is a fast HTTP router (zero memory allocation) + micro web framework in Go.
### Features
- Zippy router
- Serve static files including index
- Middleware
- Zippy router.
- Serve static files, including index.
- Extensible middleware which also allows you to use third party handler / middleware.
### Example
https://github.com/labstack/bolt/tree/master/example

36
bolt.go
View File

@ -28,8 +28,8 @@ const (
HeaderContentType = "Content-Type"
)
// MethodMap is an index lookup for HTTP methods.
var MethodMap = map[string]uint8{
// Methods is a map for looking up HTTP method index.
var Methods = map[string]uint8{
"CONNECT": 0,
"DELETE": 1,
"GET": 2,
@ -41,7 +41,7 @@ var MethodMap = map[string]uint8{
"TRACE": 8,
}
// New creates a bolt instance with options.
// New creates a bolt instance.
func New() (b *Bolt) {
b = &Bolt{
maxParam: 5,
@ -71,31 +71,41 @@ func New() (b *Bolt) {
return
}
// SetMaxParam sets the max path param. Default is 5, good enough for many users.
func (b *Bolt) SetMaxParam(n uint8) {
// MaxParam sets the max path params allowed. Default is 5, good enough for
// many users.
func (b *Bolt) MaxParam(n uint8) {
b.maxParam = n
}
// SetNotFoundHandler sets a custom NotFound handler.
func (b *Bolt) SetNotFoundHandler(h HandlerFunc) {
// NotFoundHandler sets a custom NotFound handler.
func (b *Bolt) NotFoundHandler(h HandlerFunc) {
b.notFoundHandler = h
}
// SetMethodNotAllowedHandler sets a custom MethodNotAllowed handler.
func (b *Bolt) SetMethodNotAllowedHandler(h HandlerFunc) {
// MethodNotAllowedHandler sets a custom MethodNotAllowed handler.
func (b *Bolt) MethodNotAllowedHandler(h HandlerFunc) {
b.methodNotAllowedHandler = h
}
// SetInternalServerErrorHandler sets a custom InternalServerError handler.
func (b *Bolt) SetInternalServerErrorHandler(h HandlerFunc) {
// InternalServerErrorHandler sets a custom InternalServerError handler.
func (b *Bolt) InternalServerErrorHandler(h HandlerFunc) {
b.internalServerErrorHandler = h
}
// Use adds middleware to the chain.
func (b *Bolt) Use(h ...HandlerFunc) {
// Chain adds middleware to the chain.
func (b *Bolt) Chain(h ...HandlerFunc) {
b.handlers = append(b.handlers, h...)
}
// Wrap wraps any http.Handler into bolt.HandlerFunc. It facilitates to use
// third party handler / middleware with bolt.
func (b *Bolt) Wrap(h http.Handler) HandlerFunc {
return func(c *Context) {
h.ServeHTTP(c.Response, c.Request)
c.Next()
}
}
// Connect adds a CONNECT route.
func (b *Bolt) Connect(path string, h ...HandlerFunc) {
b.Handle("CONNECT", path, h)

View File

@ -23,7 +23,7 @@ var u = user{
func TestBoltMaxParam(t *testing.T) {
b := New()
b.SetMaxParam(8)
b.MaxParam(8)
if b.maxParam != 8 {
t.Errorf("max param should be 8, found %d", b.maxParam)
}

11
context_test.go Normal file
View File

@ -0,0 +1,11 @@
package bolt
import "testing"
func TestContextBind(t *testing.T) {
}
func TestContextJSON(t *testing.T) {
}

View File

@ -41,7 +41,7 @@ func getUser(c *bolt.Context) {
func main() {
b := bolt.New()
b.Use(mw.Logger())
b.Chain(mw.Logger())
b.Index("public/index.html")
b.Static("/js", "public/js")
b.Post("/users", createUser)

View File

@ -1 +0,0 @@
package middleware

View File

@ -43,7 +43,7 @@ func NewRouter(b *Bolt) (r *router) {
r = &router{
root: &node{
prefix: "",
handlers: make([]HandlerFunc, len(MethodMap)),
handlers: make([]HandlerFunc, len(Methods)),
edges: edges{},
},
bolt: b,
@ -95,7 +95,7 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype) {
cn.prefix = search
cn.has = has
if h != nil {
cn.handlers[MethodMap[method]] = h
cn.handlers[Methods[method]] = h
}
return
} else if l < pl {
@ -107,15 +107,15 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype) {
cn.label = cn.prefix[0]
cn.prefix = cn.prefix[:l]
cn.has = snode
cn.handlers = make([]HandlerFunc, len(MethodMap))
cn.handlers = make([]HandlerFunc, len(Methods))
if l == sl {
// At parent node
cn.handlers[MethodMap[method]] = h
cn.handlers[Methods[method]] = h
} else {
// Need to fork a node
n = newNode(search[l:], has, nil, nil)
n.handlers[MethodMap[method]] = h
n.handlers[Methods[method]] = h
cn.edges = append(cn.edges, n)
}
break
@ -125,7 +125,7 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype) {
if e == nil {
n := newNode(search, has, nil, nil)
if h != nil {
n.handlers[MethodMap[method]] = h
n.handlers[Methods[method]] = h
}
cn.edges = append(cn.edges, n)
break
@ -135,7 +135,7 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype) {
} else {
// Node already exists
if h != nil {
cn.handlers[MethodMap[method]] = h
cn.handlers[Methods[method]] = h
}
break
}
@ -151,7 +151,7 @@ func newNode(pfx string, has ntype, h []HandlerFunc, e edges) (n *node) {
edges: e,
}
if h == nil {
n.handlers = make([]HandlerFunc, len(MethodMap))
n.handlers = make([]HandlerFunc, len(Methods))
}
if e == nil {
n.edges = edges{}
@ -168,7 +168,7 @@ func (r *router) Find(method, path string) (handler HandlerFunc, c *Context, s S
for {
if search == "" || search == cn.prefix {
// Node found
h := cn.handlers[MethodMap[method]]
h := cn.handlers[Methods[method]]
if h != nil {
// Handler found
handler = h