diff --git a/bootstrap/app.go b/bootstrap/app.go index a0a7fe6f1dace703f5cd7eef80ec310d2a92fc46..616c8026c2760d738897f6ac1f007142a7ea7f3d 100644 --- a/bootstrap/app.go +++ b/bootstrap/app.go @@ -1,8 +1,12 @@ package bootstrap import ( + "encoding/json" "fmt" "github.com/HFO4/cloudreve/pkg/conf" + "github.com/HFO4/cloudreve/pkg/request" + "github.com/HFO4/cloudreve/pkg/util" + "github.com/hashicorp/go-version" ) // InitApplication 初始化应用常量 @@ -18,4 +22,34 @@ func InitApplication() { ================================================ `) + go CheckUpdate() +} + +type GitHubRelease struct { + URL string `json:"html_url"` + Name string `json:"name"` + Tag string `json:"tag_name"` +} + +// CheckUpdate 检查更新 +func CheckUpdate() { + client := request.HTTPClient{} + res, err := client.Request("GET", "https://api.github.com/repos/cloudreve/cloudreve/releases", nil).GetResponse() + if err != nil { + util.Log().Warning("更新检查失败, %s", err) + } + + var list []GitHubRelease + if err := json.Unmarshal([]byte(res), &list); err != nil { + util.Log().Warning("更新检查失败, %s", err) + } + + if len(list) > 0 { + present, err1 := version.NewVersion(conf.BackendVersion) + latest, err2 := version.NewVersion(list[0].Tag) + if err1 == nil && err2 == nil && latest.GreaterThan(present) { + util.Log().Info("有新的版本 [%s] 可用,下载:%s", list[0].Name, list[0].URL) + } + } + } diff --git a/go.mod b/go.mod index f536587987d0e675ba5978f898c9d1701431cddd..fe0cdf51f9f026e4c9d85f491ce5104c8c34b264 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/gomodule/redigo v2.0.0+incompatible github.com/google/go-querystring v1.0.0 github.com/gorilla/websocket v1.4.1 + github.com/hashicorp/go-version v1.2.0 github.com/jinzhu/gorm v1.9.11 github.com/juju/ratelimit v1.0.1 github.com/mattn/go-colorable v0.1.4 // indirect diff --git a/go.sum b/go.sum index 85865dc5218054c2fa3a92425a64056b36faf0d6..fa31f3f81a6652b8d1a2c0a2414ecbfc9d41c750 100644 --- a/go.sum +++ b/go.sum @@ -116,6 +116,8 @@ github.com/gorilla/sessions v1.1.3 h1:uXoZdcdA5XdXF3QzuSlheVRUvjl+1rKY7zBXL68L9R github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= +github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jinzhu/gorm v1.9.11 h1:gaHGvE+UnWGlbWG4Y3FUwY1EcZ5n6S9WtqBA/uySMLE=