diff --git a/gorush/server.go b/gorush/server.go index 58c8b95..4eee5bb 100644 --- a/gorush/server.go +++ b/gorush/server.go @@ -36,6 +36,13 @@ func heartbeatHandler(c *gin.Context) { c.AbortWithStatus(200) } +func versionHandler(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "source": "https://github.com/appleboy/gorush", + "version": GetVersion(), + }) +} + func pushHandler(c *gin.Context) { var form RequestPush var msg string @@ -112,6 +119,7 @@ func routerEngine() *gin.Engine { r.POST(PushConf.API.PushURI, pushHandler) r.GET(PushConf.API.MetricURI, metricsHandler) r.GET("/healthz", heartbeatHandler) + r.GET("/version", versionHandler) r.GET("/", rootHandler) return r diff --git a/gorush/server_test.go b/gorush/server_test.go index 233bb47..b7a1775 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -268,6 +268,23 @@ func TestHeartbeatHandler(t *testing.T) { assert.Equal(t, http.StatusOK, r.Code) }) } + +func TestVersionHandler(t *testing.T) { + SetVersion("3.0.0") + initTest() + + r := gofight.New() + + r.GET("/version"). + Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { + assert.Equal(t, http.StatusOK, r.Code) + data := []byte(r.Body.String()) + + value, _ := jsonparser.GetString(data, "version") + + assert.Equal(t, "3.0.0", value) + }) +} func TestDisabledHTTPServer(t *testing.T) { initTest() PushConf.Core.Enabled = false