Skip to content

Commit

Permalink
Added auto check for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gentee committed Jan 21, 2021
1 parent 91efa13 commit 9e681bb
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 192 deletions.
351 changes: 178 additions & 173 deletions assets.go

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.

package main

import (
"github.com/kataras/golog"
"github.com/robfig/cron/v3"
)

var (
cronJobs = cron.New() //cron.New(cron.WithSeconds())
)

func RunCron() {
if _, err := cronJobs.AddFunc(`0 * * * *`, AutoCheckUpdate); err != nil {
golog.Error(err)
}
cronJobs.Start()
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/labstack/echo/v4 v4.1.17
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/sergi/go-diff v1.1.0 // indirect
github.com/stretchr/testify v1.6.1 // indirect
github.com/yuin/goldmark v1.3.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func main() {
LoadNotifications()
InitScripts()
CreateSysTray()
RunCron()
e = RunServer(WebSettings{
Port: cfg.HTTP.Port,
Open: cfg.HTTP.Open,
Expand Down
38 changes: 26 additions & 12 deletions notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,16 @@ func GetNewVersion(lang string) (ret string) {
}
}
ret = fmt.Sprintf(`%s: <span style="padding: 4px 8px;
font-weight: bold;background-color: #ffff00">%s</span><br>
<a style="margin-right: 2rem;" href="%s" target="_blank">%s</a>
<a href="%s" target="_blank">%s</a>`, Lang(lid, `newver`),
font-weight: bold;background-color: #ffff00">%s</span><br><a style="margin-right: 2rem;" href="%s" target="_blank">%s</a><a href="%s" target="_blank">%s</a>`, Lang(lid, `newver`),
nfyData.Update.Version, appInfo.Homepage+pref+nfyData.Update.Changelog,
Lang(lid, `changelog`),
appInfo.Homepage+pref+nfyData.Update.Downloads, Lang(lid, `downloads`))
}
return
}

func CheckUpdates(manual bool) error {
//resp, err := http.Get(appInfo.Homepage + `latest`)
resp, err := http.Get(`http://localhost:3000/latest`)
func CheckUpdates() error {
resp, err := http.Get(appInfo.Homepage + `latest`)
if err != nil {
return err
}
Expand All @@ -297,16 +294,33 @@ func CheckUpdates(manual bool) error {
return saveNotifications()
}

func AutoVerUpdate() error {
if err := CheckUpdates(false); err != nil {
return err
func AutoCheckUpdate() {
var (
update bool
)
now := time.Now()
switch storage.Settings.AutoUpdate {
case `daily`:
update = now.After(nfyData.Update.LastChecked.Add(time.Hour * 24))
case `weekly`:
update = now.After(nfyData.Update.LastChecked.Add(time.Hour * 24 * 7))
case `mothly`:
update = now.After(nfyData.Update.LastChecked.Add(time.Hour * 24 * 30))
}
if !update {
return
}
// lang := RootUserSettings().Lang
return nil
if err := CheckUpdates(); err != nil {
return
}
if nfy := GetNewVersion(RootUserSettings().Lang); len(nfy) > 0 {
NewNotification(&Notification{Text: nfy})
}
return
}

func latestVerHandle(c echo.Context) error {
if err := CheckUpdates(true); err != nil {
if err := CheckUpdates(); err != nil {
return jsonError(c, err)
}
return c.JSON(http.StatusOK, LatestResponse{
Expand Down
6 changes: 4 additions & 2 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Settings struct {
NotAskPassword bool `json:"notaskpassword"`
Title string `json:"title"`
HideTray bool `json:"hidetray"`
AutoUpdate string `json:"autoupdate"`
}

// Storage contains all application data
Expand All @@ -43,8 +44,9 @@ type Storage struct {
var (
storage = Storage{
Settings: Settings{
LogLevel: script.LOG_INFO,
Constants: make(map[string]string),
LogLevel: script.LOG_INFO,
Constants: make(map[string]string),
AutoUpdate: `weekly`,
},
Users: make(map[uint32]*User),
Scripts: make(map[string]*Script),
Expand Down
5 changes: 0 additions & 5 deletions timer.go

This file was deleted.

0 comments on commit 9e681bb

Please sign in to comment.