Skip to content

Commit

Permalink
Add proxy search interface (IceWhaleTech#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkLeong authored Feb 23, 2023
1 parent 45a5567 commit 86a3692
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions route/v1/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand Down
12 changes: 12 additions & 0 deletions service/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

type OtherService interface {
Search(key string) ([]model.SearchEngine, error)
AgentSearch(url string) ([]byte, error)
}

type otherService struct{}
Expand Down Expand Up @@ -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{}
}

0 comments on commit 86a3692

Please sign in to comment.