chi.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Package chi is a small, idiomatic and composable router for building HTTP services.
  2. //
  3. // chi supports the four most recent major versions of Go.
  4. //
  5. // Example:
  6. //
  7. // package main
  8. //
  9. // import (
  10. // "net/http"
  11. //
  12. // "github.com/go-chi/chi/v5"
  13. // "github.com/go-chi/chi/v5/middleware"
  14. // )
  15. //
  16. // func main() {
  17. // r := chi.NewRouter()
  18. // r.Use(middleware.Logger)
  19. // r.Use(middleware.Recoverer)
  20. //
  21. // r.Get("/", func(w http.ResponseWriter, r *http.Request) {
  22. // w.Write([]byte("root."))
  23. // })
  24. //
  25. // http.ListenAndServe(":3333", r)
  26. // }
  27. //
  28. // See github.com/go-chi/chi/_examples/ for more in-depth examples.
  29. //
  30. // URL patterns allow for easy matching of path components in HTTP
  31. // requests. The matching components can then be accessed using
  32. // chi.URLParam(). All patterns must begin with a slash.
  33. //
  34. // A simple named placeholder {name} matches any sequence of characters
  35. // up to the next / or the end of the URL. Trailing slashes on paths must
  36. // be handled explicitly.
  37. //
  38. // A placeholder with a name followed by a colon allows a regular
  39. // expression match, for example {number:\\d+}. The regular expression
  40. // syntax is Go's normal regexp RE2 syntax, except that / will never be
  41. // matched. An anonymous regexp pattern is allowed, using an empty string
  42. // before the colon in the placeholder, such as {:\\d+}
  43. //
  44. // The special placeholder of asterisk matches the rest of the requested
  45. // URL. Any trailing characters in the pattern are ignored. This is the only
  46. // placeholder which will match / characters.
  47. //
  48. // Examples:
  49. //
  50. // "/user/{name}" matches "/user/jsmith" but not "/user/jsmith/info" or "/user/jsmith/"
  51. // "/user/{name}/info" matches "/user/jsmith/info"
  52. // "/page/*" matches "/page/intro/latest"
  53. // "/page/{other}/latest" also matches "/page/intro/latest"
  54. // "/date/{yyyy:\\d\\d\\d\\d}/{mm:\\d\\d}/{dd:\\d\\d}" matches "/date/2017/04/01"
  55. package chi
  56. import "net/http"
  57. // NewRouter returns a new Mux object that implements the Router interface.
  58. func NewRouter() *Mux {
  59. return NewMux()
  60. }
  61. // Router consisting of the core routing methods used by chi's Mux,
  62. // using only the standard net/http.
  63. type Router interface {
  64. http.Handler
  65. Routes
  66. // Use appends one or more middlewares onto the Router stack.
  67. Use(middlewares ...func(http.Handler) http.Handler)
  68. // With adds inline middlewares for an endpoint handler.
  69. With(middlewares ...func(http.Handler) http.Handler) Router
  70. // Group adds a new inline-Router along the current routing
  71. // path, with a fresh middleware stack for the inline-Router.
  72. Group(fn func(r Router)) Router
  73. // Route mounts a sub-Router along a `pattern`` string.
  74. Route(pattern string, fn func(r Router)) Router
  75. // Mount attaches another http.Handler along ./pattern/*
  76. Mount(pattern string, h http.Handler)
  77. // Handle and HandleFunc adds routes for `pattern` that matches
  78. // all HTTP methods.
  79. Handle(pattern string, h http.Handler)
  80. HandleFunc(pattern string, h http.HandlerFunc)
  81. // Method and MethodFunc adds routes for `pattern` that matches
  82. // the `method` HTTP method.
  83. Method(method, pattern string, h http.Handler)
  84. MethodFunc(method, pattern string, h http.HandlerFunc)
  85. // HTTP-method routing along `pattern`
  86. Connect(pattern string, h http.HandlerFunc)
  87. Delete(pattern string, h http.HandlerFunc)
  88. Get(pattern string, h http.HandlerFunc)
  89. Head(pattern string, h http.HandlerFunc)
  90. Options(pattern string, h http.HandlerFunc)
  91. Patch(pattern string, h http.HandlerFunc)
  92. Post(pattern string, h http.HandlerFunc)
  93. Put(pattern string, h http.HandlerFunc)
  94. Trace(pattern string, h http.HandlerFunc)
  95. // NotFound defines a handler to respond whenever a route could
  96. // not be found.
  97. NotFound(h http.HandlerFunc)
  98. // MethodNotAllowed defines a handler to respond whenever a method is
  99. // not allowed.
  100. MethodNotAllowed(h http.HandlerFunc)
  101. }
  102. // Routes interface adds two methods for router traversal, which is also
  103. // used by the `docgen` subpackage to generation documentation for Routers.
  104. type Routes interface {
  105. // Routes returns the routing tree in an easily traversable structure.
  106. Routes() []Route
  107. // Middlewares returns the list of middlewares in use by the router.
  108. Middlewares() Middlewares
  109. // Match searches the routing tree for a handler that matches
  110. // the method/path - similar to routing a http request, but without
  111. // executing the handler thereafter.
  112. Match(rctx *Context, method, path string) bool
  113. // Find searches the routing tree for the pattern that matches
  114. // the method/path.
  115. Find(rctx *Context, method, path string) string
  116. }
  117. // Middlewares type is a slice of standard middleware handlers with methods
  118. // to compose middleware chains and http.Handler's.
  119. type Middlewares []func(http.Handler) http.Handler