Skip to content

feat: add badge endpoint for shields.io#25

Merged
steveiliop56 merged 2 commits into
mainfrom
feat/badge
Apr 18, 2026
Merged

feat: add badge endpoint for shields.io#25
steveiliop56 merged 2 commits into
mainfrom
feat/badge

Conversation

@steveiliop56

@steveiliop56 steveiliop56 commented Apr 18, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • New Features

    • Added a badge endpoint (/v1/badge) that returns the active instance count in JSON; can be toggled via a new config (enabled by default)
    • Added a /robots.txt endpoint with standard directives
    • Favicon now served statically when the dashboard is enabled
  • Documentation

    • README updated with the BADGE_ENABLED configuration option and default value

@coderabbitai

coderabbitai Bot commented Apr 18, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8d5edbc9-7f40-4622-8e23-31cc97370742

📥 Commits

Reviewing files that changed from the base of the PR and between f4e8d31 and 79da280.

📒 Files selected for processing (1)
  • README.md
✅ Files skipped from review due to trivial changes (1)
  • README.md

📝 Walkthrough

Walkthrough

Adds a new /v1/badge JSON badge endpoint backed by a database count query, moves favicon embedding and robots.txt routing into main, and removes Favicon/Robots handlers from the dashboard handler.

Changes

Cohort / File(s) Summary
Badge Handler Implementation
badge_handler.go
New BadgeHandler type, NewBadgeHandler constructor, and Badge(w,r) method that calls Queries.GetInstanceCount and returns shields.io-style JSON.
Instance Count Database Query
queries.sql, queries/queries.sql.go
Added GetInstanceCount SQL query and generated Queries.GetInstanceCount(ctx) (int64, error) to return total rows from instances.
Route Reorganization & Configuration
main.go
Added BadgeEnabled config (default true), embedded favicon.ico and inline /favicon.ico handler, added /v1/badge route wired to BadgeHandler when enabled, always-available /robots.txt route, and deferred DashboardHandler initialization inside the dashboard-enabled block.
Dashboard Handler Cleanup
dashboard_handler.go
Removed embedded favicon.ico data and Favicon and Robots methods; other dashboard logic unchanged.

Sequence Diagram

sequenceDiagram
    participant Client
    participant Router as HTTP Router
    participant Badge as BadgeHandler
    participant Queries
    participant DB as Database

    Client->>Router: GET /v1/badge
    Router->>Badge: Badge(w, r)
    Badge->>Queries: GetInstanceCount(ctx)
    Queries->>DB: SELECT COUNT(*) FROM instances
    DB-->>Queries: count
    Queries-->>Badge: count, error
    Badge->>Badge: build shieldsio JSON (schema:1, label:"Active Instances", message:count)
    Badge->>Router: write 200 + application/json + JSON body
    Router-->>Client: 200 OK + badge JSON
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title directly describes the main addition: a new badge endpoint for shields.io integration, which is evident from the new BadgeHandler, badge route configuration, and supporting changes across multiple files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
badge_handler.go (1)

40-43: Log DB failures before returning 500.

When GetInstanceCount fails, adding a log at Line 40 makes this endpoint easier to operate and debug.

🔧 Suggested patch
 import (
 	"fmt"
+	"log/slog"
 	"net/http"
 
 	_ "embed"
@@
 	if err != nil {
+		slog.Error("failed to get instance count", "error", err)
 		w.WriteHeader(http.StatusInternalServerError)
 		return
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@badge_handler.go` around lines 40 - 43, The handler that calls
GetInstanceCount currently returns http.StatusInternalServerError on error
without logging; update the error branch in the handler (the code surrounding
the GetInstanceCount call) to log the error and context before
w.WriteHeader(http.StatusInternalServerError) — include the error value and any
identifying info (e.g., instanceID, params) using the existing logger (e.g.,
logger, log, or h.logger) so operators can debug failures to GetInstanceCount.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@main.go`:
- Around line 109-113: The inline HTTP handler registered with
router.Get("/robots.txt") (and any other inline handlers that call w.Write)
currently ignore the error return from w.Write; update these closures to capture
the returned error (err := w.Write(...)), check if err != nil, and handle it
(for example log the write failure with the request context and return early) so
failed writes to disconnected clients are not silently ignored; locate these in
the anonymous handler closures passed to router.Get and apply the same pattern
to any other inline handlers that write to the ResponseWriter.

---

Nitpick comments:
In `@badge_handler.go`:
- Around line 40-43: The handler that calls GetInstanceCount currently returns
http.StatusInternalServerError on error without logging; update the error branch
in the handler (the code surrounding the GetInstanceCount call) to log the error
and context before w.WriteHeader(http.StatusInternalServerError) — include the
error value and any identifying info (e.g., instanceID, params) using the
existing logger (e.g., logger, log, or h.logger) so operators can debug failures
to GetInstanceCount.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f218c775-52f3-4ad7-95e8-ae379fd50598

📥 Commits

Reviewing files that changed from the base of the PR and between ee91986 and f4e8d31.

📒 Files selected for processing (5)
  • badge_handler.go
  • dashboard_handler.go
  • main.go
  • queries.sql
  • queries/queries.sql.go
💤 Files with no reviewable changes (1)
  • dashboard_handler.go

Comment thread main.go
@steveiliop56 steveiliop56 merged commit 534f620 into main Apr 18, 2026
2 checks passed
@steveiliop56 steveiliop56 deleted the feat/badge branch April 18, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant