Skip to content

Commit

Permalink
Merge pull request #23 from ferry-go/feature/core/exposed-group-struct
Browse files Browse the repository at this point in the history
exposing group struct
  • Loading branch information
ferry-go authored Jul 18, 2020
2 parents 14277cd + af32e8d commit c26490c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func appLevelMiddleware(ctx *Ctx, ferry *Ferry) {

func groupLevelMiddleware(ctx *Ctx, ferry *Ferry, routers []router) {
path := string(ctx.RequestCtx.Path())
// check if path is available in group middleware
// check if path is available in Group middleware
if len(ferry.groupMiddlewareMap) == 0 {
handleRouter(ctx, ferry, routers)
return
Expand Down
18 changes: 9 additions & 9 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ type router struct {
handlers []handler
}

type group struct {
type Group struct {
path string
ferry *Ferry
middleware []handler
middlewareCurrentIndex int
}

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{
Expand All @@ -36,32 +36,32 @@ func (g *group) addRoute(method, path string, h ...handler) {
}

// Get method of ferry
func (g *group) Get(path string, h handler) {
func (g *Group) Get(path string, h handler) {
g.addRoute(GET, path, h)
}

// Post method of ferry
func (g *group) Post(path string, h handler) {
func (g *Group) Post(path string, h handler) {
g.addRoute(POST, path, h)
}

// Put method of ferry
func (g *group) Put(path string, h handler) {
func (g *Group) Put(path string, h handler) {
g.addRoute(PUT, path, h)
}

// Delete method of ferry
func (g *group) Delete(path string, h handler) {
func (g *Group) Delete(path string, h handler) {
g.addRoute(DELETE, path, h)
}

func (g *group) Use(h handler) {
func (g *Group) Use(h handler) {
g.ferry.groupMiddlewareMap[g.path] = append(g.ferry.groupMiddlewareMap[g.path], h)
}

// Group method
func (g *group) Group(path string) *group {
return &group{
func (g *Group) Group(path string) *Group {
return &Group{
path: fmt.Sprintf("%s%s", g.path, path),
ferry: g.ferry,
middleware: []handler{},
Expand Down
14 changes: 10 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ func (ferry *Ferry) Listen(host string) error {
NoDefaultServerHeader: true,
Handler: handler,
ErrorHandler: func(r *fasthttp.RequestCtx, err error) {
ctx := getRouterContext(r, ferry)
_ = ferry.errorHandler(ctx, err)
if ferry.errorHandler != nil {
ctx := getRouterContext(r, ferry)
_ = ferry.errorHandler(ctx, err)
} else {
// TODO replace it with logger
fmt.Println(err.Error())
}

},
}
return server.ListenAndServe(host)
Expand Down Expand Up @@ -86,8 +92,8 @@ func (ferry *Ferry) Delete(path string, h ...handler) {
}

// Group method
func (ferry *Ferry) Group(path string) *group {
return &group{
func (ferry *Ferry) Group(path string) *Group {
return &Group{
path: path,
ferry: ferry,
}
Expand Down

0 comments on commit c26490c

Please sign in to comment.