From 72e41ba4139e3a34e41415067efcf7f5159c356a Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 3 Jun 2023 15:51:08 -0500 Subject: [PATCH] Added version endpoint --- cmd/root.go | 6 +++++- cmd/web.go | 21 ++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 7b4f73b..12d05ad 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,10 @@ import ( "github.com/spf13/cobra" ) +const ( + Version string = "0.51.0" +) + var ( bind string cache bool @@ -74,5 +78,5 @@ func init() { }) rootCmd.SetVersionTemplate("roulette v{{.Version}}\n") - rootCmd.Version = "0.50.0" + rootCmd.Version = Version } diff --git a/cmd/web.go b/cmd/web.go index 93b56ad..5d8d789 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -411,7 +411,7 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error { ) } - w.WriteHeader(404) + w.WriteHeader(http.StatusNotFound) w.Header().Add("Content-Type", "text/html") var htmlBody strings.Builder @@ -440,7 +440,7 @@ func serverError() func(http.ResponseWriter, *http.Request, interface{}) { ) } - w.WriteHeader(500) + w.WriteHeader(http.StatusInternalServerError) w.Header().Add("Content-Type", "text/html") var htmlBody strings.Builder @@ -961,6 +961,15 @@ func serveFavicons() httprouter.Handle { } } +func serveVersion() httprouter.Handle { + return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + data := []byte(fmt.Sprintf("roulette v%s\n", Version)) + + w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data)))) + w.Write(data) + } +} + func ServePage(args []string) error { bindHost, err := net.LookupHost(bind) if err != nil { @@ -1046,13 +1055,15 @@ func ServePage(args []string) error { mux.GET("/json", serveDebugJson(args, index)) } + mux.GET("/", serveRoot(paths, Regexes, index)) + mux.GET("/favicons/*favicon", serveFavicons()) + mux.GET(ImagePrefix+"/*image", serveImage(paths, Regexes, index)) + mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats)) - mux.GET("/", serveRoot(paths, Regexes, index)) - - mux.GET(ImagePrefix+"/*image", serveImage(paths, Regexes, index)) + mux.GET("/version", serveVersion()) srv := &http.Server{ Addr: net.JoinHostPort(bind, strconv.FormatInt(int64(port), 10)),