Skip to content

Commit

Permalink
refactor: some code improves
Browse files Browse the repository at this point in the history
  • Loading branch information
savsgio committed Mar 16, 2023
1 parent da16e53 commit c9bb37f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 30 deletions.
4 changes: 2 additions & 2 deletions atreugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func New(cfg Config) *Atreugo {
r := newRouter(cfg)

if cfg.NotFoundView != nil {
r.router.NotFound = viewToHandler(cfg.NotFoundView, r.cfg.errorView)
r.router.NotFound = viewToHandler(cfg.NotFoundView, r.errorView)
}

if cfg.MethodNotAllowedView != nil {
r.router.MethodNotAllowed = viewToHandler(cfg.MethodNotAllowedView, r.cfg.errorView)
r.router.MethodNotAllowed = viewToHandler(cfg.MethodNotAllowedView, r.errorView)
}

if cfg.PanicView != nil {
Expand Down
12 changes: 4 additions & 8 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ func newRouter(cfg Config) *Router {
router.HandleOPTIONS = false

return &Router{
cfg: &routerConfig{
errorView: cfg.ErrorView,
debug: cfg.Debug,
logger: cfg.Logger,
},
router: router,
errorView: cfg.ErrorView,
handleOPTIONS: true,
}
}
Expand Down Expand Up @@ -121,7 +117,7 @@ func (r *Router) handler(fn View, middle Middlewares) fasthttp.RequestHandler {
statusCode = fasthttp.StatusInternalServerError
}

r.cfg.errorView(actx, err, statusCode)
r.errorView(actx, err, statusCode)

break
} else if !actx.next {
Expand Down Expand Up @@ -183,10 +179,10 @@ func (r *Router) NewGroupPath(path string) *Router {
}

return &Router{
cfg: r.cfg,
parent: r,
router: r.router,
routerMutable: r.routerMutable,
errorView: r.errorView,
prefix: path,
group: groupFunc(path),
handleOPTIONS: r.handleOPTIONS,
Expand Down Expand Up @@ -353,7 +349,7 @@ func (r *Router) StaticCustom(url string, fs *StaticFS) *Path {
}

if fs.PathNotFound != nil {
ffs.PathNotFound = viewToHandler(fs.PathNotFound, r.cfg.errorView)
ffs.PathNotFound = viewToHandler(fs.PathNotFound, r.errorView)
}

stripSlashes := strings.Count(r.getGroupFullPath(url), "/")
Expand Down
20 changes: 8 additions & 12 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,12 @@ func TestRouter_newRouter(t *testing.T) {
t.Error("Router routerMutable is true")
}

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

if !isEqual(r.cfg.errorView, testConfig.ErrorView) {
t.Errorf("Router log == %p, want %p", r.cfg.errorView, testConfig.ErrorView)
}

if !isEqual(r.cfg.logger, testConfig.Logger) {
t.Errorf("Router log == %p, want %p", r.cfg.logger, testConfig.Logger)
if !r.handleOPTIONS {
t.Error("Router handleOPTIONS is false")
}
}

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

if g.handleOPTIONS != r.handleOPTIONS {
t.Errorf("Group router handleOPTIONS == '%v', want '%v'", g.handleOPTIONS, r.handleOPTIONS)
if !isEqual(g.errorView, r.errorView) {
t.Errorf("Group router errorView == '%p', want '%p'", g.errorView, r.errorView)
}

if !isEqual(g.cfg, r.cfg) {
t.Errorf("Group router config == %p, want %p", g.cfg, r.cfg)
if g.handleOPTIONS != r.handleOPTIONS {
t.Errorf("Group router handleOPTIONS == '%v', want '%v'", g.handleOPTIONS, r.handleOPTIONS)
}
}

Expand Down
9 changes: 1 addition & 8 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,24 +493,17 @@ type RequestCtx struct {
*fasthttp.RequestCtx
}

type routerConfig struct {
errorView ErrorView

debug bool
logger Logger
}

// Router dispatchs requests to different
// views via configurable routes (paths)
//
// It is prohibited copying Router values. Create new values instead.
type Router struct {
noCopy nocopy.NoCopy // nolint:structcheck,unused

cfg *routerConfig
parent *Router
router *fastrouter.Router
routerMutable bool
errorView ErrorView

prefix string
group *fastrouter.Group
Expand Down

0 comments on commit c9bb37f

Please sign in to comment.