Skip to content

Commit

Permalink
Merge branch 'rzlamrr-test'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan13888 committed May 31, 2021
2 parents d0ecfda + 9877b06 commit c945502
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Also, **VBR features could be configured as a [HTTP query parameter](https://en.
### Options (add as a [HTTP query parameter](https://en.wikipedia.org/wiki/Query_string))
**IMPORTANT: Other than the `page_id` option, there exists additional styling options and functional options (in the future). More details about the options could be found at the bottom of the [Shields.io website](https://shields.io/). Also, all the options are specified as HTTP parameters!!!**
- `page_id=<your id>` --> identifies your badge, make this unique to yourself. eg. `<your username.visitor.badge.reloaded` or `<username>-<username>`
- `color=<colour here>` --> the hex colour code of your badge, **do NOT include the `#`**
- `color=<colour here>` --> the hex colour code of the badge background, **do NOT include the `#`**
- `lcolor=<colour here>` --> the hex colour code of the label, **do NOT include the `#`**
- `style=<style name>` --> refer to the Sheilds.IO website for the available options
- `text=<Some text other than "Visitors">` --> put a customizable label on your badge
- `logo` --> logo to put beside the badge, go to https://simpleicons.org/ for the available names
Expand Down
17 changes: 9 additions & 8 deletions badges.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (

// TODO: add struct to handle badge input
func generateBadge(SHIELDS_URL string, text string, cnt string, colour string,
style string, logo string, logoColour string) []byte {
leftColour string, style string, logo string, logoColour string) []byte {
if badgeErrorCount > 5 && badgeErrorCount < 10 {
SHIELDS_URL = DEFAULT_SHIELDS
log.Err(fmt.Errorf("experienced %d errors when generating badges", badgeErrorCount)).Msg("reseting SHIELDS_URL")
}
data, err := createBadge(SHIELDS_URL, text, cnt, colour, style, logo, logoColour)
data, err := createBadge(SHIELDS_URL, text, cnt, colour, leftColour, style, logo, logoColour)
if err != nil {
fb, err := getFallBackBadge(err, text, cnt, colour, style, logo, logoColour)
fb, err := getFallBackBadge(err, text, cnt, colour, leftColour, style, logo, logoColour)
if err != nil {
logError(err)
badgeErrorCount++
Expand All @@ -30,19 +30,20 @@ func generateBadge(SHIELDS_URL string, text string, cnt string, colour string,
return data
}
func createBadge(SHIELDS_URL string, text string, cnt string, colour string,
style string, logo string, logoColour string) ([]byte, error) {
leftColour string, style string, logo string, logoColour string) ([]byte, error) {
log.Debug().
Str("shields_url", SHIELDS_URL).
Str("text", text).
Str("count", cnt).
Str("colour", colour).
Str("leftColour", leftColour).
Str("style", style).
Str("logo", logo).
Str("logo_colour", logoColour).
Msg("Generating badge")

url := fmt.Sprintf("%s/badge/%s-%s-%s?style=%s&logo=%s&logoColor=%s",
SHIELDS_URL, text, cnt, colour, style, logo, logoColour)
url := fmt.Sprintf("%s/badge/%s-%s-%s?labelColor=%s&style=%s&logo=%s&logoColor=%s",
SHIELDS_URL, text, cnt, colour, leftColour, style, logo, logoColour)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand All @@ -69,11 +70,11 @@ func createBadge(SHIELDS_URL string, text string, cnt string, colour string,
}

func getFallBackBadge(err error, text string, cnt string, colour string,
style string, logo string, logoColour string) ([]byte, error) {
leftColour string, style string, logo string, logoColour string) ([]byte, error) {
logError(err)
badgeErrorCount++
// FALLBACK if local shields fail
return createBadge(DEFAULT_SHIELDS, text, cnt, colour, style, logo, logoColour)
return createBadge(DEFAULT_SHIELDS, text, cnt, colour, leftColour, style, logo, logoColour)
}

func getErrorBadge() []byte {
Expand Down
4 changes: 3 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
// defaults
const (
colour = "blue"
leftColour = "grey"
style = "flat"
text = "Visitors"
logo = "" // https://simpleicons.org/
Expand Down Expand Up @@ -117,6 +118,7 @@ func getBadge(w http.ResponseWriter, r *http.Request) {
hash := getHash(page)

colour := qryParam("color", r, colour)
leftColour := qryParam("lcolor", r, leftColour)
style := qryParam("style", r, style)
text := qryParam("text", r, text)
logo := qryParam("logo", r, logo)
Expand All @@ -128,7 +130,7 @@ func getBadge(w http.ResponseWriter, r *http.Request) {

cnt := updateCounter(useCache, hash)

badge := generateBadge(SHIELDS_URL, text, cnt, colour, style, logo, logoColour)
badge := generateBadge(SHIELDS_URL, text, cnt, colour, leftColour, style, logo, logoColour)

date := time.Now().Add(time.Minute * -10).Format(http.TimeFormat)
w.Header().Set("Content-Type", "image/svg+xml")
Expand Down

0 comments on commit c945502

Please sign in to comment.