diff --git a/cmd/badges/main.go b/cmd/badges/main.go
index 05b671bad..1f50792ab 100644
--- a/cmd/badges/main.go
+++ b/cmd/badges/main.go
@@ -64,9 +64,11 @@ func main() {
textLength = "200"
}
+ bs := parseBadgeSettings(r.URL.Query())
+
log.Info().Str(uniqueCode, "42c5269c").Str("loc", loc.String()).Str("category", category).Send()
w.Header().Set("Content-Type", "image/svg+xml;charset=utf-8")
- _, _ = w.Write([]byte(``))
+ _, _ = w.Write([]byte(``))
})
addr := ":8080"
@@ -170,6 +172,55 @@ func processUrlPath(path string) (location, error) {
}, nil
}
+type badgeSettings struct {
+ FontColor string
+ TextShadowColor string
+ TopShadowAccentColor string
+ TitleBackgroundColor string
+ BadgeBackgroundColor string
+}
+
+// Parses badge settings from url query params
+// if error, ignore and return default badge settings
+func parseBadgeSettings(values url.Values) *badgeSettings {
+ bs := badgeSettings{
+ FontColor: "fff",
+ TextShadowColor: "010101",
+ TopShadowAccentColor: "bbb",
+ TitleBackgroundColor: "555",
+ BadgeBackgroundColor: "4c1",
+ }
+
+ fontColor := strings.ToLower(values.Get("font-color"))
+ textShadowColor := strings.ToLower(values.Get("font-shadow-color"))
+ topShadowAccentColor := strings.ToLower(values.Get("top-shadow-accent-color"))
+ titleBackgroundColor := strings.ToLower(values.Get("title-bg-color"))
+ badgeBackgroundColor := strings.ToLower(values.Get("badge-bg-color"))
+
+ // Ensure valid colors
+ r, err := regexp.Compile(`^(?:(?:[\da-f]{3}){1,2}|(?:[\da-f]{4}){1,2})$`)
+ if err != nil {
+ return &bs
+ }
+ if r.MatchString(fontColor) {
+ bs.FontColor = fontColor
+ }
+ if r.MatchString(textShadowColor) {
+ bs.TextShadowColor = textShadowColor
+ }
+ if r.MatchString(topShadowAccentColor) {
+ bs.TopShadowAccentColor = topShadowAccentColor
+ }
+ if r.MatchString(titleBackgroundColor) {
+ bs.TitleBackgroundColor = titleBackgroundColor
+ }
+ if r.MatchString(badgeBackgroundColor) {
+ bs.BadgeBackgroundColor = badgeBackgroundColor
+ }
+
+ return &bs
+}
+
// formatCount turns a float into a string usable for display
// to the user so, 2532 would be 2.5k and such up the various
// units
diff --git a/cmd/badges/main_test.go b/cmd/badges/main_test.go
index b35ce6dbe..f354bf2dc 100644
--- a/cmd/badges/main_test.go
+++ b/cmd/badges/main_test.go
@@ -1,6 +1,7 @@
package main
import (
+ "net/url"
"reflect"
"testing"
)
@@ -121,3 +122,78 @@ func Test_processPath(t *testing.T) {
})
}
}
+func Test_parseBadgeSettings(t *testing.T) {
+
+ defaultSettings := &badgeSettings{
+ FontColor: "fff",
+ TextShadowColor: "010101",
+ TopShadowAccentColor: "bbb",
+ TitleBackgroundColor: "555",
+ BadgeBackgroundColor: "4c1",
+ }
+
+ tests := []struct {
+ name string
+ values url.Values
+ want *badgeSettings
+ }{
+ {
+ name: "default settings",
+ values: url.Values{},
+ want: defaultSettings,
+ },
+ {
+ name: "valid custom settings",
+ values: url.Values{
+ "font-color": []string{"abcdef"},
+ "font-shadow-color": []string{"def"},
+ "top-shadow-accent-color": []string{"321def"},
+ "title-bg-color": []string{"456"},
+ "badge-bg-color": []string{"789"},
+ },
+ want: &badgeSettings{
+ FontColor: "abcdef",
+ TextShadowColor: "def",
+ TopShadowAccentColor: "321def",
+ TitleBackgroundColor: "456",
+ BadgeBackgroundColor: "789",
+ },
+ },
+ {
+ name: "partially-valid custom settings",
+ values: url.Values{
+ "font-color": []string{"123321"},
+ "font-shadow-color": []string{"invalid"},
+ "top-shadow-accent-color": []string{"5a534332"},
+ "title-bg-color": []string{"dd"},
+ "badge-bg-color": []string{"X&^%^#$^$@%20"},
+ },
+ want: &badgeSettings{
+ FontColor: "123321",
+ TextShadowColor: defaultSettings.TextShadowColor,
+ TopShadowAccentColor: "5a534332",
+ TitleBackgroundColor: defaultSettings.TitleBackgroundColor,
+ BadgeBackgroundColor: defaultSettings.BadgeBackgroundColor,
+ },
+ },
+ {
+ name: "invalid custom settings",
+ values: url.Values{
+ "font-color": []string{"invalid"},
+ "font-shadow-color": []string{"invalid"},
+ "top-shadow-accent-color": []string{"invalid"},
+ "title-bg-color": []string{"invalid"},
+ "badge-bg-color": []string{"invalid"},
+ },
+ want: defaultSettings,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := parseBadgeSettings(tt.values); !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("parseBadgeSettings() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}