File size: 1,965 Bytes
8d3471e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package version

import (
	"encoding/json"
	"net/http"
	"strings"
	"time"

	"ds2api/internal/version"
)

const latestReleaseAPI = "https://api.github.com/repos/CJackHwang/ds2api/releases/latest"

type latestReleasePayload struct {
	TagName     string `json:"tag_name"`
	HTMLURL     string `json:"html_url"`
	PublishedAt string `json:"published_at"`
}

func (h *Handler) getVersion(w http.ResponseWriter, _ *http.Request) {
	current, source := version.Current()
	resp := map[string]any{
		"success":         true,
		"current_version": current,
		"current_tag":     version.Tag(current),
		"source":          source,
		"checked_at":      time.Now().UTC().Format(time.RFC3339),
	}

	req, err := http.NewRequest(http.MethodGet, latestReleaseAPI, nil)
	if err != nil {
		resp["check_error"] = err.Error()
		writeJSON(w, http.StatusOK, resp)
		return
	}
	req.Header.Set("Accept", "application/vnd.github+json")
	req.Header.Set("User-Agent", "ds2api-version-check")

	client := &http.Client{Timeout: 4 * time.Second}
	r, err := client.Do(req)
	if err != nil {
		resp["check_error"] = err.Error()
		writeJSON(w, http.StatusOK, resp)
		return
	}
	defer func() { _ = r.Body.Close() }()
	if r.StatusCode < 200 || r.StatusCode >= 300 {
		resp["check_error"] = "github api status: " + r.Status
		writeJSON(w, http.StatusOK, resp)
		return
	}

	var data latestReleasePayload
	if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
		resp["check_error"] = err.Error()
		writeJSON(w, http.StatusOK, resp)
		return
	}

	latest := strings.TrimSpace(data.TagName)
	if latest == "" {
		resp["check_error"] = "missing latest tag"
		writeJSON(w, http.StatusOK, resp)
		return
	}
	latestVersion := strings.TrimPrefix(latest, "v")

	resp["latest_tag"] = latest
	resp["latest_version"] = latestVersion
	resp["release_url"] = data.HTMLURL
	resp["published_at"] = data.PublishedAt
	resp["has_update"] = version.Compare(current, latestVersion) < 0

	writeJSON(w, http.StatusOK, resp)
}