fix: middleware not work

This commit is contained in:
loveuer 2024-02-19 17:08:13 +08:00
parent de3ce47671
commit 137d4ee5c8
3 changed files with 13 additions and 5 deletions

2
nf.go
View File

@ -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 {

View File

@ -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)
}

View File

@ -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(`<h1>my not found</h1>`)
c.Set("Content-Type", "text/html")
_, err := c.Write(index)
return err
}
}