From 9dcf2f8e2858f3314cf6b5035a7450ed481212f8 Mon Sep 17 00:00:00 2001 From: loveuer Date: Tue, 30 Jan 2024 11:01:58 +0800 Subject: [PATCH] update: route handlers execute rule(execute all -> manual next) --- ctx.go | 12 ++++++------ go.sum | 0 xtest/multihandler/main.go | 27 +++++++++++++++++++++++++++ xtest/multihandler/req.http | 5 +++++ 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 go.sum create mode 100644 xtest/multihandler/main.go create mode 100644 xtest/multihandler/req.http diff --git a/ctx.go b/ctx.go index 498ca68..c26910a 100644 --- a/ctx.go +++ b/ctx.go @@ -81,14 +81,14 @@ func (c *Ctx) Cookies(key string, defaultValue ...string) string { func (c *Ctx) Next() error { c.index++ - s := len(c.handlers) - for ; c.index < s; c.index++ { - if err := c.handlers[c.index](c); err != nil { - return err - } + + var err error + + if c.index < len(c.handlers) { + err = c.handlers[c.index](c) } - return nil + return err } /* =============================================================== diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/xtest/multihandler/main.go b/xtest/multihandler/main.go new file mode 100644 index 0000000..f995883 --- /dev/null +++ b/xtest/multihandler/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "github.com/loveuer/nf" + "log" +) + +func main() { + app := nf.New() + + app.Get("/nice", h1, h2) + + log.Fatal(app.Run(":3333")) +} + +func h1(c *nf.Ctx) error { + you := c.Query("to") + if you == "you" { + return c.JSON(nf.Map{"status": 201, "msg": "nice to meet you"}) + } + + return c.Next() +} + +func h2(c *nf.Ctx) error { + return c.JSON(nf.Map{"status": 200, "msg": "hello world"}) +} diff --git a/xtest/multihandler/req.http b/xtest/multihandler/req.http new file mode 100644 index 0000000..6f515b6 --- /dev/null +++ b/xtest/multihandler/req.http @@ -0,0 +1,5 @@ +### test multi handlers no next +GET http://127.0.0.1:3333/nice?to=you + +### test multi handlers do next +GET http://127.0.0.1:3333/nice?to=nf