send search request wip
This commit is contained in:
parent
1baa40b620
commit
506107286d
5 changed files with 152 additions and 106 deletions
26
forums.go
26
forums.go
|
@ -11,8 +11,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var resultsChan = make(chan []ForumSearchResult)
|
|
||||||
|
|
||||||
func PerformRedditSearch(query string, safe string, page int) ([]ForumSearchResult, error) {
|
func PerformRedditSearch(query string, safe string, page int) ([]ForumSearchResult, error) {
|
||||||
const (
|
const (
|
||||||
pageSize = 25
|
pageSize = 25
|
||||||
|
@ -194,27 +192,3 @@ func sendSearchRequestToNode(nodeAddr, query, safe, lang string, page int) ([]Fo
|
||||||
return nil, fmt.Errorf("timeout waiting for results from node %s", nodeAddr)
|
return nil, fmt.Errorf("timeout waiting for results from node %s", nodeAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleForumResultsMessage(msg Message) {
|
|
||||||
var results []ForumSearchResult
|
|
||||||
err := json.Unmarshal([]byte(msg.Content), &results)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error unmarshalling forum results: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Received forum results: %+v", results)
|
|
||||||
// Send results to resultsChan
|
|
||||||
go func() {
|
|
||||||
resultsChan <- results
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchForumResults(query, safe, lang string, page int) []ForumSearchResult {
|
|
||||||
results, err := PerformRedditSearch(query, safe, page)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error fetching forum results: %v", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
60
images.go
60
images.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
@ -40,6 +41,7 @@ func handleImageSearch(w http.ResponseWriter, query, safe, lang string, page int
|
||||||
CurrentLang string
|
CurrentLang string
|
||||||
HasPrevPage bool
|
HasPrevPage bool
|
||||||
HasNextPage bool
|
HasNextPage bool
|
||||||
|
NoResults bool
|
||||||
}{
|
}{
|
||||||
Results: combinedResults,
|
Results: combinedResults,
|
||||||
Query: query,
|
Query: query,
|
||||||
|
@ -49,6 +51,7 @@ func handleImageSearch(w http.ResponseWriter, query, safe, lang string, page int
|
||||||
CurrentLang: lang,
|
CurrentLang: lang,
|
||||||
HasPrevPage: page > 1,
|
HasPrevPage: page > 1,
|
||||||
HasNextPage: len(combinedResults) >= 50,
|
HasNextPage: len(combinedResults) >= 50,
|
||||||
|
NoResults: len(combinedResults) == 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tmpl.Execute(w, data)
|
err = tmpl.Execute(w, data)
|
||||||
|
@ -120,12 +123,67 @@ func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult {
|
||||||
|
|
||||||
// If no results found after trying all engines
|
// If no results found after trying all engines
|
||||||
if len(results) == 0 {
|
if len(results) == 0 {
|
||||||
log.Printf("No image results found for query: %s", query)
|
log.Printf("No image results found for query: %s, trying other nodes", query)
|
||||||
|
results = tryOtherNodesForImageSearch(query, safe, lang, page)
|
||||||
}
|
}
|
||||||
|
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tryOtherNodesForImageSearch(query, safe, lang string, page int) []ImageSearchResult {
|
||||||
|
for _, nodeAddr := range peers {
|
||||||
|
results, err := sendImageSearchRequestToNode(nodeAddr, query, safe, lang, page)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error contacting node %s: %v", nodeAddr, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(results) > 0 {
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendImageSearchRequestToNode(nodeAddr, query, safe, lang string, page int) ([]ImageSearchResult, 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: fmt.Sprintf("http://localhost:%d/node", config.Port),
|
||||||
|
}
|
||||||
|
|
||||||
|
msgBytes, err := json.Marshal(searchParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal search parameters: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := Message{
|
||||||
|
ID: hostID,
|
||||||
|
Type: "search-image",
|
||||||
|
Content: string(msgBytes),
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sendMessage(nodeAddr, msg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to send search request to node %s: %v", nodeAddr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for results
|
||||||
|
select {
|
||||||
|
case res := <-imageResultsChan:
|
||||||
|
return res, nil
|
||||||
|
case <-time.After(30 * time.Second): // Need to handle this better, setting a static number is stupid
|
||||||
|
return nil, fmt.Errorf("timeout waiting for results from node %s", nodeAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func wrapImageSearchFunc(f func(string, string, string, int) ([]ImageSearchResult, time.Duration, error)) func(string, string, string, int) ([]SearchResult, time.Duration, error) {
|
func wrapImageSearchFunc(f func(string, string, string, int) ([]ImageSearchResult, time.Duration, error)) func(string, string, string, int) ([]SearchResult, time.Duration, error) {
|
||||||
return func(query, safe, lang string, page int) ([]SearchResult, time.Duration, error) {
|
return func(query, safe, lang string, page int) ([]SearchResult, time.Duration, error) {
|
||||||
imageResults, duration, err := f(query, safe, lang, page)
|
imageResults, duration, err := f(query, safe, lang, page)
|
||||||
|
|
|
@ -49,6 +49,7 @@ func handleSearchImageMessage(msg Message) {
|
||||||
Safe string `json:"safe"`
|
Safe string `json:"safe"`
|
||||||
Lang string `json:"lang"`
|
Lang string `json:"lang"`
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
|
ResponseAddr string `json:"responseAddr"`
|
||||||
}
|
}
|
||||||
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -56,6 +57,8 @@ func handleSearchImageMessage(msg Message) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Received search-image request. ResponseAddr: %s", searchParams.ResponseAddr)
|
||||||
|
|
||||||
results := fetchImageResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page)
|
results := fetchImageResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page)
|
||||||
resultsJSON, err := json.Marshal(results)
|
resultsJSON, err := json.Marshal(results)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,9 +72,17 @@ func handleSearchImageMessage(msg Message) {
|
||||||
Content: string(resultsJSON),
|
Content: string(resultsJSON),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = sendMessage(msg.ID, responseMsg)
|
// Log the address to be used for sending the response
|
||||||
|
log.Printf("Sending image search results to %s", searchParams.ResponseAddr)
|
||||||
|
|
||||||
|
if searchParams.ResponseAddr == "" {
|
||||||
|
log.Printf("Error: Response address is empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sendMessage(searchParams.ResponseAddr, responseMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error sending image search results to %s: %v", msg.ID, err)
|
log.Printf("Error sending image search results to %s: %v", searchParams.ResponseAddr, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,79 +2,80 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func sendSearchRequestToNode(nodeAddr, query, safe, lang string, page int, requestID string) ([]ForumSearchResult, error) {
|
////// FORUMS /////
|
||||||
// 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{
|
var resultsChan = make(chan []ForumSearchResult)
|
||||||
// ID: requestID,
|
|
||||||
// Type: "search-forum",
|
|
||||||
// Content: toJSON(searchParams),
|
|
||||||
// }
|
|
||||||
|
|
||||||
// msgBytes, err := json.Marshal(msg)
|
func sendForumResults(responseAddr string, results []ForumSearchResult) {
|
||||||
// if err != nil {
|
resultsMsg := Message{
|
||||||
// return nil, fmt.Errorf("failed to marshal search request: %v", err)
|
ID: hostID,
|
||||||
// }
|
Type: "forum-results",
|
||||||
|
Content: marshalResults(results),
|
||||||
|
}
|
||||||
|
|
||||||
// req, err := http.NewRequest("POST", nodeAddr, bytes.NewBuffer(msgBytes))
|
err := sendMessage(responseAddr, resultsMsg)
|
||||||
// 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 {
|
if err != nil {
|
||||||
|
log.Printf("Error sending forum search results to %s: %v", responseAddr, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Forum search results sent successfully to %s", responseAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func marshalResults(results []ForumSearchResult) string {
|
||||||
|
content, err := json.Marshal(results)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error marshalling forum results: %v", err)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return string(data)
|
return string(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleForumResultsMessage(msg Message) {
|
||||||
|
var results []ForumSearchResult
|
||||||
|
err := json.Unmarshal([]byte(msg.Content), &results)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error unmarshalling forum results: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Received forum results: %+v", results)
|
||||||
|
// Send results to resultsChan
|
||||||
|
go func() {
|
||||||
|
resultsChan <- results
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchForumResults(query, safe, lang string, page int) []ForumSearchResult {
|
||||||
|
results, err := PerformRedditSearch(query, safe, page)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error fetching forum results: %v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
////// FORUMS //////
|
||||||
|
|
||||||
|
////// IMAGES /////
|
||||||
|
|
||||||
|
var imageResultsChan = make(chan []ImageSearchResult)
|
||||||
|
|
||||||
|
func handleImageResultsMessage(msg Message) {
|
||||||
|
var results []ImageSearchResult
|
||||||
|
err := json.Unmarshal([]byte(msg.Content), &results)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error unmarshalling image results: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Received image results: %+v", results)
|
||||||
|
// Send results to imageResultsChan
|
||||||
|
go func() {
|
||||||
|
imageResultsChan <- results
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
////// IMAGES /////
|
||||||
|
|
14
node.go
14
node.go
|
@ -153,13 +153,15 @@ func interpretMessage(msg Message) {
|
||||||
handleSearchForumMessage(msg)
|
handleSearchForumMessage(msg)
|
||||||
case "forum-results":
|
case "forum-results":
|
||||||
handleForumResultsMessage(msg)
|
handleForumResultsMessage(msg)
|
||||||
|
// case "text-results":
|
||||||
|
// handleTextResultsMessage(msg) // need to implement
|
||||||
|
case "image-results":
|
||||||
|
handleImageResultsMessage(msg) // need to implement
|
||||||
|
// case "video-results":
|
||||||
|
// handleVideoResultsMessage(msg) // need to implement
|
||||||
|
// case "file-results":
|
||||||
|
// handleFileResultsMessage(msg) // need to implement
|
||||||
default:
|
default:
|
||||||
fmt.Println("Received unknown message type:", msg.Type)
|
fmt.Println("Received unknown message type:", msg.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateRequestID() string {
|
|
||||||
bytes := make([]byte, 16)
|
|
||||||
rand.Read(bytes)
|
|
||||||
return fmt.Sprintf("%x", bytes)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue