feat: 自动更新

This commit is contained in:
Akizon77
2025-04-29 21:26:50 +08:00
parent 5f882d7042
commit d4b66e86f9
4 changed files with 142 additions and 1 deletions

26
main.go
View File

@@ -10,10 +10,15 @@ import (
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/komari-monitor/komari-agent/config"
"github.com/komari-monitor/komari-agent/monitoring"
"github.com/komari-monitor/komari-agent/update"
)
"github.com/gorilla/websocket"
var (
currentVersion = "v0.0.2"
repo = "komari-monitor/komari-agent"
)
func main() {
@@ -43,6 +48,14 @@ func main() {
ticker := time.NewTicker(time.Duration(localConfig.Interval * float64(time.Second)))
defer ticker.Stop()
// Check for updates
go func() {
ticker_ := time.NewTicker(time.Duration(6) * time.Hour)
for range ticker_.C {
update_komari()
}
}()
for range ticker.C {
// If no connection, attempt to connect
if conn == nil {
@@ -83,6 +96,17 @@ func main() {
}
}
func update_komari() {
// 初始化 Updater
updater := update.NewUpdater(currentVersion, repo)
// 检查并更新
err := updater.CheckAndUpdate()
if err != nil {
log.Fatalf("更新失败: %v", err)
}
}
// connectWebSocket attempts to establish a WebSocket connection and upload basic info
func connectWebSocket(websocketEndpoint, endpoint, token string) (*websocket.Conn, error) {
dialer := &websocket.Dialer{