fixed search if "type" parameter is missing
This commit is contained in:
parent
bc85d7d4a2
commit
a0e5864677
1 changed files with 36 additions and 35 deletions
71
main.go
71
main.go
|
@ -77,38 +77,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSearch(w http.ResponseWriter, r *http.Request) {
|
func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
var query, safe, lang, searchType string
|
query, safe, lang, searchType, page := parseSearchParams(r)
|
||||||
var page int
|
|
||||||
|
|
||||||
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")
|
|
||||||
pageStr := r.URL.Query().Get("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
|
|
||||||
}
|
|
||||||
} else if r.Method == "POST" {
|
|
||||||
query = r.FormValue("q")
|
|
||||||
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 == "" {
|
if query == "" {
|
||||||
http.ServeFile(w, r, "templates/search.html")
|
http.ServeFile(w, r, "templates/search.html")
|
||||||
|
@ -116,15 +85,47 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch searchType {
|
switch searchType {
|
||||||
case "text":
|
|
||||||
HandleTextSearch(w, query, safe, lang, page)
|
|
||||||
case "image":
|
case "image":
|
||||||
handleImageSearch(w, query, safe, lang, page)
|
handleImageSearch(w, query, safe, lang, page)
|
||||||
case "video":
|
case "video":
|
||||||
videoSearchEndpointHandler(w, r)
|
videoSearchEndpointHandler(w, r)
|
||||||
case "map":
|
case "map":
|
||||||
handleMapSearch(w, query, safe)
|
handleMapSearch(w, query, safe)
|
||||||
|
case "text":
|
||||||
|
fallthrough
|
||||||
default:
|
default:
|
||||||
http.ServeFile(w, r, "templates/search.html")
|
HandleTextSearch(w, query, safe, lang, page)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseSearchParams(r *http.Request) (query, safe, lang, searchType string, page int) {
|
||||||
|
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")
|
||||||
|
pageStr := r.URL.Query().Get("p")
|
||||||
|
page = parsePageParameter(pageStr)
|
||||||
|
} else if r.Method == "POST" {
|
||||||
|
query = r.FormValue("q")
|
||||||
|
safe = r.FormValue("safe")
|
||||||
|
lang = r.FormValue("lang")
|
||||||
|
searchType = r.FormValue("t")
|
||||||
|
pageStr := r.FormValue("p")
|
||||||
|
page = parsePageParameter(pageStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if searchType == "" {
|
||||||
|
searchType = "text"
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func parsePageParameter(pageStr string) int {
|
||||||
|
page, err := strconv.Atoi(pageStr)
|
||||||
|
if err != nil || page < 1 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
return page
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue