No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

phi.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // phi is a small, idiomatic and composable router for building HTTP services.
  3. //
  4. // phi requires Go 1.7 or newer.
  5. //
  6. // Example:
  7. // package main
  8. //
  9. // import (
  10. // "net/http"
  11. //
  12. // phi "maze.io/phi.v1"
  13. // "maze.io/phi.v1/middleware"
  14. // )
  15. //
  16. // func main() {
  17. // r := phi.NewRouter()
  18. // r.Use(middleware.Logger)
  19. // r.Use(middleware.Recoverer)
  20. //
  21. // r.Get("/", func(ctx *phi.Context) {
  22. // ctx.Write([]byte("root."))
  23. // })
  24. //
  25. // http.ListenAndServe(":3333", r)
  26. // }
  27. //
  28. // See maze.io/phi/_examples/ for more in-depth examples.
  29. //
  30. package phi
  31. import "net/http"
  32. // Handler serves phi requests.
  33. type Handler interface {
  34. Serve(Context)
  35. }
  36. // HandlerFunc can handle phi requests.
  37. type HandlerFunc func(Context)
  38. // Serve request.
  39. func (f HandlerFunc) Serve(ctx Context) {
  40. f(ctx)
  41. }
  42. // Middleware can handle phi middleware.
  43. type Middleware func(Handler) Handler
  44. // Middlewares type is a slice of standard middleware handlers with methods
  45. // to compose middleware chains and Handler's.
  46. type Middlewares []Middleware
  47. // NewRouter returns a new Mux object that implements the Router interface.
  48. func NewRouter() *Mux {
  49. return NewMux()
  50. }
  51. // Router consisting of the core routing methods used by phi's Mux,
  52. // using only the standard net/http.
  53. type Router interface {
  54. http.Handler
  55. Routes
  56. // Use appends one or more middlewares onto the Router stack.
  57. Use(middlewares ...Middleware)
  58. // With adds inline middlewares for an endpoint handler.
  59. With(middlewares ...Middleware) Router
  60. // Group adds a new inline-Router along the current routing
  61. // path, with a fresh middleware stack for the inline-Router.
  62. Group(fn func(r Router)) Router
  63. // Route mounts a sub-Router along a `pattern`` string.
  64. Route(pattern string, fn func(r Router)) Router
  65. // Mount attaches another http.Handler along ./pattern/*
  66. Mount(pattern string, h Handler)
  67. // Handle and HandleFunc adds routes for `pattern` that matches
  68. // all HTTP methods.
  69. Handle(pattern string, h Handler)
  70. HandleFunc(pattern string, h HandlerFunc)
  71. // HTTP-method routing along `pattern`
  72. Connect(pattern string, h HandlerFunc)
  73. Delete(pattern string, h HandlerFunc)
  74. Get(pattern string, h HandlerFunc)
  75. Head(pattern string, h HandlerFunc)
  76. Options(pattern string, h HandlerFunc)
  77. Patch(pattern string, h HandlerFunc)
  78. Post(pattern string, h HandlerFunc)
  79. Put(pattern string, h HandlerFunc)
  80. Trace(pattern string, h HandlerFunc)
  81. // NotFound defines a handler to respond whenever a route could
  82. // not be found.
  83. NotFound(h HandlerFunc)
  84. }
  85. // Routes interface adds two methods for router traversal, which is also
  86. // used by the `docgen` subpackage to generation documentation for Routers.
  87. type Routes interface {
  88. // Routes returns the routing tree in an easily traversable structure.
  89. Routes() []Route
  90. // Middlewares returns the list of middlewares in use by the router.
  91. Middlewares() Middlewares
  92. }