Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Updated version check to use POST only. Updated version check schema.…
Browse files Browse the repository at this point in the history
… Now generating a local UUID to submit to version check as a unique ID that persists in a file.
  • Loading branch information
Fletcher Haynes authored and fhaynes committed Feb 1, 2023
1 parent 20429bb commit 6933196
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,13 @@ func (s *Server) Open() error {
// Do version check in. This is in a goroutine so that we don't block server startup if the server endpoint is down/having issues.
go func() {
s.logger.Printf("Beginning featurebase version check-in")
vc := VersionChecker{URL: "https://analytics.featurebase.com/v2/featurebase/version"}
vc := VersionChecker{URL: "https://analytics.featurebase.com/v2/featurebase/metrics"}
resp, err := vc.CheckIn()
if err != nil {
s.logger.Errorf("doing version checkin. Error was %s", err)
return
}
s.logger.Printf("Version check-in complete. Latest version is %s", resp.Information.Version)
s.logger.Printf("Version check-in complete. Latest version is %s", resp.Version)
}()

// Start DisCo.
Expand Down
74 changes: 65 additions & 9 deletions verchk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"encoding/json"
"io"
"net/http"
"os"
"strings"

"github.com/google/uuid"
)

type VersionChecker struct {
Expand All @@ -18,26 +22,36 @@ func NewVersionChecker(endpoint string) *VersionChecker {
return &v
}

func (v *VersionChecker) CheckIn() (*Response, error) {
func (v *VersionChecker) CheckIn() (*VerCheckResponse, error) {

id, err := v.WriteClientUUID()
if err != nil {
return nil, err
}

body := make(map[string]string, 2)
body["entry_type"] = "user"
body["version"] = VersionInfo(true)
body["version"] = Version
body["client_id"] = id

req, err := json.Marshal(body)
if err != nil {
return nil, err
}
_, err = http.Post(v.URL, "application/json", bytes.NewBuffer(req))

wReq := bytes.NewReader(req)

if err != nil {
return nil, err
}

var json_resp Response
r, err := http.Get(v.URL)
var json_resp VerCheckResponse
r, err := http.Post(v.URL, "application/json", wReq)
if err != nil {
return nil, err
}
data, err := io.ReadAll(r.Body)

if err != nil {
return nil, err
}
Expand All @@ -51,10 +65,52 @@ func (v *VersionChecker) CheckIn() (*Response, error) {

}

type Response struct {
Information InfoSubResponse `json:"info"`
func (v *VersionChecker) GenerateClientUUID() (string, error) {
clientUUID := uuid.New()
cleanedUUID := strings.Replace(clientUUID.String(), "-", "", -1)
return cleanedUUID, nil
}

func (v *VersionChecker) WriteClientUUID() (string, error) {
filename := ".client_id.txt"
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
fh, err := os.Create(filename)
if err != nil {
return "", err
}
defer fh.Close()
id, err := v.GenerateClientUUID()
if err != nil {
return "", err
}

_, err = fh.WriteString(id)
if err != nil {
return "", err
}

return "", err
} else {
return "", err
}
}

fh, err := os.Open(filename)
if err != nil {
return "", err
}
defer fh.Close()
buf, err := os.ReadFile(filename)

if err != nil {
return "", err
}
return string(buf), nil

}

type InfoSubResponse struct {
Version string `json:"version"`
type VerCheckResponse struct {
Version string `json:"latest_version"`
}

0 comments on commit 6933196

Please sign in to comment.