Search/main.go

117 lines
4 KiB
Go

package main
import (
"fmt"
"log"
"net/http"
"strconv"
)
// LanguageOption represents a language option for search
type LanguageOption struct {
Code string
Name string
}
var languageOptions = []LanguageOption{
{Code: "", Name: "Any Language"},
{Code: "lang_en", Name: "English"},
{Code: "lang_af", Name: "Afrikaans"},
{Code: "lang_ar", Name: "العربية (Arabic)"},
{Code: "lang_hy", Name: "Հայերեն (Armenian)"},
{Code: "lang_be", Name: "Беларуская (Belarusian)"},
{Code: "lang_bg", Name: "български (Bulgarian)"},
{Code: "lang_ca", Name: "Català (Catalan)"},
{Code: "lang_zh-CN", Name: "中文 (简体) (Chinese Simplified)"},
{Code: "lang_zh-TW", Name: "中文 (繁體) (Chinese Traditional)"},
{Code: "lang_hr", Name: "Hrvatski (Croatian)"},
{Code: "lang_cs", Name: "Čeština (Czech)"},
{Code: "lang_da", Name: "Dansk (Danish)"},
{Code: "lang_nl", Name: "Nederlands (Dutch)"},
{Code: "lang_eo", Name: "Esperanto"},
{Code: "lang_et", Name: "Eesti (Estonian)"},
{Code: "lang_tl", Name: "Filipino (Tagalog)"},
{Code: "lang_fi", Name: "Suomi (Finnish)"},
{Code: "lang_fr", Name: "Français (French)"},
{Code: "lang_de", Name: "Deutsch (German)"},
{Code: "lang_el", Name: "Ελληνικά (Greek)"},
{Code: "lang_iw", Name: "עברית (Hebrew)"},
{Code: "lang_hi", Name: "हिन्दी (Hindi)"},
{Code: "lang_hu", Name: "magyar (Hungarian)"},
{Code: "lang_is", Name: "íslenska (Icelandic)"},
{Code: "lang_id", Name: "Bahasa Indonesia (Indonesian)"},
{Code: "lang_it", Name: "italiano (Italian)"},
{Code: "lang_ja", Name: "日本語 (Japanese)"},
{Code: "lang_ko", Name: "한국어 (Korean)"},
{Code: "lang_lv", Name: "latviešu (Latvian)"},
{Code: "lang_lt", Name: "lietuvių (Lithuanian)"},
{Code: "lang_no", Name: "norsk (Norwegian)"},
{Code: "lang_fa", Name: "فارسی (Persian)"},
{Code: "lang_pl", Name: "polski (Polish)"},
{Code: "lang_pt", Name: "português (Portuguese)"},
{Code: "lang_ro", Name: "română (Romanian)"},
{Code: "lang_ru", Name: "русский (Russian)"},
{Code: "lang_sr", Name: "српски (Serbian)"},
{Code: "lang_sk", Name: "slovenčina (Slovak)"},
{Code: "lang_sl", Name: "slovenščina (Slovenian)"},
{Code: "lang_es", Name: "español (Spanish)"},
{Code: "lang_sw", Name: "Kiswahili (Swahili)"},
{Code: "lang_sv", Name: "svenska (Swedish)"},
{Code: "lang_th", Name: "ไทย (Thai)"},
{Code: "lang_tr", Name: "Türkçe (Turkish)"},
{Code: "lang_uk", Name: "українська (Ukrainian)"},
{Code: "lang_vi", Name: "Tiếng Việt (Vietnamese)"},
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", handleSearch)
http.HandleFunc("/search", handleSearch)
http.HandleFunc("/img_proxy", handleImageProxy)
http.HandleFunc("/settings", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "templates/settings.html")
})
fmt.Println("Server is listening on http://localhost:5000")
log.Fatal(http.ListenAndServe(":5000", nil))
}
func handleSearch(w http.ResponseWriter, r *http.Request) {
var query, safe, lang, searchType string
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 {
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")
}
if query == "" {
http.ServeFile(w, r, "templates/search.html")
return
}
switch searchType {
case "text":
handleTextSearch(w, query, safe, lang)
case "image":
handleImageSearch(w, query, safe, lang, page)
case "video":
videoSearchEndpointHandler(w, r)
case "map":
handleMapSearch(w, query, safe)
default:
http.ServeFile(w, r, "templates/search.html")
}
}