From de3ce47671c228fe23269443bc5829f1bc254fb7 Mon Sep 17 00:00:00 2001 From: loveuer Date: Mon, 19 Feb 2024 16:00:56 +0800 Subject: [PATCH] fix: root middleware not work --- nf.go | 2 +- router.go | 2 +- xtest/midd/main.go | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 xtest/midd/main.go diff --git a/nf.go b/nf.go index d8b94f4..9692fc4 100644 --- a/nf.go +++ b/nf.go @@ -53,7 +53,7 @@ func New(config ...Config) *App { app.config = defaultConfig } - app.RouterGroup = &RouterGroup{app: app} + app.RouterGroup = &RouterGroup{app: app, prefix: "/"} app.groups = []*RouterGroup{app.RouterGroup} if !app.config.DisableLogger { diff --git a/router.go b/router.go index d796f8d..b91da52 100644 --- a/router.go +++ b/router.go @@ -48,7 +48,7 @@ func (r *router) getRoute(method string, path string) (*_node, map[string]string root, ok := r.roots[method] if !ok { - return nil, nil + return &_node{}, nil } n := root.search(searchParts, 0) diff --git a/xtest/midd/main.go b/xtest/midd/main.go new file mode 100644 index 0000000..eebafce --- /dev/null +++ b/xtest/midd/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/loveuer/nf" + "log" +) + +func main() { + app := nf.New() + + app.Use(ml()) + app.Get("/hello", func(c *nf.Ctx) error { + return c.SendString("world") + }) + + log.Fatal(app.Run(":7777")) +} + +func ml() nf.HandlerFunc { + return func(c *nf.Ctx) error { + log.Printf("[ML] [%s] - [%s]", c.Method, c.Path()) + return c.Next() + } +}