Browse Source

feat: add /api/exist

xiaobing.wang 1 year ago
parent
commit
8963fdd7bf

+ 32 - 0
backend/docs/docs.go

@@ -15,6 +15,38 @@ const docTemplate = `{
     "host": "{{.Host}}",
     "host": "{{.Host}}",
     "basePath": "{{.BasePath}}",
     "basePath": "{{.BasePath}}",
     "paths": {
     "paths": {
+        "/exist": {
+            "get": {
+                "description": "get ip if id exist",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Safeline"
+                ],
+                "summary": "get ip if id exist",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "id",
+                        "name": "id",
+                        "in": "query",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/repos/discussions": {
         "/repos/discussions": {
             "get": {
             "get": {
                 "description": "get discussions from GitHub",
                 "description": "get discussions from GitHub",

+ 32 - 0
backend/docs/swagger.json

@@ -4,6 +4,38 @@
         "contact": {}
         "contact": {}
     },
     },
     "paths": {
     "paths": {
+        "/exist": {
+            "get": {
+                "description": "get ip if id exist",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Safeline"
+                ],
+                "summary": "get ip if id exist",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "id",
+                        "name": "id",
+                        "in": "query",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/repos/discussions": {
         "/repos/discussions": {
             "get": {
             "get": {
                 "description": "get discussions from GitHub",
                 "description": "get discussions from GitHub",

+ 21 - 0
backend/docs/swagger.yaml

@@ -91,6 +91,27 @@ definitions:
 info:
 info:
   contact: {}
   contact: {}
 paths:
 paths:
+  /exist:
+    get:
+      consumes:
+      - application/json
+      description: get ip if id exist
+      parameters:
+      - description: id
+        in: query
+        name: id
+        required: true
+        type: string
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            type: string
+      summary: get ip if id exist
+      tags:
+      - Safeline
   /repos/discussions:
   /repos/discussions:
     get:
     get:
       consumes:
       consumes:

+ 18 - 0
backend/internal/handler/safeline.go

@@ -34,3 +34,21 @@ func (h *SafelineHandler) GetInstallerCount(c *gin.Context) {
 	}
 	}
 	c.JSON(200, count)
 	c.JSON(200, count)
 }
 }
+
+// GetExist return ip if id exist
+// @Summary get ip if id exist
+// @Description get ip if id exist
+// @Tags Safeline
+// @Accept json
+// @Produce json
+// @Param id query string true "id"
+// @Success 200 {object} string
+// @Router /exist [get]
+func (h *SafelineHandler) GetExist(c *gin.Context) {
+	ip, err := h.safelineService.GetExist(c, c.Query("id"))
+	if err != nil {
+		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
+		return
+	}
+	c.JSON(200, gin.H{"ip": ip})
+}

+ 25 - 0
backend/internal/service/safeline.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"context"
 	"crypto/tls"
 	"crypto/tls"
 	"encoding/json"
 	"encoding/json"
+	"fmt"
 	"net/http"
 	"net/http"
 )
 )
 
 
@@ -51,3 +52,27 @@ func (s *SafelineService) GetInstallerCount(ctx context.Context) (InstallerCount
 	}
 	}
 	return cacheCount, nil
 	return cacheCount, nil
 }
 }
+
+// GetExist return ip if id exist
+func (s *SafelineService) GetExist(ctx context.Context, id string) (string, error) {
+	req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.APIHost+"/api/v1/public/safeline/exist?id="+id, nil)
+	if err != nil {
+		return "", err
+	}
+	res, err := s.client.Do(req)
+	if err != nil {
+		return "", err
+	}
+	defer res.Body.Close()
+	if res.StatusCode != http.StatusOK {
+		return "", fmt.Errorf("id %s not found", id)
+	}
+	var r map[string]interface{}
+	if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
+		return "", err
+	}
+	if r["code"].(float64) != 0 {
+		return "", nil
+	}
+	return r["data"].(map[string]interface{})["ip"].(string), nil
+}

+ 1 - 0
backend/main.go

@@ -53,6 +53,7 @@ func main() {
 	safelineService := service.NewSafelineService(telemetryHost)
 	safelineService := service.NewSafelineService(telemetryHost)
 	safelineHandler := handler.NewSafelineHandler(safelineService)
 	safelineHandler := handler.NewSafelineHandler(safelineService)
 	v1.GET("/safeline/count", safelineHandler.GetInstallerCount)
 	v1.GET("/safeline/count", safelineHandler.GetInstallerCount)
+	v1.GET("/exist", safelineHandler.GetExist)
 
 
 	docs.SwaggerInfo.BasePath = v1.BasePath()
 	docs.SwaggerInfo.BasePath = v1.BasePath()
 	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
 	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))