2021-09-29 15:38:50 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-07-03 14:12:13 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
// New creates a handler that returns the version of the service in JSON format.
|
|
|
|
func New(ver string) fasthttp.RequestHandler {
|
|
|
|
var body, _ = json.Marshal(struct { //nolint:errchkjson
|
|
|
|
Version string `json:"version"`
|
|
|
|
}{
|
|
|
|
Version: strings.TrimSpace(ver),
|
|
|
|
})
|
|
|
|
|
|
|
|
var notAllowed = http.StatusText(http.StatusMethodNotAllowed) + "\n"
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
2024-07-03 14:12:13 +00:00
|
|
|
switch string(ctx.Method()) {
|
|
|
|
case fasthttp.MethodGet:
|
|
|
|
ctx.SetContentType("application/json; charset=utf-8")
|
|
|
|
ctx.SetStatusCode(http.StatusOK)
|
|
|
|
_, _ = ctx.Write(body)
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
case fasthttp.MethodHead:
|
|
|
|
ctx.SetStatusCode(http.StatusOK)
|
|
|
|
|
|
|
|
default:
|
|
|
|
ctx.Error(notAllowed, http.StatusMethodNotAllowed)
|
|
|
|
}
|
2021-09-29 15:38:50 +00:00
|
|
|
}
|
|
|
|
}
|