Mining/pkg/mining/version.go
snider fa3047a314 refactor: Add reliability fixes, centralized version fetching, and CHANGELOG
Reliability fixes:
- Fix race condition on uninitialized HTTP server in transport.go
- Add proper error logging for HTTP server startup errors
- Fix potential goroutine leak in process cleanup (xmrig_start.go)
- Propagate context to DB writes for proper timeout handling

Architecture improvements:
- Centralize GitHub version fetching via FetchLatestGitHubVersion()
- Add respondWithMiningError() helper for standardized API error responses
- Update XMRig and TTMiner to use centralized version fetcher

Documentation:
- Add CHANGELOG.md with release history
- Update FUTURE_IDEAS.md with demo GIF task

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 13:33:42 +00:00

59 lines
1.4 KiB
Go

package mining
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
// GetVersion returns the version of the application
func GetVersion() string {
return version
}
// GetCommit returns the git commit hash
func GetCommit() string {
return commit
}
// GetBuildDate returns the build date
func GetBuildDate() string {
return date
}
// GitHubRelease represents the structure of a GitHub release response.
type GitHubRelease struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
}
// FetchLatestGitHubVersion fetches the latest release version from a GitHub repository.
// It takes the repository owner and name (e.g., "xmrig", "xmrig") and returns the tag name.
func FetchLatestGitHubVersion(owner, repo string) (string, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo)
resp, err := getHTTPClient().Get(url)
if err != nil {
return "", fmt.Errorf("failed to fetch version: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
io.Copy(io.Discard, resp.Body) // Drain body to allow connection reuse
return "", fmt.Errorf("failed to get latest release: unexpected status code %d", resp.StatusCode)
}
var release GitHubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", fmt.Errorf("failed to decode release: %w", err)
}
return release.TagName, nil
}