Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: version API #9

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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