Skip to content

Commit ac5e84d

Browse files
authored
feat(engine): Added OptionFunc and With (#3572)
* feat: Added `OptionFunc` and `With` * fix: `With(opts...)` must be after `New` * feat: improve New with * fix: test * optimize code * optimize nolint * optimize code Signed-off-by: Flc゛ <[email protected]> --------- Signed-off-by: Flc゛ <[email protected]>
1 parent 83fc767 commit ac5e84d

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

context_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ func TestContextRenderFile(t *testing.T) {
10001000
c.File("./gin.go")
10011001

10021002
assert.Equal(t, http.StatusOK, w.Code)
1003-
assert.Contains(t, w.Body.String(), "func New() *Engine {")
1003+
assert.Contains(t, w.Body.String(), "func New(opts ...OptionFunc) *Engine {")
10041004
// Content-Type='text/plain; charset=utf-8' when go version <= 1.16,
10051005
// else, Content-Type='text/x-go; charset=utf-8'
10061006
assert.NotEqual(t, "", w.Header().Get("Content-Type"))
@@ -1014,7 +1014,7 @@ func TestContextRenderFileFromFS(t *testing.T) {
10141014
c.FileFromFS("./gin.go", Dir(".", false))
10151015

10161016
assert.Equal(t, http.StatusOK, w.Code)
1017-
assert.Contains(t, w.Body.String(), "func New() *Engine {")
1017+
assert.Contains(t, w.Body.String(), "func New(opts ...OptionFunc) *Engine {")
10181018
// Content-Type='text/plain; charset=utf-8' when go version <= 1.16,
10191019
// else, Content-Type='text/x-go; charset=utf-8'
10201020
assert.NotEqual(t, "", w.Header().Get("Content-Type"))
@@ -1030,7 +1030,7 @@ func TestContextRenderAttachment(t *testing.T) {
10301030
c.FileAttachment("./gin.go", newFilename)
10311031

10321032
assert.Equal(t, 200, w.Code)
1033-
assert.Contains(t, w.Body.String(), "func New() *Engine {")
1033+
assert.Contains(t, w.Body.String(), "func New(opts ...OptionFunc) *Engine {")
10341034
assert.Equal(t, fmt.Sprintf("attachment; filename=\"%s\"", newFilename), w.Header().Get("Content-Disposition"))
10351035
}
10361036

@@ -1057,7 +1057,7 @@ func TestContextRenderUTF8Attachment(t *testing.T) {
10571057
c.FileAttachment("./gin.go", newFilename)
10581058

10591059
assert.Equal(t, 200, w.Code)
1060-
assert.Contains(t, w.Body.String(), "func New() *Engine {")
1060+
assert.Contains(t, w.Body.String(), "func New(opts ...OptionFunc) *Engine {")
10611061
assert.Equal(t, `attachment; filename*=UTF-8''`+url.QueryEscape(newFilename), w.Header().Get("Content-Disposition"))
10621062
}
10631063

gin.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ var regRemoveRepeatedChar = regexp.MustCompile("/{2,}")
4747
// HandlerFunc defines the handler used by gin middleware as return value.
4848
type HandlerFunc func(*Context)
4949

50+
// OptionFunc defines the function to change the default configuration
51+
type OptionFunc func(*Engine)
52+
5053
// HandlersChain defines a HandlerFunc slice.
5154
type HandlersChain []HandlerFunc
5255

@@ -182,7 +185,7 @@ var _ IRouter = (*Engine)(nil)
182185
// - ForwardedByClientIP: true
183186
// - UseRawPath: false
184187
// - UnescapePathValues: true
185-
func New() *Engine {
188+
func New(opts ...OptionFunc) *Engine {
186189
debugPrintWARNINGNew()
187190
engine := &Engine{
188191
RouterGroup: RouterGroup{
@@ -211,15 +214,15 @@ func New() *Engine {
211214
engine.pool.New = func() any {
212215
return engine.allocateContext(engine.maxParams)
213216
}
214-
return engine
217+
return engine.With(opts...)
215218
}
216219

217220
// Default returns an Engine instance with the Logger and Recovery middleware already attached.
218-
func Default() *Engine {
221+
func Default(opts ...OptionFunc) *Engine {
219222
debugPrintWARNINGDefault()
220223
engine := New()
221224
engine.Use(Logger(), Recovery())
222-
return engine
225+
return engine.With(opts...)
223226
}
224227

225228
func (engine *Engine) Handler() http.Handler {
@@ -313,6 +316,15 @@ func (engine *Engine) Use(middleware ...HandlerFunc) IRoutes {
313316
return engine
314317
}
315318

319+
// With returns a new Engine instance with the provided options.
320+
func (engine *Engine) With(opts ...OptionFunc) *Engine {
321+
for _, opt := range opts {
322+
opt(engine)
323+
}
324+
325+
return engine
326+
}
327+
316328
func (engine *Engine) rebuild404Handlers() {
317329
engine.allNoRoute = engine.combineHandlers(engine.noRoute)
318330
}

gin_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,37 @@ func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo)
696696

697697
func handlerTest1(c *Context) {}
698698
func handlerTest2(c *Context) {}
699+
700+
func TestNewOptionFunc(t *testing.T) {
701+
var fc = func(e *Engine) {
702+
e.GET("/test1", handlerTest1)
703+
e.GET("/test2", handlerTest2)
704+
705+
e.Use(func(c *Context) {
706+
c.Next()
707+
})
708+
}
709+
710+
r := New(fc)
711+
712+
routes := r.Routes()
713+
assertRoutePresent(t, routes, RouteInfo{Path: "/test1", Method: "GET", Handler: "github.com/gin-gonic/gin.handlerTest1"})
714+
assertRoutePresent(t, routes, RouteInfo{Path: "/test2", Method: "GET", Handler: "github.com/gin-gonic/gin.handlerTest2"})
715+
}
716+
717+
func TestWithOptionFunc(t *testing.T) {
718+
r := New()
719+
720+
r.With(func(e *Engine) {
721+
e.GET("/test1", handlerTest1)
722+
e.GET("/test2", handlerTest2)
723+
724+
e.Use(func(c *Context) {
725+
c.Next()
726+
})
727+
})
728+
729+
routes := r.Routes()
730+
assertRoutePresent(t, routes, RouteInfo{Path: "/test1", Method: "GET", Handler: "github.com/gin-gonic/gin.handlerTest1"})
731+
assertRoutePresent(t, routes, RouteInfo{Path: "/test2", Method: "GET", Handler: "github.com/gin-gonic/gin.handlerTest2"})
732+
}

0 commit comments

Comments
 (0)