From 137d4ee5c86dacc4cf4c1f49f5c0c13d6f0c81a1 Mon Sep 17 00:00:00 2001 From: loveuer Date: Mon, 19 Feb 2024 17:08:13 +0800 Subject: [PATCH] fix: middleware not work --- nf.go | 2 +- router.go | 4 +++- xtest/midd/main.go | 12 +++++++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/nf.go b/nf.go index 9692fc4..570ddd8 100644 --- a/nf.go +++ b/nf.go @@ -53,7 +53,7 @@ func New(config ...Config) *App { app.config = defaultConfig } - app.RouterGroup = &RouterGroup{app: app, prefix: "/"} + app.RouterGroup = &RouterGroup{app: app, prefix: ""} app.groups = []*RouterGroup{app.RouterGroup} if !app.config.DisableLogger { diff --git a/router.go b/router.go index b91da52..f412d21 100644 --- a/router.go +++ b/router.go @@ -64,10 +64,11 @@ func (r *router) getRoute(method string, path string) (*_node, map[string]string break } } + return n, params } - return nil, nil + return root, nil } func (r *router) getRoutes(method string) []*_node { @@ -90,6 +91,7 @@ func (r *router) handle(c *Ctx) error { c.params = params key := c.Method + "-" + node.pattern c.handlers = append(c.handlers, r.handlers[key]...) + //c.handlers = append(r.handlers[key], c.handlers...) } else { return c.app.config.NotFoundHandler(c) } diff --git a/xtest/midd/main.go b/xtest/midd/main.go index eebafce..fa13bc9 100644 --- a/xtest/midd/main.go +++ b/xtest/midd/main.go @@ -6,12 +6,15 @@ import ( ) func main() { - app := nf.New() + app := nf.New(nf.Config{DisableLogger: false}) - app.Use(ml()) app.Get("/hello", func(c *nf.Ctx) error { return c.SendString("world") }) + app.Get("/panic", func(c *nf.Ctx) error { + panic("panic") + }) + app.Use(ml()) log.Fatal(app.Run(":7777")) } @@ -19,6 +22,9 @@ func main() { func ml() nf.HandlerFunc { return func(c *nf.Ctx) error { log.Printf("[ML] [%s] - [%s]", c.Method, c.Path()) - return c.Next() + index := []byte(`

my not found

`) + c.Set("Content-Type", "text/html") + _, err := c.Write(index) + return err } }