From 87960917f61a24867a4384875febff708e7c2546 Mon Sep 17 00:00:00 2001 From: Robert Jacob Date: Sun, 21 Jun 2020 15:26:53 +0200 Subject: [PATCH] Add handler for version information --- main.go | 1 + version.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 version.go diff --git a/main.go b/main.go index 61a408d..a12cd62 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ func main() { prometheus.MustRegister(metrics) http.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{})) + http.Handle("/version", versionHandler(log)) http.Handle("/", http.RedirectHandler("/metrics", http.StatusFound)) log.Infof("Listen on %s...", cfg.Addr) diff --git a/version.go b/version.go new file mode 100644 index 0000000..75b65fd --- /dev/null +++ b/version.go @@ -0,0 +1,33 @@ +package main + +import ( + "encoding/json" + "net/http" + + "github.com/sirupsen/logrus" +) + +var ( + // Version contains the version as set during the build. + Version = "" + + // GitCommit contains the git commit hash set during the build. + GitCommit = "" +) + +func versionHandler(log logrus.FieldLogger) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + info := struct { + Version string `json:"version"` + Commit string `json:"commit"` + }{ + Version: Version, + Commit: GitCommit, + } + + w.Header().Add("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(info); err != nil { + log.Errorf("Error encoding version info: %s", err) + } + }) +}