refactor: move fetch latest version logic to backend

This commit is contained in:
tiny-craft 2023-08-26 02:40:30 +08:00
parent 312dc47039
commit 858adfaf0d
4 changed files with 71 additions and 19 deletions

View File

@ -1,7 +1,10 @@
package services package services
import ( import (
"encoding/json"
"github.com/adrg/sysfont" "github.com/adrg/sysfont"
"io"
"net/http"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -11,7 +14,8 @@ import (
) )
type preferencesService struct { type preferencesService struct {
pref *storage2.PreferencesStorage pref *storage2.PreferencesStorage
clientVersion string
} }
var preferences *preferencesService var preferences *preferencesService
@ -21,7 +25,8 @@ func Preferences() *preferencesService {
if preferences == nil { if preferences == nil {
oncePreferences.Do(func() { oncePreferences.Do(func() {
preferences = &preferencesService{ preferences = &preferencesService{
pref: storage2.NewPreferences(), pref: storage2.NewPreferences(),
clientVersion: "",
} }
}) })
} }
@ -80,3 +85,44 @@ func (p *preferencesService) GetFontList() (resp types.JSResp) {
resp.Success = true resp.Success = true
return return
} }
func (p *preferencesService) SetClientVersion(ver string) {
p.clientVersion = ver
}
type latestRelease struct {
Name string `json:"name"`
TagName string `json:"tag_name"`
Url string `json:"url"`
HtmlUrl string `json:"html_url"`
}
func (p *preferencesService) CheckForUpdate() (resp types.JSResp) {
// request latest version
res, err := http.Get("https://api.github.com/repos/tiny-craft/tiny-rdm/releases/latest")
if err != nil || res.StatusCode != http.StatusOK {
resp.Msg = "network error"
return
}
body, err := io.ReadAll(res.Body)
if err != nil {
resp.Msg = "invalid content"
return
}
var respObj latestRelease
err = json.Unmarshal(body, &respObj)
if err != nil {
resp.Msg = "invalid content"
return
}
// compare with current version
resp.Success = true
resp.Data = map[string]any{
"version": p.clientVersion,
"latest": respObj.TagName,
"page_url": respObj.HtmlUrl,
}
return
}

View File

@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
import { lang } from '@/langs/index.js' import { lang } from '@/langs/index.js'
import { camelCase, clone, find, get, isEmpty, isObject, map, set, snakeCase, split } from 'lodash' import { camelCase, clone, find, get, isEmpty, isObject, map, set, snakeCase, split } from 'lodash'
import { import {
CheckForUpdate,
GetFontList, GetFontList,
GetPreferences, GetPreferences,
RestorePreferences, RestorePreferences,
@ -236,11 +237,20 @@ const usePreferencesStore = defineStore('preferences', {
if (manual) { if (manual) {
msgRef = $message.loading('Retrieving for new version', { duration: 0 }) msgRef = $message.loading('Retrieving for new version', { duration: 0 })
} }
let respObj = null
try { try {
const resp = await fetch('https://api.github.com/repos/tiny-craft/tiny-rdm/releases/latest') const { success, data = {} } = await CheckForUpdate()
if (resp.status === 200) { if (success) {
respObj = await resp.json() const { version, latest, pageUrl } = data
if (latest > version) {
$dialog.warning(i18nGlobal.t('new_version_tip'), () => {
BrowserOpenURL(pageUrl)
})
return
}
}
if (manual) {
$message.info(i18nGlobal.t('no_update'))
} }
} finally { } finally {
if (msgRef != null) { if (msgRef != null) {
@ -248,17 +258,6 @@ const usePreferencesStore = defineStore('preferences', {
msgRef = null msgRef = null
} }
} }
// TODO: check current version is older then remote
if (respObj != null && !isEmpty(respObj['html_url'])) {
$dialog.warning(i18nGlobal.t('new_version_tip'), () => {
BrowserOpenURL(respObj['html_url'])
})
} else {
if (manual) {
$message.info(i18nGlobal.t('no_update'))
}
}
}, },
}, },
}) })

View File

@ -53,7 +53,12 @@ export async function setupDiscreteApi() {
theme: prefStore.isDark ? darkTheme : undefined, theme: prefStore.isDark ? darkTheme : undefined,
themeOverrides, themeOverrides,
})) }))
const { message, dialog } = createDiscreteApi(['message', 'dialog'], { configProviderProps }) const { message, dialog } = createDiscreteApi(['message', 'dialog'], {
configProviderProps,
messageProviderProps: {
placement: 'bottom-right',
},
})
window.$message = setupMessage(message) window.$message = setupMessage(message)
window.$dialog = setupDialog(dialog) window.$dialog = setupDialog(dialog)

View File

@ -19,6 +19,8 @@ var assets embed.FS
//go:embed build/appicon.png //go:embed build/appicon.png
var icon []byte var icon []byte
var version = "0.0.0"
func main() { func main() {
// Create an instance of the app structure // Create an instance of the app structure
app := NewApp() app := NewApp()
@ -66,7 +68,7 @@ func main() {
HideToolbarSeparator: true, HideToolbarSeparator: true,
}, },
About: &mac.AboutInfo{ About: &mac.AboutInfo{
Title: "Tiny RDM", Title: "Tiny RDM " + version,
Message: "A modern lightweight cross-platform Redis desktop client.\n\nCopyright © 2023", Message: "A modern lightweight cross-platform Redis desktop client.\n\nCopyright © 2023",
Icon: icon, Icon: icon,
}, },