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
import (
"encoding/json"
"github.com/adrg/sysfont"
"io"
"net/http"
"sort"
"strings"
"sync"
@ -12,6 +15,7 @@ import (
type preferencesService struct {
pref *storage2.PreferencesStorage
clientVersion string
}
var preferences *preferencesService
@ -22,6 +26,7 @@ func Preferences() *preferencesService {
oncePreferences.Do(func() {
preferences = &preferencesService{
pref: storage2.NewPreferences(),
clientVersion: "",
}
})
}
@ -80,3 +85,44 @@ func (p *preferencesService) GetFontList() (resp types.JSResp) {
resp.Success = true
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 { camelCase, clone, find, get, isEmpty, isObject, map, set, snakeCase, split } from 'lodash'
import {
CheckForUpdate,
GetFontList,
GetPreferences,
RestorePreferences,
@ -236,11 +237,20 @@ const usePreferencesStore = defineStore('preferences', {
if (manual) {
msgRef = $message.loading('Retrieving for new version', { duration: 0 })
}
let respObj = null
try {
const resp = await fetch('https://api.github.com/repos/tiny-craft/tiny-rdm/releases/latest')
if (resp.status === 200) {
respObj = await resp.json()
const { success, data = {} } = await CheckForUpdate()
if (success) {
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 {
if (msgRef != null) {
@ -248,17 +258,6 @@ const usePreferencesStore = defineStore('preferences', {
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,
themeOverrides,
}))
const { message, dialog } = createDiscreteApi(['message', 'dialog'], { configProviderProps })
const { message, dialog } = createDiscreteApi(['message', 'dialog'], {
configProviderProps,
messageProviderProps: {
placement: 'bottom-right',
},
})
window.$message = setupMessage(message)
window.$dialog = setupDialog(dialog)

View File

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