-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to debug logging and CORS handling
Signed-off-by: Alex Creasy <[email protected]>
- Loading branch information
1 parent
5cbf43c
commit 03b8e39
Showing
13 changed files
with
277 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func getEnvAsInt(name string, defaultVal int) int { | ||
if value, exists := os.LookupEnv(name); exists { | ||
if intValue, err := strconv.Atoi(value); err == nil { | ||
return intValue | ||
} | ||
} | ||
return defaultVal | ||
} | ||
|
||
func getEnvAsString(name string, defaultVal string) string { | ||
if value, exists := os.LookupEnv(name); exists { | ||
return value | ||
} | ||
return defaultVal | ||
} | ||
|
||
func parseLevel(s string) slog.Level { | ||
var level slog.Level | ||
err := level.UnmarshalText([]byte(s)) | ||
if err != nil { | ||
panic(fmt.Errorf("invalid log level: %s, valid levels are: error, warn, info, debug", s)) | ||
} | ||
return level | ||
} | ||
|
||
func newOriginParser(allowList *[]string, defaultVal string) func(s string) error { | ||
return func(s string) error { | ||
value := defaultVal | ||
|
||
if s != "" { | ||
value = s | ||
} | ||
|
||
if value == "" { | ||
allowList = nil | ||
return nil | ||
} | ||
|
||
for _, str := range strings.Split(s, ",") { | ||
*allowList = append(*allowList, strings.TrimSpace(str)) | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"testing" | ||
) | ||
|
||
var _ = Describe("newOriginParser helper function", func() { | ||
var originParser func(s string) error | ||
var allowList []string | ||
|
||
BeforeEach(func() { | ||
allowList = []string{} | ||
originParser = newOriginParser(&allowList, "") | ||
}) | ||
|
||
It("should parse a valid string list with 1 item", func() { | ||
expected := []string{"https://test.com"} | ||
|
||
err := originParser("https://test.com") | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(allowList).To(Equal(expected)) | ||
}) | ||
|
||
It("should parse a valid string list with 2 items", func() { | ||
expected := []string{"https://test.com", "https://test2.com"} | ||
|
||
err := originParser("https://test.com,https://test2.com") | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(allowList).To(Equal(expected)) | ||
}) | ||
|
||
It("should parse a valid string list with 2 items and extra spaces", func() { | ||
expected := []string{"https://test.com", "https://test2.com"} | ||
|
||
err := originParser("https://test.com, https://test2.com") | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(allowList).To(Equal(expected)) | ||
}) | ||
|
||
It("should parse an empty string", func() { | ||
err := originParser("") | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(allowList).To(BeEmpty()) | ||
}) | ||
|
||
It("should parse the wildcard string", func() { | ||
expected := []string{"*"} | ||
|
||
err := originParser("*") | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(allowList).To(Equal(expected)) | ||
}) | ||
|
||
}) | ||
|
||
func TestMainHelpers(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Main helpers suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.