fix: next (not auto run all handlers)
This commit is contained in:
parent
039f4cf8c0
commit
7cf7ec32ac
15
app.go
15
app.go
@ -139,6 +139,10 @@ func (a *App) addRoute(method, path string, handlers ...HandlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) handleHTTPRequest(c *Ctx) {
|
func (a *App) handleHTTPRequest(c *Ctx) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
httpMethod := c.Request.Method
|
httpMethod := c.Request.Method
|
||||||
rPath := c.Request.URL.Path
|
rPath := c.Request.URL.Path
|
||||||
unescape := false
|
unescape := false
|
||||||
@ -166,8 +170,11 @@ func (a *App) handleHTTPRequest(c *Ctx) {
|
|||||||
if value.handlers != nil {
|
if value.handlers != nil {
|
||||||
c.handlers = value.handlers
|
c.handlers = value.handlers
|
||||||
c.fullPath = value.fullPath
|
c.fullPath = value.fullPath
|
||||||
// todo
|
|
||||||
c.Next()
|
if err = c.Next(); err != nil {
|
||||||
|
serveError(c, errorHandler)
|
||||||
|
}
|
||||||
|
|
||||||
c.writermem.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -210,8 +217,8 @@ func (a *App) handleHTTPRequest(c *Ctx) {
|
|||||||
serveError(c, a.config.NotFoundHandler)
|
serveError(c, a.config.NotFoundHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorHandler(c *Ctx) {
|
func errorHandler(c *Ctx) error {
|
||||||
_ = c.Status(500).SendString(_500)
|
return c.Status(500).SendString(_500)
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveError(c *Ctx, handler HandlerFunc) {
|
func serveError(c *Ctx, handler HandlerFunc) {
|
||||||
|
13
ctx.go
13
ctx.go
@ -110,17 +110,20 @@ func (c *Ctx) Cookies(key string, defaultValue ...string) string {
|
|||||||
func (c *Ctx) Next() error {
|
func (c *Ctx) Next() error {
|
||||||
c.index++
|
c.index++
|
||||||
|
|
||||||
var err error
|
var (
|
||||||
|
err error
|
||||||
|
handler = c.handlers[c.index]
|
||||||
|
)
|
||||||
|
|
||||||
for c.index < len(c.handlers) {
|
//for c.index < len(c.handlers) {
|
||||||
if c.handlers[c.index] != nil {
|
if handler != nil {
|
||||||
if err = c.handlers[c.index](c); err != nil {
|
if err = handler(c); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.index++
|
c.index++
|
||||||
}
|
//}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
34
xtest/midd2/main.go
Normal file
34
xtest/midd2/main.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/loveuer/nf"
|
||||||
|
"github.com/loveuer/nf/nft/resp"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := nf.New()
|
||||||
|
|
||||||
|
api := app.Group("/api")
|
||||||
|
|
||||||
|
api.Get("/hello",
|
||||||
|
auth(),
|
||||||
|
func(c *nf.Ctx) error {
|
||||||
|
return resp.Resp403(c, errors.New("in hello"))
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
log.Fatal(app.Run(":80"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func auth() nf.HandlerFunc {
|
||||||
|
return func(c *nf.Ctx) error {
|
||||||
|
token := c.Query("token")
|
||||||
|
if token != "zyp" {
|
||||||
|
return resp.Resp401(c, errors.New("no auth"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Next()
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user