Skip to content

Commit

Permalink
new version update send to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr committed Mar 2, 2025
1 parent 7ddb7ed commit e04882b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
3 changes: 3 additions & 0 deletions http/client_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down
23 changes: 18 additions & 5 deletions models/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import "encoding/json"
import (
"encoding/json"
)

type LogType int

Expand Down Expand Up @@ -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"`
}
26 changes: 16 additions & 10 deletions utils/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"runtime"

"github.com/logdyhq/logdy-core/models"
"github.com/sirupsen/logrus"
)

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e04882b

Please sign in to comment.