Search/node-request-search.go
2024-08-08 21:59:10 +02:00

80 lines
2.1 KiB
Go

package main
import (
"encoding/json"
)
// func sendSearchRequestToNode(nodeAddr, query, safe, lang string, page int, requestID string) ([]ForumSearchResult, error) {
// searchParams := struct {
// Query string `json:"query"`
// Safe string `json:"safe"`
// Lang string `json:"lang"`
// Page int `json:"page"`
// ResponseAddr string `json:"responseAddr"`
// }{
// Query: query,
// Safe: safe,
// Lang: lang,
// Page: page,
// ResponseAddr: "http://localhost:5000/node", // Node 1's address
// }
// msg := Message{
// ID: requestID,
// Type: "search-forum",
// Content: toJSON(searchParams),
// }
// msgBytes, err := json.Marshal(msg)
// if err != nil {
// return nil, fmt.Errorf("failed to marshal search request: %v", err)
// }
// req, err := http.NewRequest("POST", nodeAddr, bytes.NewBuffer(msgBytes))
// if err != nil {
// return nil, fmt.Errorf("failed to create search request: %v", err)
// }
// req.Header.Set("Content-Type", "application/json")
// req.Header.Set("Authorization", authCode)
// client := &http.Client{
// Timeout: time.Second * 10,
// }
// resp, err := client.Do(req)
// if err != nil {
// return nil, fmt.Errorf("failed to send search request: %v", err)
// }
// defer resp.Body.Close()
// if resp.StatusCode != http.StatusOK {
// body, _ := ioutil.ReadAll(resp.Body)
// return nil, fmt.Errorf("server error: %s", body)
// }
// var responseMsg Message
// err = json.NewDecoder(resp.Body).Decode(&responseMsg)
// if err != nil {
// return nil, fmt.Errorf("failed to decode search response: %v", err)
// }
// if responseMsg.Type != "forum-results" {
// return nil, fmt.Errorf("unexpected message type: %s", responseMsg.Type)
// }
// var results []ForumSearchResult
// err = json.Unmarshal([]byte(responseMsg.Content), &results)
// if err != nil {
// return nil, fmt.Errorf("failed to unmarshal search results: %v", err)
// }
// return results, nil
// }
func toJSON(v interface{}) string {
data, err := json.Marshal(v)
if err != nil {
return ""
}
return string(data)
}