feat: add /version endpoint with build-time SHA and timestamp #1

Open
opened 2026-05-06 13:38:18 +00:00 by gitea-mcp-bot · 0 comments

Context

Every service bootstrapped from this template should be able to report its own build identity at runtime. This is useful for health dashboards, deploy verification, and debugging — "which version is actually running right now?"

Task

Add a /version HTTP endpoint that returns build metadata injected at compile time via -ldflags.

Acceptance criteria

  • GET /version returns 200 OK with Content-Type: application/json
  • Response body: {"version": "<git-sha>", "built_at": "<RFC3339 UTC timestamp>"}
  • When built without ldflags (local dev), values fall back to "dev" and "unknown" respectively
  • The endpoint is registered in main.go alongside /healthz
  • task build injects the real values automatically via ldflags
  • A test covers at least the dev fallback case

Implementation notes

Package

Add internal/buildinfo/buildinfo.go:

package buildinfo

var (
    Version = "dev"
    BuiltAt = "unknown"
)

Handler

Register in main.go:

mux.HandleFunc("/version", buildinfo.Handler)

Where Handler is a simple http.HandlerFunc on the buildinfo package that marshals the two vars to JSON.

Taskfile ldflags

Update the build task in Taskfile.yml:

build:
  desc: Build the binary
  deps: [generate]
  vars:
    SHA:
      sh: git rev-parse --short HEAD
    BUILT_AT:
      sh: date -u +%Y-%m-%dT%H:%M:%SZ
  cmds:
    - go build
        -ldflags "-X __MODULE_PATH__/internal/buildinfo.Version={{.SHA}} -X __MODULE_PATH__/internal/buildinfo.BuiltAt={{.BUILT_AT}}"
        -o bin/__PROJECT_NAME__
        ./cmd/__PROJECT_NAME__

Test

Add internal/buildinfo/buildinfo_test.go covering:

  • Default values are "dev" and "unknown" when not injected
  • Handler returns 200 with valid JSON containing version and built_at keys

Branch

feat/version-endpoint from main

Out of scope

  • No auth on the endpoint (consistent with /healthz)
  • No structured HTML response — JSON only
  • No __PROJECT_NAME__ placeholder substitution needed (buildinfo package name is fixed)

Created via git-mcp on behalf of @mathiasbq

## Context Every service bootstrapped from this template should be able to report its own build identity at runtime. This is useful for health dashboards, deploy verification, and debugging — "which version is actually running right now?" ## Task Add a `/version` HTTP endpoint that returns build metadata injected at compile time via `-ldflags`. ## Acceptance criteria - `GET /version` returns `200 OK` with `Content-Type: application/json` - Response body: `{"version": "<git-sha>", "built_at": "<RFC3339 UTC timestamp>"}` - When built without ldflags (local dev), values fall back to `"dev"` and `"unknown"` respectively - The endpoint is registered in `main.go` alongside `/healthz` - `task build` injects the real values automatically via ldflags - A test covers at least the `dev` fallback case ## Implementation notes ### Package Add `internal/buildinfo/buildinfo.go`: ```go package buildinfo var ( Version = "dev" BuiltAt = "unknown" ) ``` ### Handler Register in `main.go`: ```go mux.HandleFunc("/version", buildinfo.Handler) ``` Where `Handler` is a simple `http.HandlerFunc` on the `buildinfo` package that marshals the two vars to JSON. ### Taskfile ldflags Update the `build` task in `Taskfile.yml`: ```yaml build: desc: Build the binary deps: [generate] vars: SHA: sh: git rev-parse --short HEAD BUILT_AT: sh: date -u +%Y-%m-%dT%H:%M:%SZ cmds: - go build -ldflags "-X __MODULE_PATH__/internal/buildinfo.Version={{.SHA}} -X __MODULE_PATH__/internal/buildinfo.BuiltAt={{.BUILT_AT}}" -o bin/__PROJECT_NAME__ ./cmd/__PROJECT_NAME__ ``` ### Test Add `internal/buildinfo/buildinfo_test.go` covering: - Default values are `"dev"` and `"unknown"` when not injected - Handler returns `200` with valid JSON containing `version` and `built_at` keys ## Branch `feat/version-endpoint` from `main` ## Out of scope - No auth on the endpoint (consistent with `/healthz`) - No structured HTML response — JSON only - No `__PROJECT_NAME__` placeholder substitution needed (buildinfo package name is fixed) --- _Created via git-mcp on behalf of @mathiasbq_
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mathias/template-go-web#1