gin
gin copied to clipboard
make middlewares work before auto redirection
What it solves
Currently if enable RedirectTrailingSlash
or RedirectFixedPath
, gin will skip any global middlewares and redirect directly. It leads to some problems. For example, recording the path that caused the redirection (logger), adding some external headers to response (handler), or even rewrite target path.
Feature
-
call global middlewares before auto redirection
-
add
AutoRedirect
API on engine to set handlers used before auto redirection. It's likeNoMethod
andNoRoute
, and will be called just before redirect.
may related: #1004 , #2458 close #2458
Codecov Report
Merging #2477 (51ef7a6) into master (d062a6a) will increase coverage by
0.13%
. The diff coverage is100.00%
.
:exclamation: Current head 51ef7a6 differs from pull request most recent head 5f94d6f. Consider uploading reports for the commit 5f94d6f to get more accurate results
@@ Coverage Diff @@
## master #2477 +/- ##
==========================================
+ Coverage 98.60% 98.73% +0.13%
==========================================
Files 41 41
Lines 3084 3088 +4
==========================================
+ Hits 3041 3049 +8
+ Misses 30 27 -3
+ Partials 13 12 -1
Flag | Coverage Δ | |
---|---|---|
go-1.13 | 98.73% <100.00%> (∅) |
|
go-1.14 | 98.57% <100.00%> (∅) |
|
go-1.15 | 98.57% <100.00%> (∅) |
|
go-1.16 | 98.57% <100.00%> (-0.04%) |
:arrow_down: |
go-1.17 | 98.47% <100.00%> (-0.04%) |
:arrow_down: |
macos-latest | 98.73% <100.00%> (+0.13%) |
:arrow_up: |
nomsgpack | 98.71% <100.00%> (+0.13%) |
:arrow_up: |
ubuntu-latest | 98.73% <100.00%> (+0.13%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
Impacted Files | Coverage Δ | |
---|---|---|
gin.go | 99.13% <100.00%> (-0.04%) |
:arrow_down: |
context.go | 97.81% <0.00%> (-0.09%) |
:arrow_down: |
tree.go | 100.00% <0.00%> (ø) |
|
debug.go | 100.00% <0.00%> (+8.33%) |
:arrow_up: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update d062a6a...5f94d6f. Read the comment docs.
@thinkerou PTAL, I think this feature will be very helpful, but I don't know if there is still something to consider when adding such feature ❤
👍 I'm hitting this problem too. I'm not able to attach headers to outgoing redirects since the middleware does handle those at the moment.
Right now gin-contrib/cors middleware fails to work when a redirect occurs (due to RedirectTrailingSlash
). But, it seems to me that being a same-domain redirect, gin (together with the CORS middleware) should support this use case. Is there any reason not to?
👀
@appleboy Hi, I have updated this PR to adapt to the latest master branch.
Sadly we have encountered the problem this PR solved again recently😥. When could it be merged?
any updates?
@unbyte Please fix the conflicts.
@appleboy fixed.
any updates