-
Notifications
You must be signed in to change notification settings - Fork 1.6k
En/http redirects #1640
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
base: development
Are you sure you want to change the base?
En/http redirects #1640
Changes from all commits
83931e0
6ea1efa
cb505b9
29d5a9b
20b8a05
53921ee
f8149ae
ae887cc
a7a956e
5cfb815
893b18f
b64fb20
1399452
535d760
b770535
70318b9
1d426f9
eeec99a
4edb668
fc05212
1e6a52d
bc2293f
ffe4a9b
7cd1be3
565e8ae
43ef4ac
9315e98
e6916ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -30,6 +30,7 @@ func (r Responder) Respond(data any, err error) { | |||||||||||||||||||||||||||||||
statusCode, errorObj := r.determineResponse(data, err) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
var resp any | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
switch v := data.(type) { | ||||||||||||||||||||||||||||||||
case resTypes.Raw: | ||||||||||||||||||||||||||||||||
resp = v.Data | ||||||||||||||||||||||||||||||||
|
@@ -47,6 +48,19 @@ func (r Responder) Respond(data any, err error) { | |||||||||||||||||||||||||||||||
v.Render(r.w) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||
case resTypes.Redirect: | ||||||||||||||||||||||||||||||||
switch r.method { | ||||||||||||||||||||||||||||||||
case http.MethodPost, http.MethodPut, http.MethodPatch: | ||||||||||||||||||||||||||||||||
statusCode = http.StatusSeeOther // 303 | ||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||
statusCode = http.StatusFound // 302 by default | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
Comment on lines
+51
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎨 style
Suggested change
Comment on lines
+51
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is something I cannot explain: why we computed this line 30
And we here don't care about statusCode we computed Shouldn't we check if data.type is not redirect before calling determineResponse ? And so redirect without calling it |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
r.w.Header().Set("Location", v.URL) | ||||||||||||||||||||||||||||||||
r.w.WriteHeader(statusCode) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||
// handling where an interface contains a nullable type with a nil value. | ||||||||||||||||||||||||||||||||
if isNil(data) { | ||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package response | ||
|
||
type Redirect struct { | ||
URL string | ||
} | ||
|
||
// NewRedirect creates a redirect response with the specified URL and status code. | ||
func NewRedirect(url string) Redirect { | ||
return Redirect{ | ||
URL: url, | ||
} | ||
} | ||
Comment on lines
+7
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch |
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.
Nitpicking, there are no need to update this, especially if we consider it diverges from Go standards