-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: refactor http middleware #996
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
session/middleware/start_session_test.go:26
- Ensure that converting 'next' using http.ConvertHandler is necessary in the test context; verify that this conversion produces the expected Goravel http.Handler instance to maintain middleware chain integrity.
StartSession()(http.ConvertHandler(next)).ServeHTTP(NewTestContext(r.Context(), next, w, r))
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #996 +/- ##
==========================================
+ Coverage 70.18% 70.38% +0.19%
==========================================
Files 168 171 +3
Lines 11428 11488 +60
==========================================
+ Hits 8021 8086 +65
+ Misses 3056 3051 -5
Partials 351 351 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
接goravel/goravel#655 (comment) 因为之前忘了哪个版本开始,应该是v1.12或者v1.13,控制台中返回响应都需要 |
可否在这个PR的具体代码上添加评论?我们可以针对具体代码进行讨论。 |
// Abort aborts the request with the specified HTTP status code, default is 400. | ||
Abort(code ...int) | ||
// AbortWithStatus aborts the request with the specified HTTP status code. | ||
// DEPRECATED: Use Abort instead. | ||
AbortWithStatus(code int) | ||
// AbortWithStatusJson aborts the request with the specified HTTP status code | ||
// and returns a JSON response object. | ||
// DEPRECATED: Use Response().Json().Abort() instead. | ||
AbortWithStatusJson(code int, jsonObj any) | ||
// Next skips the current request handler, allowing the next middleware or handler to be executed. | ||
Next() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Abort
相关的方法和Next
都去掉了,现在在中间件中想中断请求直接返回Response
不返回next.ServeHTTP
就行了。
另外ServeHTTP
这个名字是 go 标准库的,感觉不怎么好听,有必要换一个吗?
fiber 驱动的适配在 goravel/fiber#166 完成,通过全部测试,gin 我近期应该没空处理了,等其他人来吧。 |
Thanks, checking, and FYI: PR can be merged when all drivers are ready to avoid break merging. |
Awesome PR 👍 I think the essence of this PR is to implement the middleware logic by ourselves. There are a lot of break changes, I need more time to check it and will provide feedback later. |
One question: If we just want to remove the |
使用 next 控制中间件更接近 Laravel,见 https://laravel.com/docs/12.x/middleware#main-content |
目前的实现的确更优雅一些。抛开 Render 方法的修改,目前其他的逻辑好像对于在中间件中修改响应没有促进作用?同样需要去操作 gin 或 fiber 的 ctx。 |
包括中断请求,修改过的实现也更加优雅 |
单从这个功能来看,修改前后是否有优化呢?目前我的主要想法是:
|
这个PR更多算是对逻辑的优化,不需要那么多的Abort,也更接近laravel的实现。 目前看想在gin或者fiber里面直接实现像laravel那种修改不太可能或者说很麻烦,laravel是把响应留到洋葱的最后才发送给客户端,但是gin,fiber这些都是一调用对应的方法就发送了,但是如果盲目全部加一个Writer缓存起来的话,又会丢失SSE,Stream之类的特殊功能,比较头大。 升级过程我认为不算麻烦,一个项目中间件应该不会超过20个,升级一个预计用时不到1分钟。 @almas1992 有木有其他建议? |
如果未来有计划脱离 Gin/Fiber,自行实现框架的路由系统,建议将这类逻辑的重构放到自研路由层中处理,可能会更自然,也更容易扩展。 |
短期内很难完美处理,暂时先关闭了 |
Thanks, 虽然暂时关闭,但提供了很好的思路,未来优化时候再打开。 👍 |
📑 Description
Closes goravel/goravel#655
✅ Checks