error-pages/internal/http/handlers/version/handler.go

23 lines
521 B
Go
Raw Normal View History

2024-06-22 20:05:11 +00:00
package version
import (
"encoding/json"
"net/http"
"strings"
)
// New creates a handler that returns the version of the service in JSON format.
func New(ver string) http.Handler {
var body, _ = json.Marshal(struct { //nolint:errchkjson
Version string `json:"version"`
}{
Version: strings.TrimSpace(ver),
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
})
}