package version

import (
	"encoding/json"
	"fmt"
	"net/http"
	"runtime"
)

// Version information will be injected when compile stage.
// see: build scripts
var (
	GitHash   = "None"
	GitBranch = "None"
	goVersion = runtime.Version()
)

func Info() string {
	return fmt.Sprintf(`Git Commit Hash: %s
Git Branch: %s
Go Version: %s`, GitHash, GitBranch, goVersion)
}

type versionInfo struct {
	Hash      string `json:"hash"`
	Branch    string `json:"branch"`
	GoVersion string `json:"goVersion"`
}

// Handler outputs the current server version information.
func Handler(w http.ResponseWriter, _ *http.Request) {
	data, _ := json.Marshal(versionInfo{
		Hash:      GitHash,
		Branch:    GitBranch,
		GoVersion: goVersion,
	})
	_, _ = w.Write(data)
}
