mirror of
https://github.com/labstack/echo.git
synced 2025-07-05 00:58:47 +02:00
Changed how options are passed to bolt
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
39
bolt.go
39
bolt.go
@ -16,9 +16,6 @@ type (
|
|||||||
internalServerErrorHandler HandlerFunc
|
internalServerErrorHandler HandlerFunc
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
}
|
}
|
||||||
// Option is used to configure bolt. They are passed while creating a new
|
|
||||||
// instance of bolt.
|
|
||||||
Option func(*Bolt)
|
|
||||||
HandlerFunc func(*Context)
|
HandlerFunc func(*Context)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -45,7 +42,7 @@ var MethodMap = map[string]uint8{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New creates a bolt instance with options.
|
// New creates a bolt instance with options.
|
||||||
func New(opts ...Option) (b *Bolt) {
|
func New() (b *Bolt) {
|
||||||
b = &Bolt{
|
b = &Bolt{
|
||||||
maxParam: 5,
|
maxParam: 5,
|
||||||
notFoundHandler: func(c *Context) {
|
notFoundHandler: func(c *Context) {
|
||||||
@ -61,12 +58,6 @@ func New(opts ...Option) (b *Bolt) {
|
|||||||
c.Halt()
|
c.Halt()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set options
|
|
||||||
for _, o := range opts {
|
|
||||||
o(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.Router = NewRouter(b)
|
b.Router = NewRouter(b)
|
||||||
b.pool.New = func() interface{} {
|
b.pool.New = func() interface{} {
|
||||||
return &Context{
|
return &Context{
|
||||||
@ -77,39 +68,27 @@ func New(opts ...Option) (b *Bolt) {
|
|||||||
bolt: b,
|
bolt: b,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaxParam returns an option to set the max path param allowed. Default is 5,
|
// SetMaxParam sets the max path param. Default is 5, good enough for many users.
|
||||||
// good enough for many users.
|
func (b *Bolt) SetMaxParam(n uint8) {
|
||||||
func MaxParam(n uint8) Option {
|
|
||||||
return func(b *Bolt) {
|
|
||||||
b.maxParam = n
|
b.maxParam = n
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFoundHandler returns an option to set a custom NotFound hanlder.
|
// SetNotFoundHandler sets a custom NotFound handler.
|
||||||
func NotFoundHandler(h HandlerFunc) Option {
|
func (b *Bolt) SetNotFoundHandler(h HandlerFunc) {
|
||||||
return func(b *Bolt) {
|
|
||||||
b.notFoundHandler = h
|
b.notFoundHandler = h
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MethodNotAllowedHandler returns an option to set a custom MethodNotAllowed
|
// SetMethodNotAllowedHandler sets a custom MethodNotAllowed handler.
|
||||||
// handler.
|
func (b *Bolt) SetMethodNotAllowedHandler(h HandlerFunc) {
|
||||||
func MethodNotAllowedHandler(h HandlerFunc) Option {
|
|
||||||
return func(b *Bolt) {
|
|
||||||
b.methodNotAllowedHandler = h
|
b.methodNotAllowedHandler = h
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InternalServerErrorHandler returns an option to set a custom
|
// SetInternalServerErrorHandler sets a custom InternalServerError handler.
|
||||||
// InternalServerError handler.
|
func (b *Bolt) SetInternalServerErrorHandler(h HandlerFunc) {
|
||||||
func InternalServerErrorHandler(h HandlerFunc) Option {
|
|
||||||
return func(b *Bolt) {
|
|
||||||
b.internalServerErrorHandler = h
|
b.internalServerErrorHandler = h
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use adds middleware to the chain.
|
// Use adds middleware to the chain.
|
||||||
|
@ -21,14 +21,15 @@ var u = user{
|
|||||||
Name: "Joe",
|
Name: "Joe",
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaxParam(t *testing.T) {
|
func TestBoltMaxParam(t *testing.T) {
|
||||||
b := New(MaxParam(8))
|
b := New()
|
||||||
|
b.SetMaxParam(8)
|
||||||
if b.maxParam != 8 {
|
if b.maxParam != 8 {
|
||||||
t.Errorf("max param should be 8, found %d", b.maxParam)
|
t.Errorf("max param should be 8, found %d", b.maxParam)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIndex(t *testing.T) {
|
func TestBoltIndex(t *testing.T) {
|
||||||
b := New()
|
b := New()
|
||||||
b.Index("example/public/index.html")
|
b.Index("example/public/index.html")
|
||||||
r, _ := http.NewRequest("GET", "/", nil)
|
r, _ := http.NewRequest("GET", "/", nil)
|
||||||
@ -39,7 +40,7 @@ func TestIndex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStatic(t *testing.T) {
|
func TestBoltStatic(t *testing.T) {
|
||||||
b := New()
|
b := New()
|
||||||
b.Static("/js", "example/public/js")
|
b.Static("/js", "example/public/js")
|
||||||
r, _ := http.NewRequest("GET", "/js/main.js", nil)
|
r, _ := http.NewRequest("GET", "/js/main.js", nil)
|
||||||
|
@ -2,7 +2,7 @@ package bolt
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestStaticRoute(t *testing.T) {
|
func TestRouterStatic(t *testing.T) {
|
||||||
r := New().Router
|
r := New().Router
|
||||||
r.Add("GET", "/users/joe/books", func(c *Context) {})
|
r.Add("GET", "/users/joe/books", func(c *Context) {})
|
||||||
h, _, _ := r.Find("GET", "/users/joe/books")
|
h, _, _ := r.Find("GET", "/users/joe/books")
|
||||||
@ -11,7 +11,7 @@ func TestStaticRoute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParamRoute(t *testing.T) {
|
func TestRouterParam(t *testing.T) {
|
||||||
r := New().Router
|
r := New().Router
|
||||||
r.Add("GET", "/users/:name", func(c *Context) {})
|
r.Add("GET", "/users/:name", func(c *Context) {})
|
||||||
h, c, _ := r.Find("GET", "/users/joe")
|
h, c, _ := r.Find("GET", "/users/joe")
|
||||||
@ -24,7 +24,7 @@ func TestParamRoute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCatchAllRoute(t *testing.T) {
|
func TestRouterCatchAll(t *testing.T) {
|
||||||
r := New().Router
|
r := New().Router
|
||||||
r.Add("GET", "/static/*", func(c *Context) {})
|
r.Add("GET", "/static/*", func(c *Context) {})
|
||||||
h, _, _ := r.Find("GET", "/static/*")
|
h, _, _ := r.Find("GET", "/static/*")
|
||||||
@ -33,7 +33,7 @@ func TestCatchAllRoute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMicroParamRoute(t *testing.T) {
|
func TestRouterMicroParam(t *testing.T) {
|
||||||
r := New().Router
|
r := New().Router
|
||||||
r.Add("GET", "/:a/:b/:c", func(c *Context) {})
|
r.Add("GET", "/:a/:b/:c", func(c *Context) {})
|
||||||
h, c, _ := r.Find("GET", "/a/b/c")
|
h, c, _ := r.Find("GET", "/a/b/c")
|
||||||
|
Reference in New Issue
Block a user