|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "os" |
| 4 | + "net/http" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + spinhttp "github.com/fermyon/spin/sdk/go/http" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + // Default value for HTTP status code |
| 12 | + DefaultStatusCode int = http.StatusFound |
| 13 | + // Default value for redirection target |
| 14 | + DefaultRedirectionTarget string = "/" |
| 15 | + // Key for loading desired destination |
| 16 | + destinationKey string = "destination" |
| 17 | + // Key for loading desired HTTP status code |
| 18 | + statusCodeKey string = "statuscode" |
6 | 19 | ) |
7 | 20 |
|
| 21 | +func init() { |
| 22 | + r := NewSpinRedirect() |
| 23 | + spinhttp.Handle(r.handleFunc) |
| 24 | +} |
| 25 | + |
8 | 26 | func main() { |
9 | | - dest := os.Getenv("DESTINATION") |
10 | | - fmt.Printf("status: 302\nlocation: %s\n\n", dest) |
| 27 | +} |
| 28 | + |
| 29 | +// SpinRedirect is a struct that provides a handleFunc |
| 30 | +// for redirecting to a destination URL using configurable HTTP status code. |
| 31 | +type SpinRedirect struct { |
| 32 | + cfg ConfigReader |
| 33 | +} |
| 34 | + |
| 35 | +// NewSpinRedirect returns a new SpinRedirect |
| 36 | +func NewSpinRedirect() SpinRedirect { |
| 37 | + return SpinRedirect{ |
| 38 | + cfg: NewDefaultConfigReader(), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (s SpinRedirect) handleFunc(w http.ResponseWriter, r *http.Request) { |
| 43 | + dest, _ := s.getDestination() |
| 44 | + code, _ := s.getStatusCode(r.Method) |
| 45 | + |
| 46 | + w.Header().Set("Location", dest) |
| 47 | + w.WriteHeader(code) |
| 48 | +} |
| 49 | + |
| 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 |
| 58 | +} |
| 59 | + |
| 60 | +// getStatusCode returns the HTTP status code |
| 61 | +// If no status code is found, or if the provided value is invalid, |
| 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) { |
| 64 | + str := s.cfg.Get(statusCodeKey) |
| 65 | + code, err := strconv.Atoi(str) |
| 66 | + if err != nil { |
| 67 | + return DefaultStatusCode, false |
| 68 | + } |
| 69 | + if !isValidRedirectStatusCode(code, method) { |
| 70 | + return DefaultStatusCode, false |
| 71 | + } |
| 72 | + return code, true |
| 73 | +} |
| 74 | + |
| 75 | +// isValidRedirectStatusCode returns true if the provided status code is valid for redirection |
| 76 | +func isValidRedirectStatusCode(code int, method string) bool { |
| 77 | + if code == http.StatusSeeOther && |
| 78 | + (method == http.MethodPut || method == http.MethodPost) { |
| 79 | + return true |
| 80 | + } |
| 81 | + validCodes := []int{ |
| 82 | + http.StatusMovedPermanently, |
| 83 | + http.StatusFound, |
| 84 | + http.StatusTemporaryRedirect, |
| 85 | + http.StatusPermanentRedirect, |
| 86 | + } |
| 87 | + |
| 88 | + for _, c := range validCodes { |
| 89 | + if c == code { |
| 90 | + return true |
| 91 | + } |
| 92 | + } |
| 93 | + return false |
11 | 94 | } |
0 commit comments