Skip to content

Commit

Permalink
feat: 新增启动时的更新检查
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrazg committed Jul 30, 2024
1 parent 00519c7 commit 2cd6657
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 24 deletions.
6 changes: 4 additions & 2 deletions constant/url.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package constant

var (
BaseUrl = "https://api.xiaoyuzhoufm.com"
const (
BaseUrl = "https://api.xiaoyuzhoufm.com"
UpgradeUrl = "https://api.github.com/repos/ultrazg/xyz/releases/latest"
ReleaseUrl = "https://github.com/ultrazg/xyz/releases"
)
7 changes: 7 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ func Start() (err error) {

utils.P(port)

go func() {
err := utils.CheckUpgrade()
if err != nil {
return
}
}()

gin.SetMode(gin.ReleaseMode)
engine := gin.New()

Expand Down
112 changes: 112 additions & 0 deletions utils/checkupgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package utils

import (
"encoding/json"
"fmt"
"github.com/ultrazg/xyz/constant"
"io"
"net/http"
"strings"
"time"
)

type AutoGenerated struct {
URL string `json:"url"`
AssetsURL string `json:"assets_url"`
UploadURL string `json:"upload_url"`
HTMLURL string `json:"html_url"`
ID int `json:"id"`
Author Author `json:"author"`
NodeID string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Assets []Assets `json:"assets"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Body string `json:"body"`
}
type Author struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}
type Uploader struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}
type Assets struct {
URL string `json:"url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Label any `json:"label"`
Uploader Uploader `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int `json:"size"`
DownloadCount int `json:"download_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BrowserDownloadURL string `json:"browser_download_url"`
}

func CheckUpgrade() error {
response, _, err := Request(constant.UpgradeUrl, http.MethodGet, nil, nil)
if err != nil {
return err
}

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

var data *AutoGenerated
if err := json.Unmarshal(body, &data); err != nil {
return err
}

remoteVersion := strings.TrimPrefix(data.TagName, "v")

if constant.Version != remoteVersion {
fmt.Printf("\r\n✨发现新版本\r\n当前版本:%s\r\n最新版本:%s\r\n更新内容:\n%s\r\n%s", constant.Version, remoteVersion, data.Body, constant.ReleaseUrl)
}

return nil
}
46 changes: 24 additions & 22 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,37 @@ func Request(url, method string, body map[string]any, headers map[string]string)
return nil, 0, fmt.Errorf("failed to create request: %v", err)
}

// 请求头
for key, value := range headers {
req.Header.Set(key, value)
}
if headers != nil {
// 请求头
for key, value := range headers {
req.Header.Set(key, value)
}

fmt.Println("=========[REQUEST INFO]=========")
fmt.Println("Request Url:", url)
fmt.Println("Request Method:", method)
fmt.Println("=========[REQUEST INFO]=========")
fmt.Println("Request Url:", url)
fmt.Println("Request Method:", method)

if req.Body != nil {
requestBody := req.Body
var requestBodyBytes []byte
if requestBody != nil {
requestBodyBytes, _ = io.ReadAll(requestBody)
}
if req.Body != nil {
requestBody := req.Body
var requestBodyBytes []byte
if requestBody != nil {
requestBodyBytes, _ = io.ReadAll(requestBody)
}

fmt.Println("Request Body:", string(requestBodyBytes))
fmt.Println("Request Body:", string(requestBodyBytes))

req.Body = io.NopCloser(bytes.NewBuffer(requestBodyBytes))
}
req.Body = io.NopCloser(bytes.NewBuffer(requestBodyBytes))
}

fmt.Println("Request Headers:")
for key, values := range req.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
fmt.Println("Request Headers:")
for key, values := range req.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
}

fmt.Println("========================")
fmt.Println("========================")
}

var resp *http.Response
var retryErr error
Expand Down

0 comments on commit 2cd6657

Please sign in to comment.