Skip to content

Commit 5d06d5e

Browse files
committed
clean: remove trivial comments
1 parent 11b560c commit 5d06d5e

File tree

8 files changed

+22
-254
lines changed

8 files changed

+22
-254
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,31 @@ Display your SHM metrics in your README with embeddable SVG badges.
5555

5656
#### Active Instances
5757

58-
![Instances Badge](https://your-shm-server.example.com/badge/your-app/instances)
5958

59+
![Instances](https://img.shields.io/badge/instances-260-00D084?style=flat-square)
6060
```markdown
6161
![Instances](https://your-shm-server.example.com/badge/your-app/instances)
6262
```
6363

6464
#### Most Used Version
6565

66-
![Version Badge](https://your-shm-server.example.com/badge/your-app/version)
66+
![Version](https://img.shields.io/badge/version-1.2.0-8B5CF6?style=flat-square)
6767

6868
```markdown
6969
![Version](https://your-shm-server.example.com/badge/your-app/version)
7070
```
7171

7272
#### Aggregated Metrics
7373

74-
![Users Badge](https://your-shm-server.example.com/badge/your-app/metric/users_count)
74+
![Users](https://img.shields.io/badge/users-1.2k-00D084?style=flat-square)
7575

7676
```markdown
7777
![Users](https://your-shm-server.example.com/badge/your-app/metric/users_count)
7878
```
7979

8080
#### Combined Stats
8181

82-
![Adoption Badge](https://your-shm-server.example.com/badge/your-app/combined?metric=users_count)
82+
![Adoption](https://img.shields.io/badge/adoption-1.2k%20%2F%2042-6366F1?style=flat-square)
8383

8484
```markdown
8585
![Adoption](https://your-shm-server.example.com/badge/your-app/combined?metric=users_count)

internal/adapters/http/badge.go

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,41 @@ import (
99
"github.com/btouchard/shm/internal/services/badge"
1010
)
1111

12-
// BadgeInstances generates a badge showing the number of active instances.
13-
// GET /badge/{app-slug}/instances
1412
func (h *Handlers) BadgeInstances(w http.ResponseWriter, r *http.Request) {
15-
// Extract app slug from path
1613
appSlug := extractSlugFromPath(r.URL.Path, "/badge/", "/instances")
1714
if appSlug == "" {
1815
renderErrorBadge(w, "invalid slug")
1916
return
2017
}
2118

22-
// Get active instances count
2319
count, err := h.dashboard.GetActiveInstancesCount(r.Context(), appSlug)
2420
if err != nil {
2521
h.logger.Warn("failed to get instances count", "slug", appSlug, "error", err)
2622
renderErrorBadge(w, "error")
2723
return
2824
}
2925

30-
// Determine color based on count
3126
color := badge.GetInstancesColor(count)
32-
33-
// Allow custom color via query param
3427
if customColor := r.URL.Query().Get("color"); customColor != "" {
3528
color = "#" + strings.TrimPrefix(customColor, "#")
3629
}
3730

38-
// Allow custom label
3931
label := r.URL.Query().Get("label")
4032
if label == "" {
4133
label = "instances"
4234
}
4335

44-
// Generate badge
4536
b := badge.NewBadge(label, badge.FormatNumber(float64(count)), color)
4637
renderSVGBadge(w, b.ToSVG())
4738
}
4839

49-
// BadgeVersion generates a badge showing the most used version.
50-
// GET /badge/{app-slug}/version
5140
func (h *Handlers) BadgeVersion(w http.ResponseWriter, r *http.Request) {
52-
// Extract app slug from path
5341
appSlug := extractSlugFromPath(r.URL.Path, "/badge/", "/version")
5442
if appSlug == "" {
5543
renderErrorBadge(w, "invalid slug")
5644
return
5745
}
5846

59-
// Get most used version
6047
version, err := h.dashboard.GetMostUsedVersion(r.Context(), appSlug)
6148
if err != nil {
6249
h.logger.Warn("failed to get version", "slug", appSlug, "error", err)
@@ -68,28 +55,21 @@ func (h *Handlers) BadgeVersion(w http.ResponseWriter, r *http.Request) {
6855
version = "no data"
6956
}
7057

71-
// Allow custom color via query param
7258
color := badge.ColorPurple
7359
if customColor := r.URL.Query().Get("color"); customColor != "" {
7460
color = "#" + strings.TrimPrefix(customColor, "#")
7561
}
7662

77-
// Allow custom label
7863
label := r.URL.Query().Get("label")
7964
if label == "" {
8065
label = "version"
8166
}
8267

83-
// Generate badge
8468
b := badge.NewBadge(label, version, color)
8569
renderSVGBadge(w, b.ToSVG())
8670
}
8771

88-
// BadgeMetric generates a badge showing an aggregated metric value.
89-
// GET /badge/{app-slug}/metric/{metric-name}
9072
func (h *Handlers) BadgeMetric(w http.ResponseWriter, r *http.Request) {
91-
// Extract app slug and metric name from path
92-
// Path format: /badge/{slug}/metric/{name}
9373
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/badge/"), "/")
9474
if len(parts) < 3 || parts[1] != "metric" {
9575
renderErrorBadge(w, "invalid path")
@@ -104,96 +84,74 @@ func (h *Handlers) BadgeMetric(w http.ResponseWriter, r *http.Request) {
10484
return
10585
}
10686

107-
// Get aggregated metric
10887
value, err := h.dashboard.GetAggregatedMetric(r.Context(), appSlug, metricName)
10988
if err != nil {
11089
h.logger.Warn("failed to get metric", "slug", appSlug, "metric", metricName, "error", err)
11190
renderErrorBadge(w, "error")
11291
return
11392
}
11493

115-
// Determine color based on value
11694
color := badge.GetMetricColor(value)
117-
118-
// Allow custom color via query param
11995
if customColor := r.URL.Query().Get("color"); customColor != "" {
12096
color = "#" + strings.TrimPrefix(customColor, "#")
12197
}
12298

123-
// Allow custom label
12499
label := r.URL.Query().Get("label")
125100
if label == "" {
126101
label = metricName
127102
}
128103

129-
// Generate badge
130104
b := badge.NewBadge(label, badge.FormatNumber(value), color)
131105
renderSVGBadge(w, b.ToSVG())
132106
}
133107

134-
// BadgeCombined generates a combined badge showing metric value and instance count.
135-
// GET /badge/{app-slug}/combined?metric=users_count
136108
func (h *Handlers) BadgeCombined(w http.ResponseWriter, r *http.Request) {
137-
// Extract app slug from path
138109
appSlug := extractSlugFromPath(r.URL.Path, "/badge/", "/combined")
139110
if appSlug == "" {
140111
renderErrorBadge(w, "invalid slug")
141112
return
142113
}
143114

144-
// Get metric name from query param (default: users_count)
145115
metricName := r.URL.Query().Get("metric")
146116
if metricName == "" {
147117
metricName = "users_count"
148118
}
149119

150-
// Get combined stats
151120
metricValue, instanceCount, err := h.dashboard.GetCombinedStats(r.Context(), appSlug, metricName)
152121
if err != nil {
153122
h.logger.Warn("failed to get combined stats", "slug", appSlug, "metric", metricName, "error", err)
154123
renderErrorBadge(w, "error")
155124
return
156125
}
157126

158-
// Allow custom color
159127
color := badge.ColorIndigo
160128
if customColor := r.URL.Query().Get("color"); customColor != "" {
161129
color = "#" + strings.TrimPrefix(customColor, "#")
162130
}
163131

164-
// Allow custom label
165132
label := r.URL.Query().Get("label")
166133
if label == "" {
167134
label = "adoption"
168135
}
169136

170-
// Format compact: "1.2k / 42"
171137
value := badge.FormatNumber(metricValue) + " / " + badge.FormatNumber(float64(instanceCount))
172-
173-
// Generate badge
174138
b := badge.NewBadge(label, value, color)
175139
renderSVGBadge(w, b.ToSVG())
176140
}
177141

178-
// Helper functions
179-
180-
// extractSlugFromPath extracts the app slug from a badge path.
181-
// Example: /badge/my-app/instances -> "my-app"
182142
func extractSlugFromPath(path, prefix, suffix string) string {
183143
s := strings.TrimPrefix(path, prefix)
184144
s = strings.TrimSuffix(s, suffix)
185145
return s
186146
}
187147

188-
// renderSVGBadge writes an SVG badge to the response with proper headers.
189148
func renderSVGBadge(w http.ResponseWriter, svg string) {
190149
w.Header().Set("Content-Type", "image/svg+xml;charset=utf-8")
191-
w.Header().Set("Cache-Control", "public, max-age=300") // 5 minutes cache
150+
w.Header().Set("Cache-Control", "public, max-age=300")
192151
w.WriteHeader(http.StatusOK)
193152
_, _ = w.Write([]byte(svg))
194153
}
195154

196-
// renderErrorBadge renders an error badge.
197155
func renderErrorBadge(w http.ResponseWriter, message string) {
198156
b := badge.NewBadge("error", message, badge.ColorRed)
199157
renderSVGBadge(w, b.ToSVG())

internal/adapters/http/router.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,29 @@ func NewRouter(cfg RouterConfig) *http.ServeMux {
3030
logger = slog.Default()
3131
}
3232

33-
// Create repositories from store
3433
instanceRepo := cfg.Store.InstanceRepository()
3534
snapshotRepo := cfg.Store.SnapshotRepository()
3635
applicationRepo := cfg.Store.ApplicationRepository()
3736
dashboardReader := cfg.Store.DashboardReader()
3837

39-
// Create GitHub service
4038
githubSvc := github.NewStarsService(cfg.GitHubToken)
4139
githubSvc.StartCleanup(context.Background())
4240

43-
// Create application services
4441
applicationSvc := app.NewApplicationService(applicationRepo, githubSvc, logger)
4542
instanceSvc := app.NewInstanceService(instanceRepo, applicationSvc)
4643
snapshotSvc := app.NewSnapshotService(snapshotRepo, instanceRepo)
4744
dashboardSvc := app.NewDashboardService(dashboardReader)
4845

49-
// Start background scheduler for GitHub stars refresh
5046
scheduler := services.NewScheduler(applicationSvc, logger)
5147
go scheduler.Start(context.Background())
5248

53-
// Create HTTP handlers
5449
handlers := NewHandlers(instanceSvc, snapshotSvc, applicationSvc, dashboardSvc, logger)
55-
56-
// Create auth middleware
5750
authMW := NewAuthMiddlewareFromService(instanceSvc, logger)
58-
59-
// Create router
6051
mux := http.NewServeMux()
6152

62-
// Health check (no auth, no rate limit)
6353
mux.HandleFunc("/api/v1/healthcheck", handlers.Healthcheck)
6454

65-
// Badge endpoints (public, no auth, no rate limit)
6655
mux.HandleFunc("/badge/", func(w http.ResponseWriter, r *http.Request) {
67-
// Route to appropriate badge handler based on path suffix
6856
path := r.URL.Path
6957
switch {
7058
case strings.HasSuffix(path, "/instances"):
@@ -80,33 +68,25 @@ func NewRouter(cfg RouterConfig) *http.ServeMux {
8068
}
8169
})
8270

83-
// Client endpoints
8471
rl := cfg.RateLimiter
8572
if rl != nil {
8673
mux.HandleFunc("/v1/register", rl.RegisterMiddleware(handlers.Register))
8774
mux.HandleFunc("/v1/activate", rl.RegisterMiddleware(authMW.RequireSignature(handlers.Activate)))
8875
mux.HandleFunc("/v1/snapshot", rl.SnapshotMiddleware(authMW.RequireSignature(handlers.Snapshot)))
89-
90-
// Admin endpoints
9176
mux.HandleFunc("/api/v1/admin/stats", rl.AdminMiddleware(handlers.AdminStats))
9277
mux.HandleFunc("/api/v1/admin/instances", rl.AdminMiddleware(handlers.AdminInstances))
9378
mux.HandleFunc("/api/v1/admin/metrics/", rl.AdminMiddleware(handlers.AdminMetrics))
94-
95-
// Application endpoints
9679
mux.HandleFunc("/api/v1/admin/applications", rl.AdminMiddleware(handlers.AdminListApplications))
9780
mux.HandleFunc("/api/v1/admin/applications/", rl.AdminMiddleware(func(w http.ResponseWriter, r *http.Request) {
98-
// Route to appropriate handler based on path suffix
9981
if r.URL.Path == "/api/v1/admin/applications/" {
10082
handlers.AdminListApplications(w, r)
10183
return
10284
}
103-
// Check if it's a refresh-stars request
10485
if len(r.URL.Path) > len("/api/v1/admin/applications/") &&
10586
r.URL.Path[len(r.URL.Path)-len("/refresh-stars"):] == "/refresh-stars" {
10687
handlers.AdminRefreshStars(w, r)
10788
return
10889
}
109-
// Otherwise, treat as get/update application
11090
if r.Method == http.MethodGet {
11191
handlers.AdminGetApplication(w, r)
11292
} else if r.Method == http.MethodPut {
@@ -116,15 +96,12 @@ func NewRouter(cfg RouterConfig) *http.ServeMux {
11696
}
11797
}))
11898
} else {
119-
// No rate limiting (for testing)
12099
mux.HandleFunc("/v1/register", handlers.Register)
121100
mux.HandleFunc("/v1/activate", authMW.RequireSignature(handlers.Activate))
122101
mux.HandleFunc("/v1/snapshot", authMW.RequireSignature(handlers.Snapshot))
123102
mux.HandleFunc("/api/v1/admin/stats", handlers.AdminStats)
124103
mux.HandleFunc("/api/v1/admin/instances", handlers.AdminInstances)
125104
mux.HandleFunc("/api/v1/admin/metrics/", handlers.AdminMetrics)
126-
127-
// Application endpoints
128105
mux.HandleFunc("/api/v1/admin/applications", handlers.AdminListApplications)
129106
mux.HandleFunc("/api/v1/admin/applications/", func(w http.ResponseWriter, r *http.Request) {
130107
if r.URL.Path == "/api/v1/admin/applications/" {

internal/services/badge/colors.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,18 @@
22

33
package badge
44

5-
// Color palette for badges - custom and original
65
const (
7-
// Label background (left side)
8-
ColorLabel = "#404040"
9-
10-
// Status colors (right side)
11-
ColorGreen = "#00D084" // Success, high numbers
12-
ColorBlue = "#3B82F6" // Info, neutral
13-
ColorYellow = "#F59E0B" // Warning, medium numbers
14-
ColorRed = "#EF4444" // Error, low numbers
15-
ColorPurple = "#8B5CF6" // Special, version info
16-
ColorGray = "#6B7280" // Inactive, no data
17-
ColorTeal = "#14B8A6" // Custom metrics
18-
ColorIndigo = "#6366F1" // Combined stats
6+
ColorLabel = "#555"
7+
ColorGreen = "#00D084"
8+
ColorBlue = "#3B82F6"
9+
ColorYellow = "#F59E0B"
10+
ColorRed = "#EF4444"
11+
ColorPurple = "#8B5CF6"
12+
ColorGray = "#6B7280"
13+
ColorTeal = "#14B8A6"
14+
ColorIndigo = "#6366F1"
1915
)
2016

21-
// GetInstancesColor returns the appropriate color based on instance count.
2217
func GetInstancesColor(count int) string {
2318
switch {
2419
case count >= 10:
@@ -30,8 +25,6 @@ func GetInstancesColor(count int) string {
3025
}
3126
}
3227

33-
// GetMetricColor returns the color for a custom metric based on value.
34-
// This is a simple heuristic - adjust based on your needs.
3528
func GetMetricColor(value float64) string {
3629
switch {
3730
case value >= 1000:

0 commit comments

Comments
 (0)