Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 42 additions & 35 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)

// Data ... is the collection of inputs we need to fill our template
type Data struct {
BackgroundColor string
Percentage int
Progress int
Percentage float64
Progress float64
PickedColor string
}

Expand All @@ -35,19 +34,20 @@ func fixDir() {
}
}

func pickColor(percentage int, successColor string, warningColor string, dangerColor string) string {
func pickColor(percentage float64, successColor, warningColor, dangerColor string) string {
pickedColor := green
if successColor != "" {
pickedColor = "#" + successColor
}

if percentage >= 0 && percentage < 33 {
switch {
case percentage >= 0 && percentage < 33:
if dangerColor != "" {
pickedColor = "#" + dangerColor
} else {
pickedColor = red
}
} else if percentage >= 33 && percentage < 70 {
case percentage >= 33 && percentage < 70:
if warningColor != "" {
pickedColor = "#" + warningColor
} else {
Expand All @@ -63,39 +63,46 @@ func init() {
functions.HTTP("Progress", Progress)
}

// Progress ... Entrypoint of our Cloud Function
func Progress(w http.ResponseWriter, r *http.Request) {
var id = fmt.Sprintf(path.Base(r.URL.Path))

if percentage, err := strconv.Atoi(id); err == nil {
// Template helper to check if a float is an integer
func isInt(f float64) bool {
return f == float64(int(f))
}

// Read (with the intention to overwrite) success, warning, and danger colors if provided
successColor := r.URL.Query().Get("successColor")
warningColor := r.URL.Query().Get("warningColor")
dangerColor := r.URL.Query().Get("dangerColor")
func Progress(w http.ResponseWriter, r *http.Request) {
id := path.Base(r.URL.Path)

data := Data{
BackgroundColor: grey,
Percentage: percentage,
Progress: percentage - (percentage / 10),
PickedColor: pickColor(percentage, successColor, warningColor, dangerColor),
}
percentage, err := strconv.ParseFloat(id, 64)
if err != nil {
http.Error(w, "Invalid percentage value", http.StatusBadRequest)
return
}

tpl, err := template.ParseFiles("progress.html")
successColor := r.URL.Query().Get("successColor")
warningColor := r.URL.Query().Get("warningColor")
dangerColor := r.URL.Query().Get("dangerColor")

if err != nil {
log.Fatalln(err)
}

buf := new(bytes.Buffer)
data := Data{
BackgroundColor: grey,
Percentage: percentage,
Progress: percentage - (percentage / 10),
PickedColor: pickColor(percentage, successColor, warningColor, dangerColor),
}

err = tpl.Execute(buf, data)
if err != nil {
log.Fatalln(err)
}
// Parse template with custom "isInt" function
tpl := template.New("progress.html").Funcs(template.FuncMap{
"isInt": isInt,
})
tpl, err = tpl.ParseFiles("progress.html")
if err != nil {
log.Fatalln("Error parsing template:", err)
}

fmt.Printf("The percentage is: %d\n", percentage)
w.Header().Add("Content-Type", "image/svg+xml")
fmt.Fprintf(w, buf.String())
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, data); err != nil {
log.Fatalln("Error executing template:", err)
}
}

fmt.Printf("The percentage is: %v\n", percentage)
w.Header().Set("Content-Type", "image/svg+xml")
_, _ = w.Write(buf.Bytes())
}
10 changes: 7 additions & 3 deletions progress.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<svg width="90.0" height="20" xmlns="http://www.w3.org/2000/svg">
<linearGradient id="a" x2="0" y2="100%%">
<linearGradient id="a" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".2"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
Expand All @@ -8,7 +8,11 @@
<rect rx="4" width="90.0" height="20" fill="url(#a)"/>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="45.0" y="14">
{{.Percentage}}%%
{{if isInt .Percentage}}
{{printf "%.0f%%" .Percentage}}
{{else}}
{{printf "%.2f%%" .Percentage}}
{{end}}
</text>
</g>
</svg>
</svg>