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.

route.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package web
  4. import (
  5. "net/http"
  6. "strings"
  7. "code.gitea.io/gitea/modules/web/middleware"
  8. "gitea.com/go-chi/binding"
  9. "github.com/go-chi/chi/v5"
  10. )
  11. // Bind binding an obj to a handler's context data
  12. func Bind[T any](_ T) http.HandlerFunc {
  13. return func(resp http.ResponseWriter, req *http.Request) {
  14. theObj := new(T) // create a new form obj for every request but not use obj directly
  15. data := middleware.GetContextData(req.Context())
  16. binding.Bind(req, theObj)
  17. SetForm(data, theObj)
  18. middleware.AssignForm(theObj, data)
  19. }
  20. }
  21. // SetForm set the form object
  22. func SetForm(dataStore middleware.ContextDataStore, obj any) {
  23. dataStore.GetData()["__form"] = obj
  24. }
  25. // GetForm returns the validate form information
  26. func GetForm(dataStore middleware.ContextDataStore) any {
  27. return dataStore.GetData()["__form"]
  28. }
  29. // Route defines a route based on chi's router
  30. type Route struct {
  31. R chi.Router
  32. curGroupPrefix string
  33. curMiddlewares []any
  34. }
  35. // NewRoute creates a new route
  36. func NewRoute() *Route {
  37. r := chi.NewRouter()
  38. return &Route{R: r}
  39. }
  40. // Use supports two middlewares
  41. func (r *Route) Use(middlewares ...any) {
  42. for _, m := range middlewares {
  43. r.R.Use(toHandlerProvider(m))
  44. }
  45. }
  46. // Group mounts a sub-Router along a `pattern` string.
  47. func (r *Route) Group(pattern string, fn func(), middlewares ...any) {
  48. previousGroupPrefix := r.curGroupPrefix
  49. previousMiddlewares := r.curMiddlewares
  50. r.curGroupPrefix += pattern
  51. r.curMiddlewares = append(r.curMiddlewares, middlewares...)
  52. fn()
  53. r.curGroupPrefix = previousGroupPrefix
  54. r.curMiddlewares = previousMiddlewares
  55. }
  56. func (r *Route) getPattern(pattern string) string {
  57. newPattern := r.curGroupPrefix + pattern
  58. if !strings.HasPrefix(newPattern, "/") {
  59. newPattern = "/" + newPattern
  60. }
  61. if newPattern == "/" {
  62. return newPattern
  63. }
  64. return strings.TrimSuffix(newPattern, "/")
  65. }
  66. func (r *Route) wrapMiddlewareAndHandler(h []any) ([]func(http.Handler) http.Handler, http.HandlerFunc) {
  67. handlerProviders := make([]func(http.Handler) http.Handler, 0, len(r.curMiddlewares)+len(h))
  68. for _, m := range r.curMiddlewares {
  69. handlerProviders = append(handlerProviders, toHandlerProvider(m))
  70. }
  71. for _, m := range h {
  72. handlerProviders = append(handlerProviders, toHandlerProvider(m))
  73. }
  74. middlewares := handlerProviders[:len(handlerProviders)-1]
  75. handlerFunc := handlerProviders[len(handlerProviders)-1](nil).ServeHTTP
  76. return middlewares, handlerFunc
  77. }
  78. func (r *Route) Methods(method, pattern string, h []any) {
  79. middlewares, handlerFunc := r.wrapMiddlewareAndHandler(h)
  80. fullPattern := r.getPattern(pattern)
  81. if strings.Contains(method, ",") {
  82. methods := strings.Split(method, ",")
  83. for _, method := range methods {
  84. r.R.With(middlewares...).Method(strings.TrimSpace(method), fullPattern, handlerFunc)
  85. }
  86. } else {
  87. r.R.With(middlewares...).Method(method, fullPattern, handlerFunc)
  88. }
  89. }
  90. // Mount attaches another Route along ./pattern/*
  91. func (r *Route) Mount(pattern string, subR *Route) {
  92. subR.Use(r.curMiddlewares...)
  93. r.R.Mount(r.getPattern(pattern), subR.R)
  94. }
  95. // Any delegate requests for all methods
  96. func (r *Route) Any(pattern string, h ...any) {
  97. middlewares, handlerFunc := r.wrapMiddlewareAndHandler(h)
  98. r.R.With(middlewares...).HandleFunc(r.getPattern(pattern), handlerFunc)
  99. }
  100. // RouteMethods delegate special methods, it is an alias of "Methods", while the "pattern" is the first parameter
  101. func (r *Route) RouteMethods(pattern, methods string, h ...any) {
  102. r.Methods(methods, pattern, h)
  103. }
  104. // Delete delegate delete method
  105. func (r *Route) Delete(pattern string, h ...any) {
  106. r.Methods("DELETE", pattern, h)
  107. }
  108. // Get delegate get method
  109. func (r *Route) Get(pattern string, h ...any) {
  110. r.Methods("GET", pattern, h)
  111. }
  112. // GetOptions delegate get and options method
  113. func (r *Route) GetOptions(pattern string, h ...any) {
  114. r.Methods("GET,OPTIONS", pattern, h)
  115. }
  116. // PostOptions delegate post and options method
  117. func (r *Route) PostOptions(pattern string, h ...any) {
  118. r.Methods("POST,OPTIONS", pattern, h)
  119. }
  120. // Head delegate head method
  121. func (r *Route) Head(pattern string, h ...any) {
  122. r.Methods("HEAD", pattern, h)
  123. }
  124. // Post delegate post method
  125. func (r *Route) Post(pattern string, h ...any) {
  126. r.Methods("POST", pattern, h)
  127. }
  128. // Put delegate put method
  129. func (r *Route) Put(pattern string, h ...any) {
  130. r.Methods("PUT", pattern, h)
  131. }
  132. // Patch delegate patch method
  133. func (r *Route) Patch(pattern string, h ...any) {
  134. r.Methods("PATCH", pattern, h)
  135. }
  136. // ServeHTTP implements http.Handler
  137. func (r *Route) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  138. r.R.ServeHTTP(w, req)
  139. }
  140. // NotFound defines a handler to respond whenever a route could not be found.
  141. func (r *Route) NotFound(h http.HandlerFunc) {
  142. r.R.NotFound(h)
  143. }
  144. // Combo delegates requests to Combo
  145. func (r *Route) Combo(pattern string, h ...any) *Combo {
  146. return &Combo{r, pattern, h}
  147. }
  148. // Combo represents a tiny group routes with same pattern
  149. type Combo struct {
  150. r *Route
  151. pattern string
  152. h []any
  153. }
  154. // Get delegates Get method
  155. func (c *Combo) Get(h ...any) *Combo {
  156. c.r.Get(c.pattern, append(c.h, h...)...)
  157. return c
  158. }
  159. // Post delegates Post method
  160. func (c *Combo) Post(h ...any) *Combo {
  161. c.r.Post(c.pattern, append(c.h, h...)...)
  162. return c
  163. }
  164. // Delete delegates Delete method
  165. func (c *Combo) Delete(h ...any) *Combo {
  166. c.r.Delete(c.pattern, append(c.h, h...)...)
  167. return c
  168. }
  169. // Put delegates Put method
  170. func (c *Combo) Put(h ...any) *Combo {
  171. c.r.Put(c.pattern, append(c.h, h...)...)
  172. return c
  173. }
  174. // Patch delegates Patch method
  175. func (c *Combo) Patch(h ...any) *Combo {
  176. c.r.Patch(c.pattern, append(c.h, h...)...)
  177. return c
  178. }