Skip to content

Commit

Permalink
version API (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored Nov 25, 2024
1 parent a7742f7 commit 1371922
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
3 changes: 2 additions & 1 deletion cmd/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ func runCmdHandler(cmd *cobra.Command, _ []string) error {
}

http.HandleFunc("/", faucet.ServeHTTP)
cmd.Printf("listening on :%d", port)
http.HandleFunc("/version", versionHTTP)
cmd.Printf("listening faucet on :%d", port)

return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
25 changes: 25 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package cmd
import (
"context"
"fmt"
"net/http"
"strings"
"time"

"github.com/ignite/cli/v28/ignite/pkg/xhttp"
"github.com/spf13/cobra"

"github.com/ignite/faucet/version"
Expand Down Expand Up @@ -65,3 +67,26 @@ func checkNewVersion(ctx context.Context) {

fmt.Printf("⬆️ Gex %[1]v is available! To upgrade: https://github.com/ignite/gex/releases/%[1]v", next)
}

func versionHTTP(w http.ResponseWriter, r *http.Request) {
info, err := version.GetInfo(r.Context())
if err != nil {
responseError(w, http.StatusBadRequest, err)
return
}
responseSuccess(w, info)
}

type errResponse struct {
Error string `json:"error,omitempty"`
}

func responseError(w http.ResponseWriter, code int, err error) {
_ = xhttp.ResponseJSON(w, code, errResponse{
Error: err.Error(),
})
}

func responseSuccess(w http.ResponseWriter, info version.Info) {
_ = xhttp.ResponseJSON(w, http.StatusOK, info)
}
18 changes: 9 additions & 9 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ const (
var Version = versionDev

type Info struct {
Version string
GoVersion string
BuildDate string
SourceHash string
OS string
Arch string
Uname string
CWD string
BuildFromSource bool
Version string `json:"version"`
GoVersion string `json:"go_version"`
BuildDate string `json:"build_date"`
SourceHash string `json:"source_hash"`
OS string `json:"os,omitempty"`
Arch string `json:"arch,omitempty"`
Uname string `json:"uname,omitempty"`
CWD string `json:"cwd,omitempty"`
BuildFromSource bool `json:"build_from_source,omitempty"`
}

// CheckNext checks whether there is a new version of Faucet.
Expand Down

0 comments on commit 1371922

Please sign in to comment.