提交 76ac2cd0 编写于 作者: N ning

refactor version api

上级 859876e3
......@@ -20,6 +20,7 @@ import (
"github.com/ccfos/nightingale/v6/pkg/httpx"
"github.com/ccfos/nightingale/v6/pkg/i18nx"
"github.com/ccfos/nightingale/v6/pkg/logx"
"github.com/ccfos/nightingale/v6/pkg/version"
"github.com/ccfos/nightingale/v6/prom"
"github.com/ccfos/nightingale/v6/pushgw/idents"
"github.com/ccfos/nightingale/v6/pushgw/writer"
......@@ -85,6 +86,7 @@ func Initialize(configDir string, cryptoKey string) (func(), error) {
writers := writer.NewWriters(config.Pushgw)
httpx.InitRSAConfig(&config.HTTP.RSA)
go version.GetGithubVersion()
alertrtRouter := alertrt.New(config.HTTP, config.Alert, alertMuteCache, targetCache, busiGroupCache, alertStats, ctx, externalProcessors)
centerRouter := centerrt.New(config.HTTP, config.Center, cconf.Operations, dsCache, notifyConfigCache, promClients, redis, sso, ctx, metas, idents, targetCache, userCache, userGroupCache)
......
......@@ -26,7 +26,6 @@ import (
"github.com/rakyll/statik/fs"
"github.com/toolkits/pkg/ginx"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/net/httplib"
"github.com/toolkits/pkg/runner"
)
......@@ -388,14 +387,7 @@ func (rt *Router) Config(r *gin.Engine) {
v = version.Version[:lastIndex]
}
req := httplib.Get("https://api.github.com/repos/ccfos/nightingale/releases/latest")
var release GithubRelease
err := req.ToJSON(&release)
if err != nil {
ginx.NewRender(c).Data(gin.H{"version": v, "github_verison": ""}, nil)
} else {
ginx.NewRender(c).Data(gin.H{"version": v, "github_verison": release.TagName}, nil)
}
ginx.NewRender(c).Data(gin.H{"version": v, "github_verison": version.GithubVersion.Load().(string)}, nil)
})
if rt.HTTP.APIForService.Enable {
......@@ -502,7 +494,3 @@ func Dangerous(c *gin.Context, v interface{}, code ...int) {
c.JSON(http.StatusOK, gin.H{"error": t.Error()})
}
}
type GithubRelease struct {
TagName string `json:"tag_name"`
}
package version
import "github.com/hashicorp/go-version"
import (
"sync/atomic"
"time"
"github.com/hashicorp/go-version"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/net/httplib"
)
var Version = "unknown"
var GithubVersion atomic.Value
func CompareVersion(v1, v2 string) (int, error) {
version1, err := version.NewVersion(v1)
......@@ -22,3 +30,21 @@ func CompareVersion(v1, v2 string) (int, error) {
}
return 0, nil
}
func GetGithubVersion() {
for {
req := httplib.Get("https://api.github.com/repos/ccfos/nightingale/releases/latest")
var release GithubRelease
err := req.ToJSON(&release)
if err != nil {
logger.Errorf("get github version fail: %v", err)
}
GithubVersion.Store(release.TagName)
time.Sleep(24 * time.Hour)
}
}
type GithubRelease struct {
TagName string `json:"tag_name"`
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册