mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	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
This commit is contained in:
		@@ -634,8 +634,8 @@ func mustEnableAttachments(ctx *context.APIContext) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// bind binding an obj to a func(ctx *context.APIContext)
 | 
			
		||||
func bind[T any](obj T) http.HandlerFunc {
 | 
			
		||||
	return web.Wrap(func(ctx *context.APIContext) {
 | 
			
		||||
func bind[T any](_ T) any {
 | 
			
		||||
	return func(ctx *context.APIContext) {
 | 
			
		||||
		theObj := new(T) // create a new form obj for every request but not use obj directly
 | 
			
		||||
		errs := binding.Bind(ctx.Req, theObj)
 | 
			
		||||
		if len(errs) > 0 {
 | 
			
		||||
@@ -643,7 +643,7 @@ func bind[T any](obj T) http.HandlerFunc {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		web.SetForm(ctx, theObj)
 | 
			
		||||
	})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// The OAuth2 plugin is expected to be executed first, as it must ignore the user id stored
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user