summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authornavanchauhan <navanchauhan@gmail.com>2022-10-17 17:21:54 -0600
committernavanchauhan <navanchauhan@gmail.com>2022-10-17 17:21:54 -0600
commit2e2943c27176d8ddbfbc551632e4f9d0e86cb41e (patch)
tree8ae108d2571ce9696735980ee59fda5e934ec644 /main.go
parentfd084a6b6a0fd586c5aa8b8895919ab19c5ec32b (diff)
reorganised code
Diffstat (limited to 'main.go')
-rw-r--r--main.go174
1 files changed, 18 insertions, 156 deletions
diff --git a/main.go b/main.go
index f0c0a91..c88f2b9 100644
--- a/main.go
+++ b/main.go
@@ -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) {