diff --git a/http/client_handlers.go b/http/client_handlers.go index d5c0f6d..c7c8245 100644 --- a/http/client_handlers.go +++ b/http/client_handlers.go @@ -47,6 +47,8 @@ func handleStatus(config *Config) func(w http.ResponseWriter, r *http.Request) { configStr = utils.LoadFile(config.ConfigFilePath) } + newVersion, _ := utils.IsNewVersionAvailable() + initMsg, _ := json.Marshal(models.InitMessage{ BaseMessage: models.BaseMessage{ MessageType: models.MessageTypeInit, @@ -55,6 +57,7 @@ func handleStatus(config *Config) func(w http.ResponseWriter, r *http.Request) { AuthRequired: config.UiPass != "", ConfigStr: configStr, ApiPrefix: config.HttpPathPrefix, + UpdateVersion: newVersion, }) w.Header().Add("content-type", "application/json") diff --git a/models/message.go b/models/message.go index a1bf10e..eb3fcd6 100644 --- a/models/message.go +++ b/models/message.go @@ -1,6 +1,8 @@ package models -import "encoding/json" +import ( + "encoding/json" +) type LogType int @@ -51,10 +53,21 @@ type ClientMsgStatus struct { Stats Stats `json:"stats"` } +type LogdyVersionUpdateResponse struct { + Checked bool `json:"checked"` // whether the new version was checked + LocalVersion string `json:"local_version"` + CurrentVersion string `json:"current_version"` + CurrentVersionPublishedAt string `json:"current_version_published"` + DownloadLink string `json:"download_link"` + BlogLink string `json:"blog_link"` + Excerpt string `json:"excerpt"` +} + type InitMessage struct { BaseMessage - AnalyticsEnabled bool `json:"analyticsEnabled"` - AuthRequired bool `json:"authRequired"` - ConfigStr string `json:"configStr"` - ApiPrefix string `json:"apiPrefix"` + AnalyticsEnabled bool `json:"analyticsEnabled"` + AuthRequired bool `json:"authRequired"` + ConfigStr string `json:"configStr"` + ApiPrefix string `json:"apiPrefix"` + UpdateVersion LogdyVersionUpdateResponse `json:"updateVersion"` } diff --git a/utils/update.go b/utils/update.go index 4d1fb28..c9a035f 100644 --- a/utils/update.go +++ b/utils/update.go @@ -6,6 +6,7 @@ import ( "net/http" "runtime" + "github.com/logdyhq/logdy-core/models" "github.com/sirupsen/logrus" ) @@ -18,16 +19,8 @@ import ( }; */ -type UpdateResponse struct { - CurrentVersion string `json:"current_version"` - CurrentVersionPublishedAt string `json:"current_version_published"` - DownloadLink string `json:"download_link"` - BlogLink string `json:"blog_link"` - Excerpt string `json:"excerpt"` -} - -func checkUpdates(version string) (UpdateResponse, error) { - update := UpdateResponse{} +func checkUpdates(version string) (models.LogdyVersionUpdateResponse, error) { + update := models.LogdyVersionUpdateResponse{} resp, err := http.Get("https://update.logdy.dev?version=" + version) if err != nil { return update, err @@ -59,7 +52,16 @@ func init() { } } +var UpdateVersionChecked = false +var UpdateVersion models.LogdyVersionUpdateResponse + +func IsNewVersionAvailable() (models.LogdyVersionUpdateResponse, bool) { + return UpdateVersion, UpdateVersionChecked +} + func CheckUpdatesAndPrintInfo(currentVersion string) { + UpdateVersionChecked = true + update, err := checkUpdates(currentVersion) if err != nil { @@ -99,6 +101,10 @@ func CheckUpdatesAndPrintInfo(currentVersion string) { return } + UpdateVersion = update + UpdateVersion.Checked = true + UpdateVersion.LocalVersion = currentVersion + Logger.WithFields(logrus.Fields{ "response": update, "current_version": currentVersion,