summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go69
1 files changed, 69 insertions, 0 deletions
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: "<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)