ソースを参照

添加health接口

yjp 3 年 前
コミット
b3b7b14ad1
2 ファイル変更41 行追加0 行削除
  1. 27 0
      dapr_api.go
  2. 14 0
      test/health_test.go

+ 27 - 0
dapr_api.go

@@ -0,0 +1,27 @@
+package dapr_api
+
+import (
+	"fmt"
+	"github.com/go-resty/resty/v2"
+	"time"
+)
+
+const (
+	healthUrlFormat = "http://localhost:%d/v1.0/healthz"
+)
+
+func Health(daprPort int, timeout time.Duration) error {
+	healthUrl := fmt.Sprintf(healthUrlFormat, daprPort)
+
+	resp, err := resty.New().SetTimeout(timeout).R().Get(healthUrl)
+	if err != nil {
+		return err
+	}
+
+	if resp.IsError() {
+		return fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
+	}
+
+	return nil
+
+}

+ 14 - 0
test/health_test.go

@@ -0,0 +1,14 @@
+package test
+
+import (
+	"git.sxidc.com/service-supports/dapr_api"
+	"testing"
+	"time"
+)
+
+func TestHealth(t *testing.T) {
+	err := dapr_api.Health(10080, 10*time.Second)
+	if err != nil {
+		t.Fatal(err)
+	}
+}