diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..f2f9c19 --- /dev/null +++ b/cache.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "sync" +) + +// TextSearchResult represents a single search result item. +type TextSearchResult struct { + URL string + Header string + Description string + Source string +} + +// CacheKey represents the key used to store search results in the cache. +type CacheKey struct { + Query string + Page int + Safe string + Lang string +} + +// ResultsCache is a thread-safe map for caching search results by composite keys. +type ResultsCache struct { + mu sync.Mutex + results map[string][]TextSearchResult +} + +// NewResultsCache creates a new ResultsCache. +func NewResultsCache() *ResultsCache { + return &ResultsCache{ + results: make(map[string][]TextSearchResult), + } +} + +// Get retrieves the results for a given key from the cache. +func (rc *ResultsCache) Get(key CacheKey) ([]TextSearchResult, bool) { + rc.mu.Lock() + defer rc.mu.Unlock() + results, exists := rc.results[rc.keyToString(key)] + return results, exists +} + +// Set stores the results for a given key in the cache. +func (rc *ResultsCache) Set(key CacheKey, results []TextSearchResult) { + rc.mu.Lock() + defer rc.mu.Unlock() + rc.results[rc.keyToString(key)] = results +} + +// keyToString converts a CacheKey to a string representation. +func (rc *ResultsCache) keyToString(key CacheKey) string { + return fmt.Sprintf("%s|%d|%s|%s", key.Query, key.Page, key.Safe, key.Lang) +} diff --git a/images.go b/images.go index aa4b471..a588348 100644 --- a/images.go +++ b/images.go @@ -56,9 +56,9 @@ func fetchImageResults(query string, safe, lang string, page int) ([]ImageSearch offset = (page - 1) * resultsPerPage } - // Ensuring safe search is enabled by default if not specified + // Ensuring safe search is disabled by default if not specified if safe == "" { - safe = "1" + safe = "0" } // Defaulting to English Canada locale if not specified @@ -66,8 +66,7 @@ func fetchImageResults(query string, safe, lang string, page int) ([]ImageSearch lang = "en_CA" } - // Format &lang=lang_de is incorret, implement fix ! - + // Format &lang=lang_de is incorrect, implement fix ! apiURL := fmt.Sprintf("https://api.qwant.com/v3/search/images?t=images&q=%s&count=%d&locale=%s&offset=%d&device=desktop&tgp=2&safesearch=%s", url.QueryEscape(query), resultsPerPage, diff --git a/main.go b/main.go index 6ced187..8fe2a0a 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +// main.go package main import ( @@ -88,6 +89,9 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { var err error page, err = strconv.Atoi(pageStr) if err != nil || page < 1 { + if debugMode { + log.Printf("Invalid page parameter: %v, defaulting to page 1", err) + } page = 1 // Default to page 1 if no valid page is specified } } else if r.Method == "POST" { @@ -95,6 +99,15 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { safe = r.FormValue("safe") lang = r.FormValue("lang") searchType = r.FormValue("t") + pageStr := r.FormValue("p") + var err error + page, err = strconv.Atoi(pageStr) + if err != nil || page < 1 { + if debugMode { + log.Printf("Invalid page parameter: %v, defaulting to page 1", err) + } + page = 1 // Default to page 1 if no valid page is specified + } } if query == "" { @@ -104,7 +117,7 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { switch searchType { case "text": - HandleTextSearch(w, query, safe, lang) + HandleTextSearch(w, query, safe, lang, page) case "image": handleImageSearch(w, query, safe, lang, page) case "video": diff --git a/run.sh b/run.sh index ede2f3e..5758a55 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,3 @@ #!/bin/bash -go run main.go text-google.go images.go imageproxy.go video.go map.go text.go text-quant.go text-duckduckgo.go --debug \ No newline at end of file +go run main.go text-google.go images.go imageproxy.go video.go map.go text.go text-quant.go text-duckduckgo.go cache.go --debug \ No newline at end of file diff --git a/templates/text.html b/templates/text.html index 4a5749d..5f1a093 100644 --- a/templates/text.html +++ b/templates/text.html @@ -10,15 +10,15 @@

Ocásek

- - - -
+ + + +
-
+
@@ -42,7 +42,6 @@
-
@@ -58,7 +57,6 @@
- {{if .Results}} {{range .Results}}
@@ -72,7 +70,18 @@
No results found for '{{ .Query }}'. Try different keywords.
{{end}}
- +
+
+ + + {{ if .HasPrevPage }} + + {{ end }} + {{ if .HasNextPage }} + + {{ end }} +
+