gin icon indicating copy to clipboard operation
gin copied to clipboard

make middlewares work before auto redirection

Open unbyte opened this issue 3 years ago • 10 comments

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 like NoMethod and NoRoute, and will be called just before redirect.

may related: #1004 , #2458 close #2458

unbyte avatar Aug 22 '20 16:08 unbyte

Codecov Report

Merging #2477 (51ef7a6) into master (d062a6a) will increase coverage by 0.13%. The diff coverage is 100.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 Impacted file tree graph

@@            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.

codecov[bot] avatar Aug 22 '20 16:08 codecov[bot]

@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 ❤

unbyte avatar Aug 22 '20 16:08 unbyte

👍 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.

knusbaum avatar Sep 10 '20 01:09 knusbaum

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?

betabandido avatar Feb 18 '21 10:02 betabandido

👀

GiancarlosIO avatar Apr 15 '21 00:04 GiancarlosIO

@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?

unbyte avatar Nov 02 '21 07:11 unbyte

any updates?

Arekusei avatar Dec 21 '21 10:12 Arekusei

@unbyte Please fix the conflicts.

appleboy avatar Dec 27 '21 06:12 appleboy

@appleboy fixed.

unbyte avatar Dec 28 '21 07:12 unbyte

any updates

dungmv avatar May 11 '22 07:05 dungmv