diff options
-rw-r--r-- | definitions.go | 34 | ||||
-rw-r--r-- | handlerFuncs.go | 119 | ||||
-rw-r--r-- | main.go | 174 |
3 files changed, 171 insertions, 156 deletions
diff --git a/definitions.go b/definitions.go new file mode 100644 index 0000000..2289865 --- /dev/null +++ b/definitions.go @@ -0,0 +1,34 @@ +package main + +type SearchQuery struct { + Query string + MaxResults int64 + Offset int64 +} + +type BaseResponse struct { + GlobalVars GlobalVars +} + +type SearchResponse struct { + GlobalVars GlobalVars + Success bool + SearchResults []interface{} + NumResults int + TotalResults int64 + MoreResults bool + OriginalQuery SearchQuery + Offset int64 + LastOffset int64 + NumPages int +} + +type DocumentResponse struct { + GlobalVars GlobalVars + SearchResults interface{} + SummaryPDF string +} + +type GlobalVars struct { + Name string +} diff --git a/handlerFuncs.go b/handlerFuncs.go new file mode 100644 index 0000000..78a498e --- /dev/null +++ b/handlerFuncs.go @@ -0,0 +1,119 @@ +package main + +import ( + "fmt" + "html/template" + "net/http" + "strconv" + + "github.com/meilisearch/meilisearch-go" +) + +func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.Index, searchTemplate *template.Template) { + 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)) + + searchTemplate.Execute(w, SearchResponse{ + GlobalVars: globalVariables, + 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") + } +} + +func documentHandler510k(w http.ResponseWriter, r *http.Request, index *meilisearch.Index, template *template.Template) { + r.ParseForm() + fmt.Println(r.Form) + var res interface{} + var documentID string = r.FormValue("id") + if r.Form["id"] != nil { + index.GetDocument(documentID, &meilisearch.DocumentQuery{ + Fields: []string{ + "title", + "applicant", + "decision", + "decision_date", + "full_text", + "id", + "predicates", + "submission_date", + "contact", + "STREET1", + "STREET2", + "CITY", + "STATE", + "ZIP", + "COUNTRY_CODE", + "postal_code", + "REVIEWADVISECOMM", + "PRODUCTCODE", + "STATEORSUMM", + "CLASSADVISECOMM", + "SSPINDICATOR", + "TYPE", + "THIRDPARTY", + "EXPEDITEDREVIEW", + }}, &res) + fmt.Println(res) + var year = documentID[1:3] + + template.Execute(w, DocumentResponse{ + GlobalVars: globalVariables, + SearchResults: res, + SummaryPDF: create_pdf_url(year, documentID), + }) + } else { + fmt.Println("No ID provided") + } + +} @@ -12,39 +12,6 @@ import ( "github.com/meilisearch/meilisearch-go" ) -type SearchQuery struct { - Query string - MaxResults int64 - Offset int64 -} - -type BaseResponse struct { - GlobalVars GlobalVars -} - -type SearchResponse struct { - GlobalVars GlobalVars - Success bool - SearchResults []interface{} - NumResults int - TotalResults int64 - MoreResults bool - OriginalQuery SearchQuery - Offset int64 - LastOffset int64 - NumPages int -} - -type DocumentResponse struct { - GlobalVars GlobalVars - SearchResults interface{} - SummaryPDF string -} - -type GlobalVars struct { - Name string -} - var globalVariables = GlobalVars{ Name: "510K Search", } @@ -58,68 +25,6 @@ func create_pdf_url(year string, knumber string) string { } } -func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.Index, searchTemplate *template.Template) { - 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)) - - searchTemplate.Execute(w, SearchResponse{ - GlobalVars: globalVariables, - 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") - } -} - func pageCount(total int, perPage int) int { return int(math.Ceil(float64(total) / float64(perPage))) } @@ -140,80 +45,37 @@ func main() { Host: meili_host, }) + funcMap := template.FuncMap{ + "unescapeHTML": func(s string) template.HTML { + return template.HTML(s) + }, + } + + // Classic UI Templates + classicIndexTemplate := template.Must(template.ParseFiles("templates/search.gtpl")) + searchResTemplate := template.Must(template.New("results.gtpl").Funcs(funcMap).ParseFiles("templates/results.gtpl")) + + // v2.0 UI Templates + indexTemplate := template.Must(template.ParseFiles("templates/home.html")) + searchResultsTemplate2 := template.Must(template.New("search_results.html").Funcs(funcMap).ParseFiles("templates/search_results.html")) + documentDetailsTemplate2 := template.Must(template.ParseFiles("templates/document_details.html")) + index := client.Index("510k") http.HandleFunc("/classic/", func(w http.ResponseWriter, r *http.Request) { - t, _ := template.ParseFiles("templates/search.gtpl") - t.Execute(w, BaseResponse{ + classicIndexTemplate.Execute(w, BaseResponse{ GlobalVars: globalVariables, }) }) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - t, _ := template.ParseFiles("templates/home.html") - t.Execute(w, BaseResponse{ + indexTemplate.Execute(w, BaseResponse{ GlobalVars: globalVariables, }) }) - funcMap := template.FuncMap{ - "unescapeHTML": func(s string) template.HTML { - return template.HTML(s) - }, - } - - // Classic UI - searchResTemplate := template.Must(template.New("results.gtpl").Funcs(funcMap).ParseFiles("templates/results.gtpl")) - - // v2.0 UI - searchResultsTemplate2 := template.Must(template.New("search_results.html").Funcs(funcMap).ParseFiles("templates/search_results.html")) - documentDetailsTemplate2 := template.Must(template.ParseFiles("templates/document_details.html")) - http.HandleFunc("/dbentry", func(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - fmt.Println(r.Form) - var res interface{} - var documentID string = r.FormValue("id") - if r.Form["id"] != nil { - index.GetDocument(documentID, &meilisearch.DocumentQuery{ - Fields: []string{ - "title", - "applicant", - "decision", - "decision_date", - "full_text", - "id", - "predicates", - "submission_date", - "contact", - "STREET1", - "STREET2", - "CITY", - "STATE", - "ZIP", - "COUNTRY_CODE", - "postal_code", - "REVIEWADVISECOMM", - "PRODUCTCODE", - "STATEORSUMM", - "CLASSADVISECOMM", - "SSPINDICATOR", - "TYPE", - "THIRDPARTY", - "EXPEDITEDREVIEW", - }}, &res) - fmt.Println(res) - var year = documentID[1:3] - - documentDetailsTemplate2.Execute(w, DocumentResponse{ - GlobalVars: globalVariables, - SearchResults: res, - SummaryPDF: create_pdf_url(year, documentID), - }) - } else { - fmt.Println("No ID provided") - } - + documentHandler510k(w, r, index, documentDetailsTemplate2) }) http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { |