Browse Source

feat: add /api/exist

xiaobing.wang 1 year ago
parent
commit
9166f87178

+ 19 - 6
backend/docs/docs.go

@@ -16,7 +16,7 @@ const docTemplate = `{
     "basePath": "{{.BasePath}}",
     "basePath": "{{.BasePath}}",
     "paths": {
     "paths": {
         "/exist": {
         "/exist": {
-            "get": {
+            "post": {
                 "description": "get ip if id exist",
                 "description": "get ip if id exist",
                 "consumes": [
                 "consumes": [
                     "application/json"
                     "application/json"
@@ -30,11 +30,13 @@ const docTemplate = `{
                 "summary": "get ip if id exist",
                 "summary": "get ip if id exist",
                 "parameters": [
                 "parameters": [
                     {
                     {
-                        "type": "string",
-                        "description": "id",
-                        "name": "id",
-                        "in": "query",
-                        "required": true
+                        "description": "body",
+                        "name": "body",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/handler.ExistReq"
+                        }
                     }
                     }
                 ],
                 ],
                 "responses": {
                 "responses": {
@@ -163,6 +165,17 @@ const docTemplate = `{
         }
         }
     },
     },
     "definitions": {
     "definitions": {
+        "handler.ExistReq": {
+            "type": "object",
+            "properties": {
+                "id": {
+                    "type": "string"
+                },
+                "token": {
+                    "type": "string"
+                }
+            }
+        },
         "service.Category": {
         "service.Category": {
             "type": "object",
             "type": "object",
             "properties": {
             "properties": {

+ 19 - 6
backend/docs/swagger.json

@@ -5,7 +5,7 @@
     },
     },
     "paths": {
     "paths": {
         "/exist": {
         "/exist": {
-            "get": {
+            "post": {
                 "description": "get ip if id exist",
                 "description": "get ip if id exist",
                 "consumes": [
                 "consumes": [
                     "application/json"
                     "application/json"
@@ -19,11 +19,13 @@
                 "summary": "get ip if id exist",
                 "summary": "get ip if id exist",
                 "parameters": [
                 "parameters": [
                     {
                     {
-                        "type": "string",
-                        "description": "id",
-                        "name": "id",
-                        "in": "query",
-                        "required": true
+                        "description": "body",
+                        "name": "body",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/handler.ExistReq"
+                        }
                     }
                     }
                 ],
                 ],
                 "responses": {
                 "responses": {
@@ -152,6 +154,17 @@
         }
         }
     },
     },
     "definitions": {
     "definitions": {
+        "handler.ExistReq": {
+            "type": "object",
+            "properties": {
+                "id": {
+                    "type": "string"
+                },
+                "token": {
+                    "type": "string"
+                }
+            }
+        },
         "service.Category": {
         "service.Category": {
             "type": "object",
             "type": "object",
             "properties": {
             "properties": {

+ 13 - 5
backend/docs/swagger.yaml

@@ -1,4 +1,11 @@
 definitions:
 definitions:
+  handler.ExistReq:
+    properties:
+      id:
+        type: string
+      token:
+        type: string
+    type: object
   service.Category:
   service.Category:
     properties:
     properties:
       emoji:
       emoji:
@@ -92,16 +99,17 @@ info:
   contact: {}
   contact: {}
 paths:
 paths:
   /exist:
   /exist:
-    get:
+    post:
       consumes:
       consumes:
       - application/json
       - application/json
       description: get ip if id exist
       description: get ip if id exist
       parameters:
       parameters:
-      - description: id
-        in: query
-        name: id
+      - description: body
+        in: body
+        name: body
         required: true
         required: true
-        type: string
+        schema:
+          $ref: '#/definitions/handler.ExistReq'
       produces:
       produces:
       - application/json
       - application/json
       responses:
       responses:

+ 15 - 5
backend/internal/handler/safeline.go

@@ -35,17 +35,27 @@ func (h *SafelineHandler) GetInstallerCount(c *gin.Context) {
 	c.JSON(200, count)
 	c.JSON(200, count)
 }
 }
 
 
-// GetExist return ip if id exist
+type ExistReq struct {
+	Id    string `json:"id"`
+	Token string `json:"token"`
+}
+
+// Exist return ip if id exist
 // @Summary get ip if id exist
 // @Summary get ip if id exist
 // @Description get ip if id exist
 // @Description get ip if id exist
 // @Tags Safeline
 // @Tags Safeline
 // @Accept json
 // @Accept json
 // @Produce json
 // @Produce json
-// @Param id query string true "id"
+// @Param body body ExistReq true "body"
 // @Success 200 {object} string
 // @Success 200 {object} string
-// @Router /exist [get]
-func (h *SafelineHandler) GetExist(c *gin.Context) {
-	ip, err := h.safelineService.GetExist(c, c.Query("id"))
+// @Router /exist [post]
+func (h *SafelineHandler) Exist(c *gin.Context) {
+	req := &ExistReq{}
+	if err := c.ShouldBindJSON(req); err != nil {
+		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+		return
+	}
+	ip, err := h.safelineService.GetExist(c, req.Id, req.Token)
 	if err != nil {
 	if err != nil {
 		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
 		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
 		return
 		return

+ 8 - 3
backend/internal/service/safeline.go

@@ -4,8 +4,11 @@ import (
 	"context"
 	"context"
 	"crypto/tls"
 	"crypto/tls"
 	"encoding/json"
 	"encoding/json"
+	"errors"
 	"fmt"
 	"fmt"
+	"io"
 	"net/http"
 	"net/http"
+	"strings"
 )
 )
 
 
 var cacheCount InstallerCount
 var cacheCount InstallerCount
@@ -54,8 +57,9 @@ func (s *SafelineService) GetInstallerCount(ctx context.Context) (InstallerCount
 }
 }
 
 
 // GetExist return ip if id exist
 // 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)
+func (s *SafelineService) GetExist(ctx context.Context, id string, token string) (string, error) {
+	body := fmt.Sprintf(`{"id":"%s", "token": "%s"}`, id, token)
+	req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.APIHost+"/api/v1/public/safeline/exist", strings.NewReader(body))
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err
 	}
 	}
@@ -65,7 +69,8 @@ func (s *SafelineService) GetExist(ctx context.Context, id string) (string, erro
 	}
 	}
 	defer res.Body.Close()
 	defer res.Body.Close()
 	if res.StatusCode != http.StatusOK {
 	if res.StatusCode != http.StatusOK {
-		return "", fmt.Errorf("id %s not found", id)
+		raw, _ := io.ReadAll(res.Body)
+		return "", errors.New(string(raw))
 	}
 	}
 	var r map[string]interface{}
 	var r map[string]interface{}
 	if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
 	if err := json.NewDecoder(res.Body).Decode(&r); err != nil {

+ 1 - 1
backend/main.go

@@ -53,7 +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)
+	v1.POST("/exist", safelineHandler.Exist)
 
 
 	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))