Categorygithub.com/tsingson/phi
modulepackage
0.3.1
Repository: https://github.com/tsingson/phi.git
Documentation: pkg.go.dev

# README

phi

GoDoc Widget Travis Widget License Widget GoReport 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(next HandlerFunc) HandlerFunc {
  return func(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)
})

// tasks
r.Group(func(r Router) {
  mw := func(next HandlerFunc) HandlerFunc {
    return func(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(next HandlerFunc) HandlerFunc {
    return func(ctx *fasthttp.RequestCtx) {
      next(ctx)
      ctx.WriteString("+caution")
    }
  }
  r.With(caution).Delete("/task", func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("delete task")
  })
})

// cat
r.Route("/cat", func(r Router) {
  r.NotFound(func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("no such cat")
    ctx.SetStatusCode(404)
  })
  r.Use(func(next HandlerFunc) HandlerFunc {
    return func(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")
  })
})

// user
userRouter := NewRouter()
userRouter.NotFound(func(ctx *fasthttp.RequestCtx) {
  ctx.WriteString("no such user")
  ctx.SetStatusCode(404)
})
userRouter.Use(func(next HandlerFunc) HandlerFunc {
  return func(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)

return r

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

# Functions

Chain returns a Middlewares type from a slice of middleware handlers.
NewMux returns a newly initialized Mux object that implements the Router interface.
NewMux returns a newly initialized Mux object that implements the Router interface.
NewRouteContext returns a new routing Context object.
NewRouter returns a new Mux object that implements the Router interface.
RegisterMethod registers new methods which can be used in `Router.Method` call.
RouteContext returns phi's routing Context object from *fasthttp.RequestCtx.
URLParam returns the url parameter from *fasthttp.RequestCtx.
Walk walks any router tree that implements Routes interface.

# Variables

RouteCtxKey is the context.Context key to store the request context.

# Structs

ChainHandler is a phi.HandlerFunc with support for handler composition and execution.
Context is the default routing context set on the root node of a request context to track route patterns, URL parameters and an optional routing path.
Mux is a simple HTTP route multiplexer that parses a request path, records any URL params, and executes an end handler.
Route describes the details of a routing handler.
RouteParams is a structure to track URL routing parameters efficiently.

# Interfaces

HandlerFunc represents a fasthttp request handler, it has one method: Handler, which is equal to fasthttp.RequestHandler.
Router consisting of the core routing methods used by phi's Mux,.
Routes interface adds two methods for router traversal, which is also used by the `docgen` subpackage to generation documentation for Routers.

# Type aliases

Middleware represents phi middlewares, which accept a RequestHandlerFunc and return a RequestHandlerFunc.
Middlewares type is a slice of standard middleware handlers with methods to compose middleware chains and phi.HandlerFunc's.
RequestHandlerFunc type is an adapter to allow the use of ordinary functions as handlers.
WalkFunc is the type of the function called for each method and route visited by Walk.