mirror of
				https://github.com/labstack/echo.git
				synced 2025-10-30 23:57:38 +02:00 
			
		
		
		
	Wrap method for using third party middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
		
							
								
								
									
										24
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal 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 | ||||
| @@ -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
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								bolt.go
									
									
									
									
									
								
							| @@ -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) | ||||
|   | ||||
| @@ -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
									
								
							
							
						
						
									
										11
									
								
								context_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| package bolt | ||||
|  | ||||
| import "testing" | ||||
|  | ||||
| func TestContextBind(t *testing.T) { | ||||
|  | ||||
| } | ||||
|  | ||||
| func TestContextJSON(t *testing.T) { | ||||
|  | ||||
| } | ||||
| @@ -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) | ||||
|   | ||||
| @@ -1 +0,0 @@ | ||||
| package middleware | ||||
							
								
								
									
										18
									
								
								router.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								router.go
									
									
									
									
									
								
							| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user