-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use alby hub latest version from alby api
- Loading branch information
Showing
6 changed files
with
20 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,3 @@ | ||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/getAlby/hub/logger" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var Tag string = "" | ||
|
||
type githubRelease struct { | ||
TagName string `json:"tag_name"` | ||
} | ||
|
||
var latestRelease = "" | ||
var lastVersionCheck = time.Time{} | ||
|
||
func GetLatestReleaseTag() string { | ||
if latestRelease != "" && time.Since(lastVersionCheck) < 5*time.Minute { | ||
return latestRelease | ||
} | ||
url := "https://api.github.com/repos/getAlby/hub/releases" | ||
|
||
client := http.Client{ | ||
Timeout: time.Second * 10, | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
logger.Logger.WithError(err).WithFields(logrus.Fields{ | ||
"url": url, | ||
}).Error("Failed to create http request") | ||
return "" | ||
} | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
logger.Logger.WithError(err).WithFields(logrus.Fields{ | ||
"url": url, | ||
}).Error("Failed to send request") | ||
return "" | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
body, readErr := io.ReadAll(res.Body) | ||
if readErr != nil { | ||
logger.Logger.WithError(err).WithFields(logrus.Fields{ | ||
"url": url, | ||
}).Error("Failed to read response body") | ||
return "" | ||
} | ||
|
||
releases := []githubRelease{} | ||
jsonErr := json.Unmarshal(body, &releases) | ||
if jsonErr != nil { | ||
logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{ | ||
"url": url, | ||
}).Error("Failed to deserialize json") | ||
return "" | ||
} | ||
|
||
if len(releases) < 1 { | ||
logger.Logger.Error("no github releases found") | ||
return "" | ||
} | ||
|
||
latestRelease = releases[0].TagName | ||
|
||
logger.Logger.WithFields(logrus.Fields{ | ||
"latest": latestRelease, | ||
"current": Tag, | ||
}).Info("Found latest github release") | ||
|
||
lastVersionCheck = time.Now() | ||
|
||
return latestRelease | ||
} |