-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbadge_handler.go
More file actions
56 lines (46 loc) · 1.34 KB
/
badge_handler.go
File metadata and controls
56 lines (46 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"net/http"
_ "embed"
"github.com/go-chi/render"
"github.com/tinyauthapp/analytics/queries"
)
type BadgeHandler struct {
queries *queries.Queries
}
func NewBadgeHandler(queries *queries.Queries) *BadgeHandler {
return &BadgeHandler{
queries: queries,
}
}
type shieldsioData struct {
SchemaVersion int `json:"schemaVersion"`
Label string `json:"label"`
Message string `json:"message"`
Color string `json:"color,omitempty"`
LabelColor string `json:"labelColor,omitempty"`
IsError bool `json:"isError,omitempty"`
NamedLogo string `json:"namedLogo,omitempty"`
LogoSvg string `json:"logoSvg,omitempty"`
LogoColor string `json:"logoColor,omitempty"`
LogoSize string `json:"logoSize,omitempty"`
Style string `json:"style,omitempty"`
}
func (h *BadgeHandler) Badge(w http.ResponseWriter, r *http.Request) {
instanceCount, err := h.queries.GetInstanceCount(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
badgeData := shieldsioData{
SchemaVersion: 1,
Label: "Active Instances",
Message: fmt.Sprintf("%d", instanceCount),
Color: "brightgreen",
LabelColor: "grey",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
render.JSON(w, r, badgeData)
}