Skip to content

Commit 6bbdfa6

Browse files
committed
chore: getStatusCode and getDestination now return a boolean too
The boolean indicates if users provided value is valid or must have been replaced with the corresponding default value. For now, we ignore this boolean. Once we can detect that user is running in debug/verbose mode, we can use the boolean as gate for writing to stdout Signed-off-by: Thorsten Hans <[email protected]>
1 parent e5919c0 commit 6bbdfa6

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

redirect.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"net/http"
65
"strconv"
76

@@ -11,6 +10,8 @@ import (
1110
const (
1211
// Default value for HTTP status code
1312
DefaultStatusCode int = http.StatusFound
13+
// Default value for redirection target
14+
DefaultRedirectionTarget string = "/"
1415
// Key for loading desired destination
1516
destinationKey string = "destination"
1617
// Key for loading desired HTTP status code
@@ -39,34 +40,36 @@ func NewSpinRedirect() SpinRedirect {
3940
}
4041

4142
func (s SpinRedirect) handleFunc(w http.ResponseWriter, r *http.Request) {
42-
dest := s.getDestination()
43-
code := s.getStatusCode(r.Method)
43+
dest, _ := s.getDestination()
44+
code, _ := s.getStatusCode(r.Method)
4445

4546
w.Header().Set("Location", dest)
4647
w.WriteHeader(code)
4748
}
4849

49-
// getDestination returns the destination URL
50-
// If no destination is found, an empty string is returned.
51-
func (s SpinRedirect) getDestination() string {
52-
return s.cfg.Get(destinationKey)
50+
// getDestination returns the destination URL and a boolean indicating if the user provided a destination URL.
51+
// If no destination is found, DefaultRedirectionTarget (/) is returned.
52+
func (s SpinRedirect) getDestination() (string, bool) {
53+
d := s.cfg.Get(destinationKey)
54+
if len(d) == 0 {
55+
return DefaultRedirectionTarget, false
56+
}
57+
return d, true
5358
}
5459

5560
// getStatusCode returns the HTTP status code
5661
// If no status code is found, or if the provided value is invalid,
57-
// DefaultStatusCode is returned.
58-
func (s SpinRedirect) getStatusCode(method string) int {
62+
// DefaultStatusCode is returned along with a boolean indicating if the user provided a valid status code.
63+
func (s SpinRedirect) getStatusCode(method string) (int, bool) {
5964
str := s.cfg.Get(statusCodeKey)
6065
code, err := strconv.Atoi(str)
6166
if err != nil {
62-
return DefaultStatusCode
67+
return DefaultStatusCode, false
6368
}
6469
if !isValidRedirectStatusCode(code, method) {
65-
fmt.Printf("Invalid status code provided: %d. Will use %d instead.\n", code, DefaultStatusCode)
66-
return DefaultStatusCode
70+
return DefaultStatusCode, false
6771
}
68-
69-
return code
72+
return code, true
7073
}
7174

7275
// isValidRedirectStatusCode returns true if the provided status code is valid for redirection

0 commit comments

Comments
 (0)