mirror of
https://github.com/labstack/echo.git
synced 2025-04-21 12:17:04 +02:00
Flexible handler and middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
99124cda0a
commit
e49e74aeac
@ -3,8 +3,9 @@ Bolt is a fast HTTP router (zero memory allocation) + micro web framework in Go.
|
|||||||
|
|
||||||
### Features
|
### Features
|
||||||
- Zippy router.
|
- Zippy router.
|
||||||
|
- Extensible middleware.
|
||||||
|
- Bring you own handler / middleware.
|
||||||
- Serve static files, including index.
|
- Serve static files, including index.
|
||||||
- Extensible middleware which also allows you to use third party handler / middleware.
|
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
https://github.com/labstack/bolt/tree/master/example
|
https://github.com/labstack/bolt/tree/master/example
|
||||||
|
131
bolt.go
131
bolt.go
@ -9,14 +9,17 @@ import (
|
|||||||
type (
|
type (
|
||||||
Bolt struct {
|
Bolt struct {
|
||||||
Router *router
|
Router *router
|
||||||
handlers []HandlerFunc
|
middleware []MiddlewareFunc
|
||||||
maxParam byte
|
maxParam byte
|
||||||
notFoundHandler HandlerFunc
|
notFoundHandler HandlerFunc
|
||||||
methodNotAllowedHandler HandlerFunc
|
methodNotAllowedHandler HandlerFunc
|
||||||
internalServerErrorHandler HandlerFunc
|
internalServerErrorHandler HandlerFunc
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
}
|
}
|
||||||
|
Handler interface{}
|
||||||
HandlerFunc func(*Context)
|
HandlerFunc func(*Context)
|
||||||
|
Middleware interface{}
|
||||||
|
MiddlewareFunc func(HandlerFunc) HandlerFunc
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -28,34 +31,21 @@ const (
|
|||||||
HeaderContentType = "Content-Type"
|
HeaderContentType = "Content-Type"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Methods is a map for looking up HTTP method index.
|
|
||||||
var Methods = map[string]uint8{
|
|
||||||
"CONNECT": 0,
|
|
||||||
"DELETE": 1,
|
|
||||||
"GET": 2,
|
|
||||||
"HEAD": 3,
|
|
||||||
"OPTIONS": 4,
|
|
||||||
"PATCH": 5,
|
|
||||||
"POST": 6,
|
|
||||||
"PUT": 7,
|
|
||||||
"TRACE": 8,
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a bolt instance.
|
// New creates a bolt instance.
|
||||||
func New() (b *Bolt) {
|
func New() (b *Bolt) {
|
||||||
b = &Bolt{
|
b = &Bolt{
|
||||||
maxParam: 5,
|
maxParam: 5,
|
||||||
notFoundHandler: func(c *Context) {
|
notFoundHandler: func(c *Context) {
|
||||||
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
c.Halt()
|
// c.Halt()
|
||||||
},
|
},
|
||||||
methodNotAllowedHandler: func(c *Context) {
|
methodNotAllowedHandler: func(c *Context) {
|
||||||
http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||||
c.Halt()
|
// c.Halt()
|
||||||
},
|
},
|
||||||
internalServerErrorHandler: func(c *Context) {
|
internalServerErrorHandler: func(c *Context) {
|
||||||
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
c.Halt()
|
// c.Halt()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
b.Router = NewRouter(b)
|
b.Router = NewRouter(b)
|
||||||
@ -64,13 +54,24 @@ func New() (b *Bolt) {
|
|||||||
Response: &response{},
|
Response: &response{},
|
||||||
params: make(Params, b.maxParam),
|
params: make(Params, b.maxParam),
|
||||||
store: make(store),
|
store: make(store),
|
||||||
i: -1,
|
// i: -1,
|
||||||
bolt: b,
|
bolt: b,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOP
|
||||||
|
func (h HandlerFunc) ServeHTTP(r http.ResponseWriter, w *http.Request) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bolt) Sub(prefix string, m ...MiddlewareFunc) *Bolt {
|
||||||
|
return &Bolt{
|
||||||
|
// prefix: b.prefix + prefix,
|
||||||
|
// middleware: append(b.handlers, handlers...),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MaxParam sets the max path params allowed. Default is 5, good enough for
|
// MaxParam sets the max path params allowed. Default is 5, good enough for
|
||||||
// many users.
|
// many users.
|
||||||
func (b *Bolt) MaxParam(n uint8) {
|
func (b *Bolt) MaxParam(n uint8) {
|
||||||
@ -92,74 +93,68 @@ func (b *Bolt) InternalServerErrorHandler(h HandlerFunc) {
|
|||||||
b.internalServerErrorHandler = h
|
b.internalServerErrorHandler = h
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chain adds middleware to the chain.
|
// Use adds handler to the middleware chain.
|
||||||
func (b *Bolt) Chain(h ...HandlerFunc) {
|
func (b *Bolt) Use(m ...Middleware) {
|
||||||
b.handlers = append(b.handlers, h...)
|
for _, h := range m {
|
||||||
}
|
b.middleware = append(b.middleware, wrapM(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.
|
// Connect adds a CONNECT route.
|
||||||
func (b *Bolt) Connect(path string, h ...HandlerFunc) {
|
func (b *Bolt) Connect(path string, h Handler) {
|
||||||
b.Handle("CONNECT", path, h)
|
b.Handle("CONNECT", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete adds a DELETE route.
|
// Delete adds a DELETE route.
|
||||||
func (b *Bolt) Delete(path string, h ...HandlerFunc) {
|
func (b *Bolt) Delete(path string, h Handler) {
|
||||||
b.Handle("DELETE", path, h)
|
b.Handle("DELETE", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get adds a GET route.
|
// Get adds a GET route.
|
||||||
func (b *Bolt) Get(path string, h ...HandlerFunc) {
|
func (b *Bolt) Get(path string, h Handler) {
|
||||||
b.Handle("GET", path, h)
|
b.Handle("GET", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head adds a HEAD route.
|
// Head adds a HEAD route.
|
||||||
func (b *Bolt) Head(path string, h ...HandlerFunc) {
|
func (b *Bolt) Head(path string, h Handler) {
|
||||||
b.Handle("HEAD", path, h)
|
b.Handle("HEAD", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options adds an OPTIONS route.
|
// Options adds an OPTIONS route.
|
||||||
func (b *Bolt) Options(path string, h ...HandlerFunc) {
|
func (b *Bolt) Options(path string, h Handler) {
|
||||||
b.Handle("OPTIONS", path, h)
|
b.Handle("OPTIONS", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patch adds a PATCH route.
|
// Patch adds a PATCH route.
|
||||||
func (b *Bolt) Patch(path string, h ...HandlerFunc) {
|
func (b *Bolt) Patch(path string, h Handler) {
|
||||||
b.Handle("PATCH", path, h)
|
b.Handle("PATCH", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post adds a POST route.
|
// Post adds a POST route.
|
||||||
func (b *Bolt) Post(path string, h ...HandlerFunc) {
|
func (b *Bolt) Post(path string, h Handler) {
|
||||||
b.Handle("POST", path, h)
|
b.Handle("POST", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put adds a PUT route.
|
// Put adds a PUT route.
|
||||||
func (b *Bolt) Put(path string, h ...HandlerFunc) {
|
func (b *Bolt) Put(path string, h Handler) {
|
||||||
b.Handle("PUT", path, h)
|
b.Handle("PUT", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trace adds a TRACE route.
|
// Trace adds a TRACE route.
|
||||||
func (b *Bolt) Trace(path string, h ...HandlerFunc) {
|
func (b *Bolt) Trace(path string, h Handler) {
|
||||||
b.Handle("TRACE", path, h)
|
b.Handle("TRACE", path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle adds method, path handler to the router.
|
// Handle adds method, path handler to the router.
|
||||||
func (b *Bolt) Handle(method, path string, h []HandlerFunc) {
|
func (b *Bolt) Handle(method, path string, h Handler) {
|
||||||
h = append(b.handlers, h...)
|
b.Router.Add(method, path, b.wrapH(h))
|
||||||
l := len(h)
|
// hs := append(b.middleware, wrap(h, false))
|
||||||
b.Router.Add(method, path, func(c *Context) {
|
// l := len(hs)
|
||||||
c.handlers = h
|
// b.Router.Add(method, path, func(c *Context) {
|
||||||
c.l = l
|
// c.handlers = hs
|
||||||
c.Next()
|
// c.l = l
|
||||||
})
|
// c.Next()
|
||||||
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Static serves static files.
|
// Static serves static files.
|
||||||
@ -183,10 +178,14 @@ func (b *Bolt) Index(file string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bolt) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
func (b *Bolt) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
// Find and execute handler
|
|
||||||
h, c, s := b.Router.Find(r.Method, r.URL.Path)
|
h, c, s := b.Router.Find(r.Method, r.URL.Path)
|
||||||
c.reset(rw, r)
|
c.reset(rw, r)
|
||||||
if h != nil {
|
if h != nil {
|
||||||
|
// Middleware
|
||||||
|
for i := len(b.middleware) - 1; i >= 0; i-- {
|
||||||
|
h = b.middleware[i](h)
|
||||||
|
}
|
||||||
|
// Handler
|
||||||
h(c)
|
h(c)
|
||||||
} else {
|
} else {
|
||||||
if s == NotFound {
|
if s == NotFound {
|
||||||
@ -202,6 +201,40 @@ func (b *Bolt) Run(addr string) {
|
|||||||
log.Fatal(http.ListenAndServe(addr, b))
|
log.Fatal(http.ListenAndServe(addr, b))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bolt) Stop(addr string) {
|
// wraps Handler
|
||||||
panic("implement it")
|
func (b *Bolt) wrapH(h Handler) HandlerFunc {
|
||||||
|
switch h := h.(type) {
|
||||||
|
case func(*Context):
|
||||||
|
return HandlerFunc(h)
|
||||||
|
case http.HandlerFunc:
|
||||||
|
return func(c *Context) {
|
||||||
|
h.ServeHTTP(c.Response, c.Request)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("bolt: unknown handler")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wraps Middleware
|
||||||
|
func wrapM(m Middleware) MiddlewareFunc {
|
||||||
|
switch m := m.(type) {
|
||||||
|
case func(HandlerFunc) HandlerFunc:
|
||||||
|
return MiddlewareFunc(m)
|
||||||
|
case func(http.ResponseWriter, *http.Request):
|
||||||
|
return func(h HandlerFunc) HandlerFunc {
|
||||||
|
return func(c *Context) {
|
||||||
|
m(c.Response, c.Request)
|
||||||
|
h(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case func(http.Handler) http.Handler:
|
||||||
|
return func(h HandlerFunc) HandlerFunc {
|
||||||
|
return func(c *Context) {
|
||||||
|
m(h).ServeHTTP(c.Response, c.Request)
|
||||||
|
h(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("bolt: unknown middleware")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
24
context.go
24
context.go
@ -16,8 +16,8 @@ type (
|
|||||||
params Params
|
params Params
|
||||||
handlers []HandlerFunc
|
handlers []HandlerFunc
|
||||||
store map[string]interface{}
|
store map[string]interface{}
|
||||||
l int // Handlers' length
|
// l int // Handlers' length
|
||||||
i int // Current handler index
|
// i int // Current handler index
|
||||||
bolt *Bolt
|
bolt *Bolt
|
||||||
}
|
}
|
||||||
store map[string]interface{}
|
store map[string]interface{}
|
||||||
@ -64,12 +64,12 @@ func (c *Context) JSON(n int, i interface{}) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// Next executes the next handler in the chain.
|
// Next executes the next handler in the chain.
|
||||||
func (c *Context) Next() {
|
// func (c *Context) Next() {
|
||||||
c.i++
|
// c.i++
|
||||||
if c.i < c.l {
|
// if c.i < c.l {
|
||||||
c.handlers[c.i](c)
|
// c.handlers[c.i](c)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Get retrieves data from the context.
|
// Get retrieves data from the context.
|
||||||
func (c *Context) Get(key string) interface{} {
|
func (c *Context) Get(key string) interface{} {
|
||||||
@ -89,10 +89,10 @@ func (c *Context) Redirect(n int, url string) {
|
|||||||
func (c *Context) reset(rw http.ResponseWriter, r *http.Request) {
|
func (c *Context) reset(rw http.ResponseWriter, r *http.Request) {
|
||||||
c.Response.reset(rw)
|
c.Response.reset(rw)
|
||||||
c.Request = r
|
c.Request = r
|
||||||
c.i = -1
|
// c.i = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Halt halts the current request.
|
// Halt halts the current request.
|
||||||
func (c *Context) Halt() {
|
// func (c *Context) Halt() {
|
||||||
c.i = c.l
|
// c.i = c.l
|
||||||
}
|
// }
|
||||||
|
@ -5,6 +5,8 @@ import (
|
|||||||
|
|
||||||
"github.com/labstack/bolt"
|
"github.com/labstack/bolt"
|
||||||
mw "github.com/labstack/bolt/middleware"
|
mw "github.com/labstack/bolt/middleware"
|
||||||
|
"github.com/rs/cors"
|
||||||
|
"github.com/thoas/stats"
|
||||||
)
|
)
|
||||||
|
|
||||||
type user struct {
|
type user struct {
|
||||||
@ -41,7 +43,7 @@ func getUser(c *bolt.Context) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
b := bolt.New()
|
b := bolt.New()
|
||||||
b.Chain(mw.Logger())
|
b.Use(mw.Logger)
|
||||||
b.Index("public/index.html")
|
b.Index("public/index.html")
|
||||||
b.Static("/js", "public/js")
|
b.Static("/js", "public/js")
|
||||||
b.Post("/users", createUser)
|
b.Post("/users", createUser)
|
||||||
|
@ -10,10 +10,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
BasicAuthFunc func(usr, pwd string) bool
|
BasicAuthFunc func(string, string) bool
|
||||||
AuthorizedHandler bolt.HandlerFunc
|
AuthorizedHandler bolt.HandlerFunc
|
||||||
UnauthorizedHandler func(c *bolt.Context, err error)
|
UnauthorizedHandler func(*bolt.Context, error)
|
||||||
JwtKeyFunc func(kid string) ([]byte, error)
|
JwtKeyFunc func(string) ([]byte, error)
|
||||||
Claims map[string]interface{}
|
Claims map[string]interface{}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ func JwtAuth(ah AuthorizedHandler, uah UnauthorizedHandler, fn JwtKeyFunc) bolt.
|
|||||||
if t.Valid {
|
if t.Valid {
|
||||||
c.Set("claims", Claims(t.Claims))
|
c.Set("claims", Claims(t.Claims))
|
||||||
ah(c)
|
ah(c)
|
||||||
c.Next()
|
// c.Next()
|
||||||
} else {
|
} else {
|
||||||
// TODO: capture errors
|
// TODO: capture errors
|
||||||
uah(c, err)
|
uah(c, err)
|
||||||
|
@ -8,10 +8,10 @@ import (
|
|||||||
"github.com/labstack/gommon/color"
|
"github.com/labstack/gommon/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Logger() bolt.HandlerFunc {
|
func Logger(h bolt.HandlerFunc) bolt.HandlerFunc {
|
||||||
return func(c *bolt.Context) {
|
return bolt.HandlerFunc(func(c *bolt.Context) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
c.Next()
|
h(c)
|
||||||
end := time.Now()
|
end := time.Now()
|
||||||
col := color.Green
|
col := color.Green
|
||||||
m := c.Request.Method
|
m := c.Request.Method
|
||||||
@ -28,5 +28,5 @@ func Logger() bolt.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s %s %s %s", m, p, col(s), end.Sub(start))
|
log.Printf("%s %s %s %s", m, p, col(s), end.Sub(start))
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
@ -17,14 +17,16 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *response) WriteHeader(c int) {
|
|
||||||
|
func (r *response) WriteHeader(n int) {
|
||||||
|
// TODO: fix when halted.
|
||||||
if r.committed {
|
if r.committed {
|
||||||
// TODO: Warning
|
// TODO: Warning
|
||||||
log.Println("bolt: response already committed")
|
log.Println("bolt: response already committed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.status = c
|
r.status = n
|
||||||
r.ResponseWriter.WriteHeader(c)
|
r.ResponseWriter.WriteHeader(n)
|
||||||
r.committed = true
|
r.committed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
32
router.go
32
router.go
@ -39,11 +39,24 @@ const (
|
|||||||
NotAllowed
|
NotAllowed
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// methods is a map for looking up HTTP method index.
|
||||||
|
var methods = map[string]uint8{
|
||||||
|
"CONNECT": 0,
|
||||||
|
"DELETE": 1,
|
||||||
|
"GET": 2,
|
||||||
|
"HEAD": 3,
|
||||||
|
"OPTIONS": 4,
|
||||||
|
"PATCH": 5,
|
||||||
|
"POST": 6,
|
||||||
|
"PUT": 7,
|
||||||
|
"TRACE": 8,
|
||||||
|
}
|
||||||
|
|
||||||
func NewRouter(b *Bolt) (r *router) {
|
func NewRouter(b *Bolt) (r *router) {
|
||||||
r = &router{
|
r = &router{
|
||||||
root: &node{
|
root: &node{
|
||||||
prefix: "",
|
prefix: "",
|
||||||
handlers: make([]HandlerFunc, len(Methods)),
|
handlers: make([]HandlerFunc, len(methods)),
|
||||||
edges: edges{},
|
edges: edges{},
|
||||||
},
|
},
|
||||||
bolt: b,
|
bolt: b,
|
||||||
@ -95,7 +108,7 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype) {
|
|||||||
cn.prefix = search
|
cn.prefix = search
|
||||||
cn.has = has
|
cn.has = has
|
||||||
if h != nil {
|
if h != nil {
|
||||||
cn.handlers[Methods[method]] = h
|
cn.handlers[methods[method]] = h
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
} else if l < pl {
|
} else if l < pl {
|
||||||
@ -107,15 +120,15 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype) {
|
|||||||
cn.label = cn.prefix[0]
|
cn.label = cn.prefix[0]
|
||||||
cn.prefix = cn.prefix[:l]
|
cn.prefix = cn.prefix[:l]
|
||||||
cn.has = snode
|
cn.has = snode
|
||||||
cn.handlers = make([]HandlerFunc, len(Methods))
|
cn.handlers = make([]HandlerFunc, len(methods))
|
||||||
|
|
||||||
if l == sl {
|
if l == sl {
|
||||||
// At parent node
|
// At parent node
|
||||||
cn.handlers[Methods[method]] = h
|
cn.handlers[methods[method]] = h
|
||||||
} else {
|
} else {
|
||||||
// Need to fork a node
|
// Need to fork a node
|
||||||
n = newNode(search[l:], has, nil, nil)
|
n = newNode(search[l:], has, nil, nil)
|
||||||
n.handlers[Methods[method]] = h
|
n.handlers[methods[method]] = h
|
||||||
cn.edges = append(cn.edges, n)
|
cn.edges = append(cn.edges, n)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@ -125,7 +138,7 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype) {
|
|||||||
if e == nil {
|
if e == nil {
|
||||||
n := newNode(search, has, nil, nil)
|
n := newNode(search, has, nil, nil)
|
||||||
if h != nil {
|
if h != nil {
|
||||||
n.handlers[Methods[method]] = h
|
n.handlers[methods[method]] = h
|
||||||
}
|
}
|
||||||
cn.edges = append(cn.edges, n)
|
cn.edges = append(cn.edges, n)
|
||||||
break
|
break
|
||||||
@ -135,7 +148,7 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype) {
|
|||||||
} else {
|
} else {
|
||||||
// Node already exists
|
// Node already exists
|
||||||
if h != nil {
|
if h != nil {
|
||||||
cn.handlers[Methods[method]] = h
|
cn.handlers[methods[method]] = h
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -151,7 +164,7 @@ func newNode(pfx string, has ntype, h []HandlerFunc, e edges) (n *node) {
|
|||||||
edges: e,
|
edges: e,
|
||||||
}
|
}
|
||||||
if h == nil {
|
if h == nil {
|
||||||
n.handlers = make([]HandlerFunc, len(Methods))
|
n.handlers = make([]HandlerFunc, len(methods))
|
||||||
}
|
}
|
||||||
if e == nil {
|
if e == nil {
|
||||||
n.edges = edges{}
|
n.edges = edges{}
|
||||||
@ -168,7 +181,7 @@ func (r *router) Find(method, path string) (handler HandlerFunc, c *Context, s S
|
|||||||
for {
|
for {
|
||||||
if search == "" || search == cn.prefix {
|
if search == "" || search == cn.prefix {
|
||||||
// Node found
|
// Node found
|
||||||
h := cn.handlers[Methods[method]]
|
h := cn.handlers[methods[method]]
|
||||||
if h != nil {
|
if h != nil {
|
||||||
// Handler found
|
// Handler found
|
||||||
handler = h
|
handler = h
|
||||||
@ -252,6 +265,7 @@ func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
r.bolt.pool.Put(c)
|
r.bolt.pool.Put(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get returns path parameter by name.
|
||||||
func (ps Params) Get(n string) (v string) {
|
func (ps Params) Get(n string) (v string) {
|
||||||
for _, p := range ps {
|
for _, p := range ps {
|
||||||
if p.Name == n {
|
if p.Name == n {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user