Skip to content

Commit

Permalink
Merge pull request #21 from ferry-go/feature/core/adding-handler-leve…
Browse files Browse the repository at this point in the history
…l-middleware

added handler level middleware
  • Loading branch information
pittalamadhuri authored Jul 15, 2020
2 parents b1026f6 + b0894d1 commit 497a549
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
11 changes: 11 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
39 changes: 28 additions & 11 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
})
}

Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

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

Expand Down

0 comments on commit 497a549

Please sign in to comment.