2024-05-16 18:29:26 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-05-17 14:26:28 +02:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
2024-06-09 21:44:49 +02:00
|
|
|
"math/rand"
|
2024-05-16 18:29:26 +02:00
|
|
|
"net/http"
|
2024-05-18 01:59:29 +02:00
|
|
|
"sync"
|
2024-05-17 14:26:28 +02:00
|
|
|
"time"
|
2024-05-16 18:29:26 +02:00
|
|
|
)
|
|
|
|
|
2024-05-19 22:57:23 +02:00
|
|
|
var (
|
2024-06-09 21:44:49 +02:00
|
|
|
searchEngines []searchEngine
|
|
|
|
searchEngineLock sync.Mutex
|
2024-05-19 22:57:23 +02:00
|
|
|
)
|
2024-05-18 01:59:29 +02:00
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
type searchEngine struct {
|
|
|
|
Name string
|
|
|
|
Func func(string, string, string, int) ([]TextSearchResult, error)
|
|
|
|
Weight int
|
|
|
|
}
|
|
|
|
|
2024-05-18 01:59:29 +02:00
|
|
|
func init() {
|
2024-06-09 21:44:49 +02:00
|
|
|
searchEngines = []searchEngine{
|
|
|
|
{Name: "Google", Func: PerformGoogleTextSearch, Weight: 1},
|
|
|
|
{Name: "LibreX", Func: PerformLibreXTextSearch, Weight: 2},
|
|
|
|
// {Name: "DuckDuckGo", Func: PerformDuckDuckGoTextSearch, Weight: 3}, // DuckDuckGo timeouts too fast and search results are trash
|
|
|
|
// {Name: "SearXNG", Func: PerformSearXNGTextSearch, Weight: 2}, // Uncomment when implemented
|
|
|
|
}
|
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2024-05-18 01:59:29 +02:00
|
|
|
}
|
2024-05-17 14:26:28 +02:00
|
|
|
|
2024-05-19 22:57:23 +02:00
|
|
|
func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int) {
|
2024-05-18 01:59:29 +02:00
|
|
|
startTime := time.Now()
|
2024-05-19 22:57:23 +02:00
|
|
|
|
2024-05-24 14:07:16 +02:00
|
|
|
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
|
2024-06-09 21:44:49 +02:00
|
|
|
combinedResults := getTextResultsFromCacheOrFetch(cacheKey, query, safe, lang, page)
|
2024-05-21 08:48:09 +02:00
|
|
|
|
|
|
|
hasPrevPage := page > 1
|
2024-06-09 21:44:49 +02:00
|
|
|
hasNextPage := len(combinedResults) > 0
|
2024-05-21 08:48:09 +02:00
|
|
|
|
|
|
|
displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds(), page, hasPrevPage, hasNextPage)
|
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
// Prefetch next and previous pages
|
|
|
|
go prefetchPage(query, safe, lang, page+1)
|
|
|
|
if hasPrevPage {
|
|
|
|
go prefetchPage(query, safe, lang, page-1)
|
2024-05-21 10:19:40 +02:00
|
|
|
}
|
2024-05-21 08:48:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page int) []TextSearchResult {
|
2024-05-24 14:07:16 +02:00
|
|
|
cacheChan := make(chan []SearchResult)
|
2024-05-20 22:14:48 +02:00
|
|
|
var combinedResults []TextSearchResult
|
2024-05-19 22:57:23 +02:00
|
|
|
|
2024-05-20 22:14:48 +02:00
|
|
|
go func() {
|
|
|
|
results, exists := resultsCache.Get(cacheKey)
|
|
|
|
if exists {
|
|
|
|
log.Println("Cache hit")
|
|
|
|
cacheChan <- results
|
|
|
|
} else {
|
|
|
|
log.Println("Cache miss")
|
|
|
|
cacheChan <- nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2024-05-24 14:07:16 +02:00
|
|
|
case results := <-cacheChan:
|
|
|
|
if results == nil {
|
2024-06-09 21:44:49 +02:00
|
|
|
combinedResults = fetchTextResults(query, safe, lang, page)
|
2024-06-12 14:51:45 +02:00
|
|
|
if len(combinedResults) > 0 {
|
|
|
|
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
|
|
|
}
|
2024-05-24 14:07:16 +02:00
|
|
|
} else {
|
|
|
|
textResults, _, _ := convertToSpecificResults(results)
|
|
|
|
combinedResults = textResults
|
2024-05-20 22:14:48 +02:00
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
log.Println("Cache check timeout")
|
2024-06-09 21:44:49 +02:00
|
|
|
combinedResults = fetchTextResults(query, safe, lang, page)
|
2024-06-12 14:51:45 +02:00
|
|
|
if len(combinedResults) > 0 {
|
|
|
|
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
|
|
|
}
|
2024-05-19 22:57:23 +02:00
|
|
|
}
|
|
|
|
|
2024-05-21 08:48:09 +02:00
|
|
|
return combinedResults
|
|
|
|
}
|
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
func prefetchPage(query, safe, lang string, page int) {
|
2024-05-24 14:07:16 +02:00
|
|
|
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
|
2024-05-21 08:48:09 +02:00
|
|
|
if _, exists := resultsCache.Get(cacheKey); !exists {
|
2024-06-09 21:44:49 +02:00
|
|
|
log.Printf("Page %d not cached, caching now...", page)
|
|
|
|
pageResults := fetchTextResults(query, safe, lang, page)
|
2024-06-12 14:51:45 +02:00
|
|
|
if len(pageResults) > 0 {
|
|
|
|
resultsCache.Set(cacheKey, convertToSearchResults(pageResults))
|
|
|
|
}
|
2024-05-21 08:48:09 +02:00
|
|
|
} else {
|
2024-06-09 21:44:49 +02:00
|
|
|
log.Printf("Page %d already cached", page)
|
2024-05-20 22:14:48 +02:00
|
|
|
}
|
2024-05-21 08:48:09 +02:00
|
|
|
}
|
2024-05-19 22:57:23 +02:00
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
2024-06-12 14:51:45 +02:00
|
|
|
var results []TextSearchResult
|
|
|
|
var err error
|
2024-05-19 22:57:23 +02:00
|
|
|
|
2024-06-12 14:51:45 +02:00
|
|
|
for attempts := 0; attempts < len(searchEngines); attempts++ {
|
|
|
|
engine := selectSearchEngine()
|
|
|
|
log.Printf("Using search engine: %s", engine.Name)
|
|
|
|
|
|
|
|
results, err = engine.Func(query, safe, lang, page)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error performing search with %s: %v", engine.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(results) > 0 {
|
|
|
|
break
|
|
|
|
}
|
2024-05-21 08:48:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
return results
|
2024-05-19 22:57:23 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
func selectSearchEngine() searchEngine {
|
|
|
|
searchEngineLock.Lock()
|
|
|
|
defer searchEngineLock.Unlock()
|
2024-05-17 14:26:28 +02:00
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
totalWeight := 0
|
|
|
|
for _, engine := range searchEngines {
|
|
|
|
totalWeight += engine.Weight
|
|
|
|
}
|
2024-05-17 14:26:28 +02:00
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
randValue := rand.Intn(totalWeight)
|
|
|
|
for _, engine := range searchEngines {
|
|
|
|
if randValue < engine.Weight {
|
|
|
|
// Adjust weights for load balancing
|
|
|
|
for i := range searchEngines {
|
|
|
|
if searchEngines[i].Name == engine.Name {
|
|
|
|
searchEngines[i].Weight = max(1, searchEngines[i].Weight-1)
|
|
|
|
} else {
|
|
|
|
searchEngines[i].Weight++
|
2024-05-18 01:59:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-09 21:44:49 +02:00
|
|
|
return engine
|
|
|
|
}
|
|
|
|
randValue -= engine.Weight
|
2024-05-17 14:26:28 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 21:44:49 +02:00
|
|
|
return searchEngines[0] // fallback to the first engine
|
2024-05-21 08:48:09 +02:00
|
|
|
}
|
|
|
|
|
2024-05-19 22:57:23 +02:00
|
|
|
func displayResults(w http.ResponseWriter, results []TextSearchResult, query, lang string, elapsed float64, page int, hasPrevPage, hasNextPage bool) {
|
2024-05-21 08:48:09 +02:00
|
|
|
log.Printf("Displaying results for page %d", page)
|
|
|
|
log.Printf("Total results: %d", len(results))
|
|
|
|
log.Printf("Has previous page: %t, Has next page: %t", hasPrevPage, hasNextPage)
|
|
|
|
|
2024-05-19 22:57:23 +02:00
|
|
|
tmpl, err := template.New("text.html").Funcs(template.FuncMap{
|
|
|
|
"sub": func(a, b int) int {
|
|
|
|
return a - b
|
|
|
|
},
|
|
|
|
"add": func(a, b int) int {
|
|
|
|
return a + b
|
|
|
|
},
|
|
|
|
}).ParseFiles("templates/text.html")
|
2024-05-17 14:26:28 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := struct {
|
|
|
|
Results []TextSearchResult
|
|
|
|
Query string
|
|
|
|
Fetched string
|
2024-05-19 22:57:23 +02:00
|
|
|
Page int
|
|
|
|
HasPrevPage bool
|
|
|
|
HasNextPage bool
|
2024-05-17 14:26:28 +02:00
|
|
|
LanguageOptions []LanguageOption
|
|
|
|
CurrentLang string
|
|
|
|
}{
|
|
|
|
Results: results,
|
|
|
|
Query: query,
|
2024-05-18 01:59:29 +02:00
|
|
|
Fetched: fmt.Sprintf("%.2f seconds", elapsed),
|
2024-05-19 22:57:23 +02:00
|
|
|
Page: page,
|
|
|
|
HasPrevPage: hasPrevPage,
|
|
|
|
HasNextPage: hasNextPage,
|
2024-05-17 14:26:28 +02:00
|
|
|
LanguageOptions: languageOptions,
|
|
|
|
CurrentLang: lang,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmpl.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
}
|
2024-05-16 18:29:26 +02:00
|
|
|
}
|