mirror of
				https://github.com/labstack/echo.git
				synced 2025-10-30 23:57:38 +02:00 
			
		
		
		
	Changed how options are passed to bolt
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
		
							
								
								
									
										47
									
								
								bolt.go
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								bolt.go
									
									
									
									
									
								
							| @@ -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) { | ||||
| 		b.maxParam = n | ||||
| 	} | ||||
| // 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) { | ||||
| 		b.notFoundHandler = h | ||||
| 	} | ||||
| // 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) { | ||||
| 		b.methodNotAllowedHandler = h | ||||
| 	} | ||||
| // 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) { | ||||
| 		b.internalServerErrorHandler = h | ||||
| 	} | ||||
| // SetInternalServerErrorHandler sets a custom InternalServerError handler. | ||||
| func (b *Bolt) SetInternalServerErrorHandler(h HandlerFunc) { | ||||
| 	b.internalServerErrorHandler = h | ||||
| } | ||||
|  | ||||
| // Use adds middleware to the chain. | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user