diff options
author | navanchauhan <navanchauhan@gmail.com> | 2022-10-16 16:46:48 -0600 |
---|---|---|
committer | navanchauhan <navanchauhan@gmail.com> | 2022-10-16 16:46:48 -0600 |
commit | 83b48f3e8833c2f693df04ef58ca57afa76dc2fa (patch) | |
tree | 4d6b93848a33c7d1e12a49306e87a8d938486b93 /main.go | |
parent | 47c4143081176b08af8793524287710a9a0e7290 (diff) |
new ui
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 69 |
1 files changed, 69 insertions, 0 deletions
@@ -57,6 +57,11 @@ func main() { t.Execute(w, nil) }) + http.HandleFunc("/2.0/", func(w http.ResponseWriter, r *http.Request) { + t, _ := template.ParseFiles("templates/home.html") + t.Execute(w, nil) + }) + funcMap := template.FuncMap{ "unescapeHTML": func(s string) template.HTML { return template.HTML(s) @@ -66,11 +71,75 @@ func main() { //searchResTemplate := template.Must(template.ParseFiles("results.gtpl")) searchResTemplate := template.Must(template.New("results.gtpl").Funcs(funcMap).ParseFiles("results.gtpl")) + // v2.0 UI + searchResultsTemplate2 := template.Must(template.New("search_results.html").Funcs(funcMap).ParseFiles("templates/search_results.html")) + if err != nil { fmt.Println("Error parsing template") os.Exit(1) } + http.HandleFunc("/2.0/search", func(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + fmt.Println(r.Form) + if r.Form["query"] != nil || r.FormValue("query") != "" { + fmt.Println("query:", r.Form["query"]) + var myOffset int64 + if r.Form["offset"] != nil { + offset, _ := strconv.ParseInt(r.FormValue("offset"), 10, 64) + myOffset = offset + if offset < 0 { + myOffset = 0 + } + } else { + offset := int64(0) + myOffset = offset + } + query := SearchQuery{ + Query: r.FormValue("query"), + MaxResults: 100, + Offset: myOffset, + } + + res, err := index.Search(query.Query, &meilisearch.SearchRequest{ + Limit: query.MaxResults, + Offset: query.Offset, + AttributesToRetrieve: []string{ + "title", + "applicant", + "submission_date", + "predicates", + "id", + }, + AttributesToCrop: []string{"full_text"}, + AttributesToHighlight: []string{"full_text"}, + HighlightPreTag: "<mark>", + HighlightPostTag: "</mark>", + }) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + numPages := pageCount(int(res.EstimatedTotalHits), int(query.MaxResults)) + + searchResultsTemplate2.Execute(w, SearchResponse{ + Success: true, + SearchResults: res.Hits, + NumResults: len(res.Hits) + int(query.Offset), + TotalResults: res.EstimatedTotalHits, + MoreResults: res.EstimatedTotalHits > query.MaxResults, + OriginalQuery: query, + Offset: query.Offset + query.MaxResults, + LastOffset: query.Offset - query.MaxResults, + NumPages: numPages, + }) + } else { + fmt.Println("query is empty") + } + }) + http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { r.ParseForm() fmt.Println(r.Form) |