github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/bootstrap/app.go (about)

     1  package bootstrap
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/cloudreve/Cloudreve/v3/pkg/conf"
     8  	"github.com/cloudreve/Cloudreve/v3/pkg/request"
     9  	"github.com/cloudreve/Cloudreve/v3/pkg/util"
    10  	"github.com/hashicorp/go-version"
    11  )
    12  
    13  // InitApplication 初始化应用常量
    14  func InitApplication() {
    15  	fmt.Print(`
    16     ___ _                 _                    
    17    / __\ | ___  _   _  __| |_ __ _____   _____ 
    18   / /  | |/ _ \| | | |/ _  | '__/ _ \ \ / / _ \	
    19  / /___| | (_) | |_| | (_| | | |  __/\ V /  __/
    20  \____/|_|\___/ \__,_|\__,_|_|  \___| \_/ \___|
    21  
    22     V` + conf.BackendVersion + `  Commit #` + conf.LastCommit + `  Pro=` + conf.IsPro + `
    23  ================================================
    24  
    25  `)
    26  	go CheckUpdate()
    27  }
    28  
    29  type GitHubRelease struct {
    30  	URL  string `json:"html_url"`
    31  	Name string `json:"name"`
    32  	Tag  string `json:"tag_name"`
    33  }
    34  
    35  // CheckUpdate 检查更新
    36  func CheckUpdate() {
    37  	client := request.NewClient()
    38  	res, err := client.Request("GET", "https://api.github.com/repos/cloudreve/cloudreve/releases", nil).GetResponse()
    39  	if err != nil {
    40  		util.Log().Warning("更新检查失败, %s", err)
    41  		return
    42  	}
    43  
    44  	var list []GitHubRelease
    45  	if err := json.Unmarshal([]byte(res), &list); err != nil {
    46  		util.Log().Warning("更新检查失败, %s", err)
    47  		return
    48  	}
    49  
    50  	if len(list) > 0 {
    51  		present, err1 := version.NewVersion(conf.BackendVersion)
    52  		latest, err2 := version.NewVersion(list[0].Tag)
    53  		if err1 == nil && err2 == nil && latest.GreaterThan(present) {
    54  			util.Log().Info("有新的版本 [%s] 可用,下载:%s", list[0].Name, list[0].URL)
    55  		}
    56  	}
    57  
    58  }