Ben 1 năm trước cách đây
mục cha
commit
54cc7af561
2 tập tin đã thay đổi với 24 bổ sung9 xóa
  1. 1 0
      internal/dto/response.go
  2. 23 9
      internal/server/server.go

+ 1 - 0
internal/dto/response.go

@@ -68,4 +68,5 @@ type ConfigResponse struct {
 type ConfigResult struct {
 	FreeTrialDuration uint64 `json:"freeTrialDuration"`
 	Timestamp         int64  `json:"timestamp"`
+	Node              Node   `json:"node"`
 }

+ 23 - 9
internal/server/server.go

@@ -29,6 +29,7 @@ func Config(c *gin.Context) {
 		Data: dto.ConfigResult{
 			FreeTrialDuration: totalFreeDuration - usedDuration,
 			Timestamp:         time.Now().Unix(),
+			Node:              convert2DtoNode(healthNodes()[0]),
 		},
 	})
 }
@@ -55,6 +56,7 @@ func UpdateUsedDuration(c *gin.Context) {
 		Data: dto.ConfigResult{
 			FreeTrialDuration: totalFreeDuration - usedDuration,
 			Timestamp:         time.Now().Unix(),
+			Node:              convert2DtoNode(healthNodes()[0]),
 		},
 	})
 }
@@ -99,15 +101,8 @@ func List(c *gin.Context) {
 	defer locker.RUnlock()
 
 	dtoNodes := make([]dto.Node, 0)
-	for _, node := range nodes {
-		if node.LastUpdateTime.Add(10 * time.Second).After(time.Now()) {
-			dtoNodes = append(dtoNodes, dto.Node{
-				Ip:          node.Ip,
-				CountryCode: node.CountryCode,
-				CountryName: node.CountryName,
-				Icon:        "https://img.zcool.cn/community/010a5a57dbc2140000018c1b0a9b5f.png",
-			})
-		}
+	for _, node := range healthNodes() {
+		dtoNodes = append(dtoNodes, convert2DtoNode(node))
 	}
 
 	c.JSON(http.StatusOK, dto.ListResponse{
@@ -164,3 +159,22 @@ func Secret(c *gin.Context) {
 func Health(c *gin.Context) {
 	c.JSON(http.StatusOK, gin.H{"status": "up"})
 }
+
+func healthNodes() []*model.Node {
+	healthNodes := make([]*model.Node, 0)
+	for _, node := range nodes {
+		if node.LastUpdateTime.Add(10 * time.Second).After(time.Now()) {
+			healthNodes = append(healthNodes, node)
+		}
+	}
+	return healthNodes
+}
+
+func convert2DtoNode(node *model.Node) dto.Node {
+	return dto.Node{
+		Ip:          node.Ip,
+		CountryCode: node.CountryCode,
+		CountryName: node.CountryName,
+		Icon:        "https://img.zcool.cn/community/010a5a57dbc2140000018c1b0a9b5f.png",
+	}
+}