updated logging, added theming, added settings button
This commit is contained in:
parent
51b2ef43bc
commit
ca15fb7ec5
25 changed files with 384 additions and 62 deletions
25
text.go
25
text.go
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
@ -20,7 +19,7 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int) {
|
||||
func HandleTextSearch(w http.ResponseWriter, settings UserSettings, query, safe, lang string, page int) {
|
||||
startTime := time.Now()
|
||||
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
|
||||
|
@ -39,7 +38,7 @@ func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int)
|
|||
elapsedTime := time.Since(startTime)
|
||||
tmpl, err := template.New("text.html").Funcs(funcs).ParseFiles("templates/text.html")
|
||||
if err != nil {
|
||||
log.Printf("Error parsing template: %v", err)
|
||||
printErr("Error parsing template: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
@ -54,6 +53,7 @@ func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int)
|
|||
HasPrevPage bool
|
||||
HasNextPage bool
|
||||
NoResults bool
|
||||
Theme string
|
||||
}{
|
||||
Results: combinedResults,
|
||||
Query: query,
|
||||
|
@ -64,11 +64,12 @@ func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int)
|
|||
HasPrevPage: page > 1,
|
||||
HasNextPage: len(combinedResults) >= 50,
|
||||
NoResults: len(combinedResults) == 0,
|
||||
Theme: settings.Theme,
|
||||
}
|
||||
|
||||
err = tmpl.Execute(w, data)
|
||||
if err != nil {
|
||||
log.Printf("Error executing template: %v", err)
|
||||
printErr("Error executing template: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
@ -80,10 +81,10 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
|
|||
go func() {
|
||||
results, exists := resultsCache.Get(cacheKey)
|
||||
if exists {
|
||||
log.Println("Cache hit")
|
||||
printInfo("Cache hit")
|
||||
cacheChan <- results
|
||||
} else {
|
||||
log.Println("Cache miss")
|
||||
printInfo("Cache miss")
|
||||
cacheChan <- nil
|
||||
}
|
||||
}()
|
||||
|
@ -100,7 +101,7 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
|
|||
combinedResults = textResults
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
log.Println("Cache check timeout")
|
||||
printInfo("Cache check timeout")
|
||||
combinedResults = fetchTextResults(query, safe, lang, page)
|
||||
if len(combinedResults) > 0 {
|
||||
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
||||
|
@ -113,13 +114,13 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
|
|||
func prefetchPage(query, safe, lang string, page int) {
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
|
||||
if _, exists := resultsCache.Get(cacheKey); !exists {
|
||||
log.Printf("Page %d not cached, caching now...", page)
|
||||
printInfo("Page %d not cached, caching now...", page)
|
||||
pageResults := fetchTextResults(query, safe, lang, page)
|
||||
if len(pageResults) > 0 {
|
||||
resultsCache.Set(cacheKey, convertToSearchResults(pageResults))
|
||||
}
|
||||
} else {
|
||||
log.Printf("Page %d already cached", page)
|
||||
printInfo("Page %d already cached", page)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,12 +128,12 @@ func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
|||
var results []TextSearchResult
|
||||
|
||||
for _, engine := range textSearchEngines {
|
||||
log.Printf("Using search engine: %s", engine.Name)
|
||||
printInfo("Using search engine: %s", engine.Name)
|
||||
|
||||
searchResults, duration, err := engine.Func(query, safe, lang, page)
|
||||
updateEngineMetrics(&engine, duration, err == nil)
|
||||
if err != nil {
|
||||
log.Printf("Error performing search with %s: %v", engine.Name, err)
|
||||
printWarn("Error performing search with %s: %v", engine.Name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -146,7 +147,7 @@ func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
|||
|
||||
// If no results found after trying all engines
|
||||
if len(results) == 0 {
|
||||
log.Printf("No text results found for query: %s, trying other nodes", query)
|
||||
printWarn("No text results found for query: %s, trying other nodes", query)
|
||||
results = tryOtherNodesForTextSearch(query, safe, lang, page, []string{hostID})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue