From b0894d1486a54a7a2988a2bba5338690d523f31b Mon Sep 17 00:00:00 2001 From: saiumesh535 Date: Wed, 15 Jul 2020 16:05:24 +0530 Subject: [PATCH] added handler level middleware --- example/main.go | 11 +++++++++++ router.go | 39 ++++++++++++++++++++++++++++----------- server.go | 12 ++++++------ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/example/main.go b/example/main.go index 4f443e8..5cccb21 100644 --- a/example/main.go +++ b/example/main.go @@ -107,5 +107,16 @@ func main() { return ctx.UploadFile("static/login.js", "login.js") }) + // you can have router level middleware which works in reverse way + app.Get("/routermiddleware", func(ctx *ferry.Ctx) error { + return ctx.Send(http.StatusOK, "hola!") + }, func(ctx *ferry.Ctx) error { + fmt.Println("this prints second") + return ctx.Next() + }, func(ctx *ferry.Ctx) error { + fmt.Println("this prints first") + return ctx.Next() + }) + log.Fatal(app.Listen("localhost:3000")) } diff --git a/router.go b/router.go index 7af0597..9e29018 100644 --- a/router.go +++ b/router.go @@ -16,7 +16,7 @@ type errHandler func(ctx *Ctx, err error) error type router struct { routerPath string regexPath string - handler handler + handlers []handler } type group struct { @@ -35,13 +35,13 @@ var ( var routerRegexReplace = "[a-zA-Z0-9_-]*" -func (g *group) addRoute(method, path string, h handler) { +func (g *group) addRoute(method, path string, h ...handler) { groupPath := fmt.Sprintf("%s%s", g.path, path) pathWithRegex := findAndReplace(groupPath) g.ferry.routerMap[method] = append(g.ferry.routerMap[method], router{ routerPath: groupPath, regexPath: pathWithRegex, - handler: h, + handlers: h, }) } @@ -143,18 +143,35 @@ func findAndReplace(path string) string { func handleRouter(ctx *Ctx, ferry *Ferry, routers []router) { urlPath := string(ctx.RequestCtx.Path()) query := ctx.RequestCtx.QueryArgs() - for _, route := range routers { - match, _ := regexp.MatchString(route.regexPath, urlPath) + var route *router + for _, r := range routers { + match, _ := regexp.MatchString(r.regexPath, urlPath) if match { - ctx.routerPath = route.routerPath - ctx.queryPath = query.String() - if err := route.handler(ctx); err != nil { - handlerRouterError(err, ctx, ferry) + route = &r + break + } + } + if route != nil { + ctx.routerPath = route.routerPath + ctx.queryPath = query.String() + index := 0 + var next func() error + next = func() error { + index = index + 1 + if index <= len(route.handlers)-1 { + if err := route.handlers[len(route.handlers)-1-index](ctx); err != nil { + handlerRouterError(err, ctx, ferry) + } } - return + return nil + } + ctx.Next = next + if err := route.handlers[len(route.handlers)-1-index](ctx); err != nil { + handlerRouterError(err, ctx, ferry) } + } else { + handle404(ferry, ctx) } - handle404(ferry, ctx) } // routerPath /auth/:name diff --git a/server.go b/server.go index 8d2a710..53ee7b7 100644 --- a/server.go +++ b/server.go @@ -51,12 +51,12 @@ func (ferry *Ferry) Listen(host string) error { return server.ListenAndServe(host) } -func (ferry *Ferry) addRoute(method, path string, h handler) { +func (ferry *Ferry) addRoute(method, path string, h []handler) { pathWithRegex := findAndReplace(path) ferry.routerMap[method] = append(ferry.routerMap[method], router{ routerPath: path, regexPath: pathWithRegex, - handler: h, + handlers: h, }) } @@ -66,22 +66,22 @@ func (ferry *Ferry) Use(h handler) { } // Get method of ferry -func (ferry *Ferry) Get(path string, h handler) { +func (ferry *Ferry) Get(path string, h ...handler) { ferry.addRoute(GET, path, h) } // Post method of ferry -func (ferry *Ferry) Post(path string, h handler) { +func (ferry *Ferry) Post(path string, h ...handler) { ferry.addRoute(POST, path, h) } // Put method of ferry -func (ferry *Ferry) Put(path string, h handler) { +func (ferry *Ferry) Put(path string, h ...handler) { ferry.addRoute(PUT, path, h) } // Delete method of ferry -func (ferry *Ferry) Delete(path string, h handler) { +func (ferry *Ferry) Delete(path string, h ...handler) { ferry.addRoute(DELETE, path, h) }