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

Changed how options are passed to bolt

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-03-18 23:51:32 -07:00
parent e7496010fc
commit 16d1c799d6
3 changed files with 22 additions and 42 deletions

39
bolt.go
View File

@ -16,9 +16,6 @@ type (
internalServerErrorHandler HandlerFunc
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)
)
@ -45,7 +42,7 @@ var MethodMap = map[string]uint8{
}
// New creates a bolt instance with options.
func New(opts ...Option) (b *Bolt) {
func New() (b *Bolt) {
b = &Bolt{
maxParam: 5,
notFoundHandler: func(c *Context) {
@ -61,12 +58,6 @@ func New(opts ...Option) (b *Bolt) {
c.Halt()
},
}
// Set options
for _, o := range opts {
o(b)
}
b.Router = NewRouter(b)
b.pool.New = func() interface{} {
return &Context{
@ -77,39 +68,27 @@ func New(opts ...Option) (b *Bolt) {
bolt: b,
}
}
return
}
// MaxParam returns an option to set the max path param allowed. Default is 5,
// good enough for many users.
func MaxParam(n uint8) Option {
return func(b *Bolt) {
// SetMaxParam sets the max path param. Default is 5, good enough for many users.
func (b *Bolt) SetMaxParam(n uint8) {
b.maxParam = n
}
}
// NotFoundHandler returns an option to set a custom NotFound hanlder.
func NotFoundHandler(h HandlerFunc) Option {
return func(b *Bolt) {
// SetNotFoundHandler sets a custom NotFound handler.
func (b *Bolt) SetNotFoundHandler(h HandlerFunc) {
b.notFoundHandler = h
}
}
// MethodNotAllowedHandler returns an option to set a custom MethodNotAllowed
// handler.
func MethodNotAllowedHandler(h HandlerFunc) Option {
return func(b *Bolt) {
// SetMethodNotAllowedHandler sets a custom MethodNotAllowed handler.
func (b *Bolt) SetMethodNotAllowedHandler(h HandlerFunc) {
b.methodNotAllowedHandler = h
}
}
// InternalServerErrorHandler returns an option to set a custom
// InternalServerError handler.
func InternalServerErrorHandler(h HandlerFunc) Option {
return func(b *Bolt) {
// SetInternalServerErrorHandler sets a custom InternalServerError handler.
func (b *Bolt) SetInternalServerErrorHandler(h HandlerFunc) {
b.internalServerErrorHandler = h
}
}
// Use adds middleware to the chain.

View File

@ -21,14 +21,15 @@ var u = user{
Name: "Joe",
}
func TestMaxParam(t *testing.T) {
b := New(MaxParam(8))
func TestBoltMaxParam(t *testing.T) {
b := New()
b.SetMaxParam(8)
if b.maxParam != 8 {
t.Errorf("max param should be 8, found %d", b.maxParam)
}
}
func TestIndex(t *testing.T) {
func TestBoltIndex(t *testing.T) {
b := New()
b.Index("example/public/index.html")
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.Static("/js", "example/public/js")
r, _ := http.NewRequest("GET", "/js/main.js", nil)

View File

@ -2,7 +2,7 @@ package bolt
import "testing"
func TestStaticRoute(t *testing.T) {
func TestRouterStatic(t *testing.T) {
r := New().Router
r.Add("GET", "/users/joe/books", func(c *Context) {})
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.Add("GET", "/users/:name", func(c *Context) {})
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.Add("GET", "/static/*", func(c *Context) {})
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.Add("GET", "/:a/:b/:c", func(c *Context) {})
h, c, _ := r.Find("GET", "/a/b/c")