added image search (website style wip)
This commit is contained in:
parent
6f25a28d5c
commit
7ae56f4e21
5 changed files with 207 additions and 114 deletions
54
main.go
54
main.go
|
@ -2,12 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LanguageOption represents a language option for search
|
||||
|
@ -66,12 +62,12 @@ var languageOptions = []LanguageOption{
|
|||
{Code: "lang_vi", Name: "Tiếng Việt (Vietnamese)"},
|
||||
}
|
||||
|
||||
var funcs = template.FuncMap{
|
||||
"title": func(s string) string { return strings.Title(s) },
|
||||
"url_for": func(filename string) string { return "/" + filename },
|
||||
}
|
||||
// var funcs = template.FuncMap{
|
||||
// "title": func(s string) string { return strings.Title(s) },
|
||||
// "url_for": func(filename string) string { return "/" + filename },
|
||||
// }
|
||||
|
||||
var templates = template.Must(template.New("").Funcs(funcs).ParseFiles("templates/results.html"))
|
||||
// var templates = template.Must(template.New("").Funcs(funcs).ParseFiles("templates/results.html"))
|
||||
|
||||
func main() {
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
|
@ -82,16 +78,18 @@ func main() {
|
|||
}
|
||||
|
||||
func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
var query, safe, lang string
|
||||
var query, safe, lang, searchType string
|
||||
|
||||
if r.Method == "GET" {
|
||||
query = r.URL.Query().Get("q")
|
||||
safe = r.URL.Query().Get("safe")
|
||||
lang = r.URL.Query().Get("lang")
|
||||
searchType = r.URL.Query().Get("t")
|
||||
} else if r.Method == "POST" {
|
||||
query = r.FormValue("q")
|
||||
safe = r.FormValue("safe")
|
||||
lang = r.FormValue("lang")
|
||||
searchType = r.FormValue("t")
|
||||
}
|
||||
|
||||
if query == "" {
|
||||
|
@ -99,34 +97,12 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
start := time.Now() // This line is correctly placed
|
||||
results, err := PerformTextSearch(query, safe, lang)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to fetch search results", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
elapsed := time.Since(start) // Correctly captures the elapsed time after search
|
||||
fetched := fmt.Sprintf("Fetched the results in %.2f seconds", elapsed.Seconds())
|
||||
|
||||
data := struct {
|
||||
Results []SearchResult
|
||||
Query string
|
||||
Fetched string
|
||||
ElapsedTime string
|
||||
LanguageOptions []LanguageOption
|
||||
CurrentLang string
|
||||
}{
|
||||
Results: results,
|
||||
Query: query,
|
||||
Fetched: fetched,
|
||||
ElapsedTime: strconv.FormatFloat(time.Since(start).Seconds(), 'f', 2, 64),
|
||||
LanguageOptions: languageOptions,
|
||||
CurrentLang: lang,
|
||||
}
|
||||
|
||||
err = templates.ExecuteTemplate(w, "results.html", data)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to render template", http.StatusInternalServerError)
|
||||
return
|
||||
switch searchType {
|
||||
case "text":
|
||||
handleTextSearch(w, r, query, safe, lang) // Handles fetching and rendering text search results
|
||||
case "image":
|
||||
handleImageSearch(w, r, query) // Handles fetching and rendering image search results
|
||||
default:
|
||||
http.ServeFile(w, r, "static/search.html")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue