Skip to content

Commit c9bb37f

Browse files
committed
refactor: some code improves
1 parent da16e53 commit c9bb37f

File tree

4 files changed

+15
-30
lines changed

4 files changed

+15
-30
lines changed

atreugo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ func New(cfg Config) *Atreugo {
5050
r := newRouter(cfg)
5151

5252
if cfg.NotFoundView != nil {
53-
r.router.NotFound = viewToHandler(cfg.NotFoundView, r.cfg.errorView)
53+
r.router.NotFound = viewToHandler(cfg.NotFoundView, r.errorView)
5454
}
5555

5656
if cfg.MethodNotAllowedView != nil {
57-
r.router.MethodNotAllowed = viewToHandler(cfg.MethodNotAllowedView, r.cfg.errorView)
57+
r.router.MethodNotAllowed = viewToHandler(cfg.MethodNotAllowedView, r.errorView)
5858
}
5959

6060
if cfg.PanicView != nil {

router.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,8 @@ func newRouter(cfg Config) *Router {
4949
router.HandleOPTIONS = false
5050

5151
return &Router{
52-
cfg: &routerConfig{
53-
errorView: cfg.ErrorView,
54-
debug: cfg.Debug,
55-
logger: cfg.Logger,
56-
},
5752
router: router,
53+
errorView: cfg.ErrorView,
5854
handleOPTIONS: true,
5955
}
6056
}
@@ -121,7 +117,7 @@ func (r *Router) handler(fn View, middle Middlewares) fasthttp.RequestHandler {
121117
statusCode = fasthttp.StatusInternalServerError
122118
}
123119

124-
r.cfg.errorView(actx, err, statusCode)
120+
r.errorView(actx, err, statusCode)
125121

126122
break
127123
} else if !actx.next {
@@ -183,10 +179,10 @@ func (r *Router) NewGroupPath(path string) *Router {
183179
}
184180

185181
return &Router{
186-
cfg: r.cfg,
187182
parent: r,
188183
router: r.router,
189184
routerMutable: r.routerMutable,
185+
errorView: r.errorView,
190186
prefix: path,
191187
group: groupFunc(path),
192188
handleOPTIONS: r.handleOPTIONS,
@@ -353,7 +349,7 @@ func (r *Router) StaticCustom(url string, fs *StaticFS) *Path {
353349
}
354350

355351
if fs.PathNotFound != nil {
356-
ffs.PathNotFound = viewToHandler(fs.PathNotFound, r.cfg.errorView)
352+
ffs.PathNotFound = viewToHandler(fs.PathNotFound, r.errorView)
357353
}
358354

359355
stripSlashes := strings.Count(r.getGroupFullPath(url), "/")

router_test.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,12 @@ func TestRouter_newRouter(t *testing.T) {
139139
t.Error("Router routerMutable is true")
140140
}
141141

142-
if !r.handleOPTIONS {
143-
t.Error("Router handleOPTIONS is false")
142+
if !isEqual(r.errorView, testConfig.ErrorView) {
143+
t.Errorf("Router errorView == %p, want %p", r.errorView, testConfig.ErrorView)
144144
}
145145

146-
if !isEqual(r.cfg.errorView, testConfig.ErrorView) {
147-
t.Errorf("Router log == %p, want %p", r.cfg.errorView, testConfig.ErrorView)
148-
}
149-
150-
if !isEqual(r.cfg.logger, testConfig.Logger) {
151-
t.Errorf("Router log == %p, want %p", r.cfg.logger, testConfig.Logger)
146+
if !r.handleOPTIONS {
147+
t.Error("Router handleOPTIONS is false")
152148
}
153149
}
154150

@@ -775,12 +771,12 @@ func TestRouter_NewGroupPath(t *testing.T) {
775771
t.Errorf("Group router routerMutable == '%v', want '%v'", g.routerMutable, r.routerMutable)
776772
}
777773

778-
if g.handleOPTIONS != r.handleOPTIONS {
779-
t.Errorf("Group router handleOPTIONS == '%v', want '%v'", g.handleOPTIONS, r.handleOPTIONS)
774+
if !isEqual(g.errorView, r.errorView) {
775+
t.Errorf("Group router errorView == '%p', want '%p'", g.errorView, r.errorView)
780776
}
781777

782-
if !isEqual(g.cfg, r.cfg) {
783-
t.Errorf("Group router config == %p, want %p", g.cfg, r.cfg)
778+
if g.handleOPTIONS != r.handleOPTIONS {
779+
t.Errorf("Group router handleOPTIONS == '%v', want '%v'", g.handleOPTIONS, r.handleOPTIONS)
784780
}
785781
}
786782

types.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,24 +493,17 @@ type RequestCtx struct {
493493
*fasthttp.RequestCtx
494494
}
495495

496-
type routerConfig struct {
497-
errorView ErrorView
498-
499-
debug bool
500-
logger Logger
501-
}
502-
503496
// Router dispatchs requests to different
504497
// views via configurable routes (paths)
505498
//
506499
// It is prohibited copying Router values. Create new values instead.
507500
type Router struct {
508501
noCopy nocopy.NoCopy // nolint:structcheck,unused
509502

510-
cfg *routerConfig
511503
parent *Router
512504
router *fastrouter.Router
513505
routerMutable bool
506+
errorView ErrorView
514507

515508
prefix string
516509
group *fastrouter.Group

0 commit comments

Comments
 (0)