2021-01-26 21:06:53 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 21:06:53 +05:30
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-01-30 14:25:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2021-01-26 21:06:53 +05:30
|
|
|
|
|
|
|
"gitea.com/go-chi/binding"
|
2023-06-18 13:29:09 +05:30
|
|
|
"github.com/go-chi/chi/v5"
|
2021-01-26 21:06:53 +05:30
|
|
|
)
|
|
|
|
|
2023-06-18 13:29:09 +05:30
|
|
|
// Bind binding an obj to a handler's context data
|
|
|
|
func Bind[T any](_ T) http.HandlerFunc {
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request) {
|
2022-12-12 13:39:26 +05:30
|
|
|
theObj := new(T) // create a new form obj for every request but not use obj directly
|
2023-06-18 13:29:09 +05:30
|
|
|
data := middleware.GetContextData(req.Context())
|
|
|
|
binding.Bind(req, theObj)
|
|
|
|
SetForm(data, theObj)
|
|
|
|
middleware.AssignForm(theObj, data)
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
}
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// SetForm set the form object
|
2023-06-18 13:29:09 +05:30
|
|
|
func SetForm(dataStore middleware.ContextDataStore, obj interface{}) {
|
|
|
|
dataStore.GetData()["__form"] = obj
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetForm returns the validate form information
|
2023-06-18 13:29:09 +05:30
|
|
|
func GetForm(dataStore middleware.ContextDataStore) interface{} {
|
|
|
|
return dataStore.GetData()["__form"]
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Route defines a route based on chi's router
|
|
|
|
type Route struct {
|
|
|
|
R chi.Router
|
|
|
|
curGroupPrefix string
|
|
|
|
curMiddlewares []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRoute creates a new route
|
|
|
|
func NewRoute() *Route {
|
|
|
|
r := chi.NewRouter()
|
2023-04-27 11:36:45 +05:30
|
|
|
return &Route{R: r}
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Use supports two middlewares
|
|
|
|
func (r *Route) Use(middlewares ...interface{}) {
|
2023-04-27 11:36:45 +05:30
|
|
|
for _, m := range middlewares {
|
|
|
|
r.R.Use(toHandlerProvider(m))
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group mounts a sub-Router along a `pattern` string.
|
|
|
|
func (r *Route) Group(pattern string, fn func(), middlewares ...interface{}) {
|
2022-01-20 23:16:10 +05:30
|
|
|
previousGroupPrefix := r.curGroupPrefix
|
|
|
|
previousMiddlewares := r.curMiddlewares
|
2021-01-26 21:06:53 +05:30
|
|
|
r.curGroupPrefix += pattern
|
|
|
|
r.curMiddlewares = append(r.curMiddlewares, middlewares...)
|
|
|
|
|
|
|
|
fn()
|
|
|
|
|
|
|
|
r.curGroupPrefix = previousGroupPrefix
|
|
|
|
r.curMiddlewares = previousMiddlewares
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) getPattern(pattern string) string {
|
|
|
|
newPattern := r.curGroupPrefix + pattern
|
|
|
|
if !strings.HasPrefix(newPattern, "/") {
|
|
|
|
newPattern = "/" + newPattern
|
|
|
|
}
|
|
|
|
if newPattern == "/" {
|
|
|
|
return newPattern
|
|
|
|
}
|
|
|
|
return strings.TrimSuffix(newPattern, "/")
|
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
func (r *Route) wrapMiddlewareAndHandler(h []any) ([]func(http.Handler) http.Handler, http.HandlerFunc) {
|
|
|
|
handlerProviders := make([]func(http.Handler) http.Handler, 0, len(r.curMiddlewares)+len(h))
|
|
|
|
for _, m := range r.curMiddlewares {
|
|
|
|
handlerProviders = append(handlerProviders, toHandlerProvider(m))
|
|
|
|
}
|
|
|
|
for _, m := range h {
|
|
|
|
handlerProviders = append(handlerProviders, toHandlerProvider(m))
|
|
|
|
}
|
|
|
|
middlewares := handlerProviders[:len(handlerProviders)-1]
|
|
|
|
handlerFunc := handlerProviders[len(handlerProviders)-1](nil).ServeHTTP
|
|
|
|
return middlewares, handlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) Methods(method, pattern string, h []any) {
|
|
|
|
middlewares, handlerFunc := r.wrapMiddlewareAndHandler(h)
|
|
|
|
fullPattern := r.getPattern(pattern)
|
|
|
|
if strings.Contains(method, ",") {
|
|
|
|
methods := strings.Split(method, ",")
|
|
|
|
for _, method := range methods {
|
|
|
|
r.R.With(middlewares...).Method(strings.TrimSpace(method), fullPattern, handlerFunc)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r.R.With(middlewares...).Method(method, fullPattern, handlerFunc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
// Mount attaches another Route along ./pattern/*
|
|
|
|
func (r *Route) Mount(pattern string, subR *Route) {
|
2023-04-27 11:36:45 +05:30
|
|
|
subR.Use(r.curMiddlewares...)
|
2021-01-26 21:06:53 +05:30
|
|
|
r.R.Mount(r.getPattern(pattern), subR.R)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any delegate requests for all methods
|
|
|
|
func (r *Route) Any(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
middlewares, handlerFunc := r.wrapMiddlewareAndHandler(h)
|
|
|
|
r.R.With(middlewares...).HandleFunc(r.getPattern(pattern), handlerFunc)
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
// RouteMethods delegate special methods, it is an alias of "Methods", while the "pattern" is the first parameter
|
|
|
|
func (r *Route) RouteMethods(pattern, methods string, h ...interface{}) {
|
|
|
|
r.Methods(methods, pattern, h)
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Delete delegate delete method
|
|
|
|
func (r *Route) Delete(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
r.Methods("DELETE", pattern, h)
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Get delegate get method
|
|
|
|
func (r *Route) Get(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
r.Methods("GET", pattern, h)
|
2021-07-21 09:02:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetOptions delegate get and options method
|
|
|
|
func (r *Route) GetOptions(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
r.Methods("GET,OPTIONS", pattern, h)
|
2021-07-21 09:02:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// PostOptions delegate post and options method
|
|
|
|
func (r *Route) PostOptions(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
r.Methods("POST,OPTIONS", pattern, h)
|
2021-07-21 09:02:35 +05:30
|
|
|
}
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
// Head delegate head method
|
|
|
|
func (r *Route) Head(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
r.Methods("HEAD", pattern, h)
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Post delegate post method
|
|
|
|
func (r *Route) Post(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
r.Methods("POST", pattern, h)
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Put delegate put method
|
|
|
|
func (r *Route) Put(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
r.Methods("PUT", pattern, h)
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Patch delegate patch method
|
|
|
|
func (r *Route) Patch(pattern string, h ...interface{}) {
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
r.Methods("PATCH", pattern, h)
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements http.Handler
|
|
|
|
func (r *Route) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
r.R.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
// NotFound defines a handler to respond whenever a route could not be found.
|
2021-01-26 21:06:53 +05:30
|
|
|
func (r *Route) NotFound(h http.HandlerFunc) {
|
|
|
|
r.R.NotFound(h)
|
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
// Combo delegates requests to Combo
|
2021-01-26 21:06:53 +05:30
|
|
|
func (r *Route) Combo(pattern string, h ...interface{}) *Combo {
|
|
|
|
return &Combo{r, pattern, h}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combo represents a tiny group routes with same pattern
|
|
|
|
type Combo struct {
|
|
|
|
r *Route
|
|
|
|
pattern string
|
|
|
|
h []interface{}
|
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
// Get delegates Get method
|
2021-01-26 21:06:53 +05:30
|
|
|
func (c *Combo) Get(h ...interface{}) *Combo {
|
|
|
|
c.r.Get(c.pattern, append(c.h, h...)...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
// Post delegates Post method
|
2021-01-26 21:06:53 +05:30
|
|
|
func (c *Combo) Post(h ...interface{}) *Combo {
|
|
|
|
c.r.Post(c.pattern, append(c.h, h...)...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
// Delete delegates Delete method
|
2021-01-26 21:06:53 +05:30
|
|
|
func (c *Combo) Delete(h ...interface{}) *Combo {
|
|
|
|
c.r.Delete(c.pattern, append(c.h, h...)...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
// Put delegates Put method
|
2021-01-26 21:06:53 +05:30
|
|
|
func (c *Combo) Put(h ...interface{}) *Combo {
|
|
|
|
c.r.Put(c.pattern, append(c.h, h...)...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 00:19:06 +05:30
|
|
|
// Patch delegates Patch method
|
2021-01-26 21:06:53 +05:30
|
|
|
func (c *Combo) Patch(h ...interface{}) *Combo {
|
|
|
|
c.r.Patch(c.pattern, append(c.h, h...)...)
|
|
|
|
return c
|
|
|
|
}
|