Browse Source

Merge pull request #505 from xbingW/main

增加 /api/exist 接口
yrluke 1 year ago
parent
commit
ef5269f634

+ 45 - 0
backend/docs/docs.go

@@ -15,6 +15,40 @@ const docTemplate = `{
     "host": "{{.Host}}",
     "basePath": "{{.BasePath}}",
     "paths": {
+        "/exist": {
+            "post": {
+                "description": "get ip if id exist",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Safeline"
+                ],
+                "summary": "get ip if id exist",
+                "parameters": [
+                    {
+                        "description": "body",
+                        "name": "body",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/handler.ExistReq"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/repos/discussions": {
             "get": {
                 "description": "get discussions from GitHub",
@@ -131,6 +165,17 @@ const docTemplate = `{
         }
     },
     "definitions": {
+        "handler.ExistReq": {
+            "type": "object",
+            "properties": {
+                "id": {
+                    "type": "string"
+                },
+                "token": {
+                    "type": "string"
+                }
+            }
+        },
         "service.Category": {
             "type": "object",
             "properties": {

+ 45 - 0
backend/docs/swagger.json

@@ -4,6 +4,40 @@
         "contact": {}
     },
     "paths": {
+        "/exist": {
+            "post": {
+                "description": "get ip if id exist",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Safeline"
+                ],
+                "summary": "get ip if id exist",
+                "parameters": [
+                    {
+                        "description": "body",
+                        "name": "body",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/handler.ExistReq"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/repos/discussions": {
             "get": {
                 "description": "get discussions from GitHub",
@@ -120,6 +154,17 @@
         }
     },
     "definitions": {
+        "handler.ExistReq": {
+            "type": "object",
+            "properties": {
+                "id": {
+                    "type": "string"
+                },
+                "token": {
+                    "type": "string"
+                }
+            }
+        },
         "service.Category": {
             "type": "object",
             "properties": {

+ 29 - 0
backend/docs/swagger.yaml

@@ -1,4 +1,11 @@
 definitions:
+  handler.ExistReq:
+    properties:
+      id:
+        type: string
+      token:
+        type: string
+    type: object
   service.Category:
     properties:
       emoji:
@@ -91,6 +98,28 @@ definitions:
 info:
   contact: {}
 paths:
+  /exist:
+    post:
+      consumes:
+      - application/json
+      description: get ip if id exist
+      parameters:
+      - description: body
+        in: body
+        name: body
+        required: true
+        schema:
+          $ref: '#/definitions/handler.ExistReq'
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            type: string
+      summary: get ip if id exist
+      tags:
+      - Safeline
   /repos/discussions:
     get:
       consumes:

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

@@ -34,3 +34,31 @@ func (h *SafelineHandler) GetInstallerCount(c *gin.Context) {
 	}
 	c.JSON(200, count)
 }
+
+type ExistReq struct {
+	Id    string `json:"id"`
+	Token string `json:"token"`
+}
+
+// Exist return ip if id exist
+// @Summary get ip if id exist
+// @Description get ip if id exist
+// @Tags Safeline
+// @Accept json
+// @Produce json
+// @Param body body ExistReq true "body"
+// @Success 200 {object} string
+// @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 {
+		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
+		return
+	}
+	c.JSON(200, gin.H{"ip": ip})
+}

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

@@ -4,7 +4,11 @@ import (
 	"context"
 	"crypto/tls"
 	"encoding/json"
+	"errors"
+	"fmt"
+	"io"
 	"net/http"
+	"strings"
 )
 
 var cacheCount InstallerCount
@@ -51,3 +55,29 @@ func (s *SafelineService) GetInstallerCount(ctx context.Context) (InstallerCount
 	}
 	return cacheCount, nil
 }
+
+// GetExist return ip if id exist
+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 {
+		return "", err
+	}
+	res, err := s.client.Do(req)
+	if err != nil {
+		return "", err
+	}
+	defer res.Body.Close()
+	if res.StatusCode != http.StatusOK {
+		raw, _ := io.ReadAll(res.Body)
+		return "", errors.New(string(raw))
+	}
+	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)
 	safelineHandler := handler.NewSafelineHandler(safelineService)
 	v1.GET("/safeline/count", safelineHandler.GetInstallerCount)
+	v1.POST("/exist", safelineHandler.Exist)
 
 	docs.SwaggerInfo.BasePath = v1.BasePath()
 	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))