2020-08-08 19:29:47 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-11-16 16:27:00 +08:00
|
|
|
"io"
|
2020-08-08 19:29:47 +08:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
)
|
|
|
|
|
2024-07-06 22:34:16 +08:00
|
|
|
const updateCheckURL = "https://update.listmonk.app/update.json"
|
2020-08-08 19:29:47 +08:00
|
|
|
|
|
|
|
type AppUpdate struct {
|
2024-07-06 22:34:16 +08:00
|
|
|
Update struct {
|
|
|
|
ReleaseVersion string `json:"release_version"`
|
|
|
|
ReleaseDate string `json:"release_date"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
|
|
|
|
// This is computed and set locally based on the local version.
|
|
|
|
IsNew bool `json:"is_new"`
|
|
|
|
} `json:"update"`
|
|
|
|
Messages []struct {
|
|
|
|
Date string `json:"date"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Priority string `json:"priority"`
|
|
|
|
} `json:"messages"`
|
2020-08-08 19:29:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var reSemver = regexp.MustCompile(`-(.*)`)
|
|
|
|
|
|
|
|
// checkUpdates is a blocking function that checks for updates to the app
|
|
|
|
// at the given intervals. On detecting a new update (new semver), it
|
|
|
|
// sets the global update status that renders a prompt on the UI.
|
|
|
|
func checkUpdates(curVersion string, interval time.Duration, app *App) {
|
|
|
|
// Strip -* suffix.
|
|
|
|
curVersion = reSemver.ReplaceAllString(curVersion, "")
|
2021-01-27 15:25:53 +08:00
|
|
|
|
2024-07-18 20:51:30 +08:00
|
|
|
fnCheck := func() {
|
2020-08-08 19:29:47 +08:00
|
|
|
resp, err := http.Get(updateCheckURL)
|
|
|
|
if err != nil {
|
|
|
|
app.log.Printf("error checking for remote update: %v", err)
|
2024-07-18 20:51:30 +08:00
|
|
|
return
|
2020-08-08 19:29:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
app.log.Printf("non 200 response on remote update check: %d", resp.StatusCode)
|
2024-07-18 20:51:30 +08:00
|
|
|
return
|
2020-08-08 19:29:47 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 16:27:00 +08:00
|
|
|
b, err := io.ReadAll(resp.Body)
|
2020-08-08 19:29:47 +08:00
|
|
|
if err != nil {
|
|
|
|
app.log.Printf("error reading remote update payload: %v", err)
|
2024-07-18 20:51:30 +08:00
|
|
|
return
|
2020-08-08 19:29:47 +08:00
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2024-07-06 22:34:16 +08:00
|
|
|
var out AppUpdate
|
|
|
|
if err := json.Unmarshal(b, &out); err != nil {
|
2020-08-08 19:29:47 +08:00
|
|
|
app.log.Printf("error unmarshalling remote update payload: %v", err)
|
2024-07-18 20:51:30 +08:00
|
|
|
return
|
2020-08-08 19:29:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// There is an update. Set it on the global app state.
|
2024-07-06 22:34:16 +08:00
|
|
|
if semver.IsValid(out.Update.ReleaseVersion) {
|
|
|
|
v := reSemver.ReplaceAllString(out.Update.ReleaseVersion, "")
|
2020-08-08 19:29:47 +08:00
|
|
|
if semver.Compare(v, curVersion) > 0 {
|
2024-07-06 22:34:16 +08:00
|
|
|
out.Update.IsNew = true
|
|
|
|
app.log.Printf("new update %s found", out.Update.ReleaseVersion)
|
2020-08-08 19:29:47 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-06 22:34:16 +08:00
|
|
|
|
|
|
|
app.Lock()
|
|
|
|
app.update = &out
|
|
|
|
app.Unlock()
|
2024-07-18 20:51:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Give a 15 minute buffer after app start in case the admin wants to disable
|
|
|
|
// update checks entirely and not make a request to upstream.
|
|
|
|
time.Sleep(time.Minute * 15)
|
|
|
|
fnCheck()
|
|
|
|
|
|
|
|
// Thereafter, check every $interval.
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
2024-07-06 22:34:16 +08:00
|
|
|
|
2024-07-18 20:51:30 +08:00
|
|
|
for range ticker.C {
|
|
|
|
fnCheck()
|
2020-08-08 19:29:47 +08:00
|
|
|
}
|
|
|
|
}
|