- Updated file permissions in certificate_service_test.go and log_service_test.go to use octal notation. - Added a new doc.go file to document the services package. - Enhanced error handling in docker_service.go, log_service.go, notification_service.go, proxyhost_service.go, remoteserver_service.go, update_service.go, and uptime_service.go by logging errors when closing resources. - Improved log_service.go to simplify log file processing and deduplication. - Introduced CRUD tests for notification templates in notification_service_template_test.go. - Removed the obsolete python_compile_check.sh script. - Updated notification_service.go to improve template management functions. - Added tests for uptime service notifications in uptime_service_notification_test.go.
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package services
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/Wikid82/charon/backend/internal/logger"
|
|
"github.com/Wikid82/charon/backend/internal/version"
|
|
)
|
|
|
|
type UpdateService struct {
|
|
currentVersion string
|
|
repoOwner string
|
|
repoName string
|
|
lastCheck time.Time
|
|
cachedResult *UpdateInfo
|
|
apiURL string // For testing
|
|
}
|
|
|
|
type UpdateInfo struct {
|
|
Available bool `json:"available"`
|
|
LatestVersion string `json:"latest_version"`
|
|
ChangelogURL string `json:"changelog_url"`
|
|
}
|
|
|
|
type githubRelease struct {
|
|
TagName string `json:"tag_name"`
|
|
HTMLURL string `json:"html_url"`
|
|
}
|
|
|
|
func NewUpdateService() *UpdateService {
|
|
return &UpdateService{
|
|
currentVersion: version.Version,
|
|
repoOwner: "Wikid82",
|
|
repoName: "charon",
|
|
apiURL: "https://api.github.com/repos/Wikid82/charon/releases/latest",
|
|
}
|
|
}
|
|
|
|
// SetAPIURL sets the GitHub API URL for testing.
|
|
func (s *UpdateService) SetAPIURL(url string) {
|
|
s.apiURL = url
|
|
}
|
|
|
|
// SetCurrentVersion sets the current version for testing.
|
|
func (s *UpdateService) SetCurrentVersion(v string) {
|
|
s.currentVersion = v
|
|
}
|
|
|
|
// ClearCache clears the update cache for testing.
|
|
func (s *UpdateService) ClearCache() {
|
|
s.cachedResult = nil
|
|
s.lastCheck = time.Time{}
|
|
}
|
|
|
|
func (s *UpdateService) CheckForUpdates() (*UpdateInfo, error) {
|
|
// Cache for 1 hour
|
|
if s.cachedResult != nil && time.Since(s.lastCheck) < 1*time.Hour {
|
|
return s.cachedResult, nil
|
|
}
|
|
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
|
|
req, err := http.NewRequest("GET", s.apiURL, http.NoBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("User-Agent", "Charon-Update-Checker")
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
if err := resp.Body.Close(); err != nil {
|
|
logger.Log().WithError(err).Warn("failed to close update service response body")
|
|
}
|
|
}()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
// If rate limited or not found, just return no update available
|
|
return &UpdateInfo{Available: false}, nil
|
|
}
|
|
|
|
var release githubRelease
|
|
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Simple string comparison for now.
|
|
// In production, use a semver library.
|
|
// Assuming tags are "v0.1.0" and version is "0.1.0"
|
|
latest := release.TagName
|
|
if latest != "" && latest[0] == 'v' {
|
|
latest = latest[1:]
|
|
}
|
|
|
|
info := &UpdateInfo{
|
|
Available: latest != s.currentVersion && latest != "",
|
|
LatestVersion: release.TagName,
|
|
ChangelogURL: release.HTMLURL,
|
|
}
|
|
|
|
s.cachedResult = info
|
|
s.lastCheck = time.Now()
|
|
|
|
return info, nil
|
|
}
|