From 83b48f3e8833c2f693df04ef58ca57afa76dc2fa Mon Sep 17 00:00:00 2001 From: navanchauhan Date: Sun, 16 Oct 2022 16:46:48 -0600 Subject: new ui --- main.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'main.go') diff --git a/main.go b/main.go index 6302b6a..a41f774 100644 --- a/main.go +++ b/main.go @@ -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: "", + HighlightPostTag: "", + }) + + 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) -- cgit v1.2.3