From 86a3692dad498fd2aae9a7a5d14cece375b97a37 Mon Sep 17 00:00:00 2001 From: link Date: Thu, 23 Feb 2023 10:38:48 +0800 Subject: [PATCH] Add proxy search interface (#917) --- route/v1/other.go | 10 +++++++--- service/other.go | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/route/v1/other.go b/route/v1/other.go index 9d28269..bbc552b 100644 --- a/route/v1/other.go +++ b/route/v1/other.go @@ -10,12 +10,16 @@ import ( ) func GetSearchResult(c *gin.Context) { - key := c.Query("key") - if key == "" { + json := make(map[string]string) + c.ShouldBind(&json) + url := json["url"] + + if url == "" { c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: "key is empty"}) return } - data, err := service.MyService.Other().Search(key) + //data, err := service.MyService.Other().Search(key) + data, err := service.MyService.Other().AgentSearch(url) if err != nil { fmt.Println(err) c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()}) diff --git a/service/other.go b/service/other.go index ae92566..3133bbf 100644 --- a/service/other.go +++ b/service/other.go @@ -15,6 +15,7 @@ import ( type OtherService interface { Search(key string) ([]model.SearchEngine, error) + AgentSearch(url string) ([]byte, error) } type otherService struct{} @@ -99,6 +100,17 @@ func (s *otherService) Search(key string) ([]model.SearchEngine, error) { } +func (s *otherService) AgentSearch(url string) ([]byte, error) { + client := resty.New() + client.SetTimeout(3 * time.Second) // 设置全局超时时间 + resp, err := client.R().Get(url) + if err != nil { + logger.Error("Then get search result error: %v", zap.Error(err), zap.String("url", url)) + return nil, err + } + return resp.Body(), nil +} + func NewOtherService() OtherService { return &otherService{} }