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.

chi.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Package chi is a small, idiomatic and composable router for building HTTP services.
  3. //
  4. // chi requires Go 1.10 or newer.
  5. //
  6. // Example:
  7. // package main
  8. //
  9. // import (
  10. // "net/http"
  11. //
  12. // "github.com/go-chi/chi"
  13. // "github.com/go-chi/chi/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 regular expressions
  41. // including { or } are not supported, and / will never be
  42. // matched. An anonymous regexp pattern is allowed, using an empty string
  43. // before the colon in the placeholder, such as {:\\d+}
  44. //
  45. // The special placeholder of asterisk matches the rest of the requested
  46. // URL. Any trailing characters in the pattern are ignored. This is the only
  47. // placeholder which will match / characters.
  48. //
  49. // Examples:
  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/*/index" also matches "/page/intro/latest"
  54. // "/date/{yyyy:\\d\\d\\d\\d}/{mm:\\d\\d}/{dd:\\d\\d}" matches "/date/2017/04/01"
  55. //
  56. package chi
  57. import "net/http"
  58. // NewRouter returns a new Mux object that implements the Router interface.
  59. func NewRouter() *Mux {
  60. return NewMux()
  61. }
  62. // Router consisting of the core routing methods used by chi's Mux,
  63. // using only the standard net/http.
  64. type Router interface {
  65. http.Handler
  66. Routes
  67. // Use appends one or more middlewares onto the Router stack.
  68. Use(middlewares ...func(http.Handler) http.Handler)
  69. // With adds inline middlewares for an endpoint handler.
  70. With(middlewares ...func(http.Handler) http.Handler) Router
  71. // Group adds a new inline-Router along the current routing
  72. // path, with a fresh middleware stack for the inline-Router.
  73. Group(fn func(r Router)) Router
  74. // Route mounts a sub-Router along a `pattern`` string.
  75. Route(pattern string, fn func(r Router)) Router
  76. // Mount attaches another http.Handler along ./pattern/*
  77. Mount(pattern string, h http.Handler)
  78. // Handle and HandleFunc adds routes for `pattern` that matches
  79. // all HTTP methods.
  80. Handle(pattern string, h http.Handler)
  81. HandleFunc(pattern string, h http.HandlerFunc)
  82. // Method and MethodFunc adds routes for `pattern` that matches
  83. // the `method` HTTP method.
  84. Method(method, pattern string, h http.Handler)
  85. MethodFunc(method, pattern string, h http.HandlerFunc)
  86. // HTTP-method routing along `pattern`
  87. Connect(pattern string, h http.HandlerFunc)
  88. Delete(pattern string, h http.HandlerFunc)
  89. Get(pattern string, h http.HandlerFunc)
  90. Head(pattern string, h http.HandlerFunc)
  91. Options(pattern string, h http.HandlerFunc)
  92. Patch(pattern string, h http.HandlerFunc)
  93. Post(pattern string, h http.HandlerFunc)
  94. Put(pattern string, h http.HandlerFunc)
  95. Trace(pattern string, h http.HandlerFunc)
  96. // NotFound defines a handler to respond whenever a route could
  97. // not be found.
  98. NotFound(h http.HandlerFunc)
  99. // MethodNotAllowed defines a handler to respond whenever a method is
  100. // not allowed.
  101. MethodNotAllowed(h http.HandlerFunc)
  102. }
  103. // Routes interface adds two methods for router traversal, which is also
  104. // used by the `docgen` subpackage to generation documentation for Routers.
  105. type Routes interface {
  106. // Routes returns the routing tree in an easily traversable structure.
  107. Routes() []Route
  108. // Middlewares returns the list of middlewares in use by the router.
  109. Middlewares() Middlewares
  110. // Match searches the routing tree for a handler that matches
  111. // the method/path - similar to routing a http request, but without
  112. // executing the handler thereafter.
  113. Match(rctx *Context, method, path string) bool
  114. }
  115. // Middlewares type is a slice of standard middleware handlers with methods
  116. // to compose middleware chains and http.Handler's.
  117. type Middlewares []func(http.Handler) http.Handler