修正地址

This commit is contained in:
2024-12-16 11:54:05 +08:00
parent c4a66fa06d
commit dc86f93a28
2 changed files with 70 additions and 0 deletions

69
api/search.go Normal file
View File

@@ -0,0 +1,69 @@
package api
import (
"encoding/json"
"io"
"log"
"net/http"
"github.com/graphql-go/graphql"
)
type Search struct {
Name string `json:"text"`
Count int `json:"count"`
}
var SearchItem = graphql.NewObject(graphql.ObjectConfig{
Name: "Search",
Description: "搜索",
Fields: graphql.Fields{
"name": &graphql.Field{Type: graphql.String, Description: "搜索词"},
},
})
var SearchItems = &graphql.Field{
Name: "Searchs",
Description: "搜索词列表",
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "SearchConnection",
Description: "搜索词列表",
Fields: graphql.Fields{
"list": &graphql.Field{Type: graphql.NewList(SearchItem), Description: "搜索词列表"},
},
}),
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{Type: graphql.String, Description: "按指定字符筛选"},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
var searchs []Search
// 发送 GET 请求
resp, err := http.Get("http://localhost:6005/api/get_search/hot")
if err != nil {
log.Fatalf("Failed to fetch data: %v", err)
}
defer resp.Body.Close()
// 读取响应体
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Failed to read response body: %v", err)
}
// 检查 HTTP 状态码
if resp.StatusCode != http.StatusOK {
log.Println("HTTP request failed with status code: %d", resp.StatusCode)
}
// 解码 JSON 数据
err = json.Unmarshal(body, &searchs)
if err != nil {
log.Println("Failed to parse JSON: %v", err)
}
return map[string]interface{}{
"list": searchs,
}, err
},
}

View File

@@ -246,6 +246,7 @@ func main() {
"article": api.ArticleItem, "article": api.ArticleItem,
"images": api.ImageItems, "images": api.ImageItems,
"image": api.ImageItem, "image": api.ImageItem,
"searchs": api.SearchItems,
}})}) }})})
if err != nil { if err != nil {