Skip to content

Repository files navigation

phi

GoDoc WidgetTravis WidgetLicense WidgetGoReport Widget

phi is a package which ports chi to fasthttp.

fork

fork from fate-lovely/phi that fate-lovely port from chi

my modify:

  • rename func and type name , more clean to read

Install

go get -u github.com/fate-lovely/phi

Example

r:=NewRouter()
reqIDMW:=func(nextHandlerFunc) HandlerFunc {
returnfunc(ctx*fasthttp.RequestCtx) {
next(ctx)
ctx.WriteString("+reqid=1")
}
}
r.Use(reqIDMW)
r.Get("/", func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("index")
})
r.NotFound(func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("whoops, not found")
ctx.SetStatusCode(404)
})
r.MethodNotAllowed(func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("whoops, bad method")
ctx.SetStatusCode(405)
})
// tasksr.Group(func(rRouter) {
mw:=func(nextHandlerFunc) HandlerFunc {
returnfunc(ctx*fasthttp.RequestCtx) {
next(ctx)
ctx.WriteString("+task")
}
}
r.Use(mw)
r.Get("/task", func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("task")
})
r.Post("/task", func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("new task")
})
caution:=func(nextHandlerFunc) HandlerFunc {
returnfunc(ctx*fasthttp.RequestCtx) {
next(ctx)
ctx.WriteString("+caution")
}
}
r.With(caution).Delete("/task", func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("delete task")
})
})
// catr.Route("/cat", func(rRouter) {
r.NotFound(func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("no such cat")
ctx.SetStatusCode(404)
})
r.Use(func(nextHandlerFunc) HandlerFunc {
returnfunc(ctx*fasthttp.RequestCtx) {
next(ctx)
ctx.WriteString("+cat")
}
})
r.Get("/", func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("cat")
})
r.Patch("/", func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("patch cat")
})
})
// useruserRouter:=NewRouter()
userRouter.NotFound(func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("no such user")
ctx.SetStatusCode(404)
})
userRouter.Use(func(nextHandlerFunc) HandlerFunc {
returnfunc(ctx*fasthttp.RequestCtx) {
next(ctx)
ctx.WriteString("+user")
}
})
userRouter.Get("/", func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("user")
})
userRouter.Post("/", func(ctx*fasthttp.RequestCtx) {
ctx.WriteString("new user")
})
r.Mount("/user", userRouter)
returnr

output:

PathStatus CodeBody
GET /200index+reqid=1
POST /405whoops, bad method+reqid=1
GET /nothing404whoops, not found+reqid=1
GET /task200task+task+reqid=1
POST /task200new task+task+reqid=1
DELETE /task200delete task+caution+task+reqid=1
GET /cat200cat+cat+reqid=1
PATCH /cat200patch cat+cat+reqid=1
GET /cat/nothing404no such cat+cat+reqid=1
GET /user200user+user+reqid=1
POST /user200new user+user+reqid=1
GET /user/nothing404no such user+user+reqid=1

License

Licensed under MIT License

About

chi for fasthttp

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages