summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authornavanchauhan <navanchauhan@gmail.com>2022-10-17 17:08:23 -0600
committernavanchauhan <navanchauhan@gmail.com>2022-10-17 17:08:23 -0600
commitfd084a6b6a0fd586c5aa8b8895919ab19c5ec32b (patch)
tree80d3abe4836501bb46f60cf4781caa72aba97f75 /main.go
parent684cb35dfb982aaa89e7b91601c11ad8aec59c92 (diff)
split into handler functions
Diffstat (limited to 'main.go')
-rw-r--r--main.go224
1 files changed, 99 insertions, 125 deletions
diff --git a/main.go b/main.go
index 1c2b8f6..f0c0a91 100644
--- a/main.go
+++ b/main.go
@@ -18,7 +18,12 @@ type SearchQuery struct {
Offset int64
}
+type BaseResponse struct {
+ GlobalVars GlobalVars
+}
+
type SearchResponse struct {
+ GlobalVars GlobalVars
Success bool
SearchResults []interface{}
NumResults int
@@ -31,7 +36,88 @@ type SearchResponse struct {
}
type DocumentResponse struct {
+ GlobalVars GlobalVars
SearchResults interface{}
+ SummaryPDF string
+}
+
+type GlobalVars struct {
+ Name string
+}
+
+var globalVariables = GlobalVars{
+ Name: "510K Search",
+}
+
+func create_pdf_url(year string, knumber string) string {
+ year_int, _ := strconv.Atoi(year[0:2])
+ if year[0] == '0' || year[0] == '1' || year[0] == '2' || year[0] == '3' || year[0] == '4' || year[0] == '5' && year[0:2] != "01" && year[0:2] != "00" {
+ return fmt.Sprintf("https://www.accessdata.fda.gov/cdrh_docs/pdf%d/%s.pdf", year_int, knumber)
+ } else {
+ return fmt.Sprintf("https://www.accessdata.fda.gov/cdrh_docs/pdf/%s.pdf", knumber)
+ }
+}
+
+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 {
@@ -58,12 +144,16 @@ func main() {
http.HandleFunc("/classic/", func(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/search.gtpl")
- t.Execute(w, nil)
+ t.Execute(w, BaseResponse{
+ GlobalVars: globalVariables,
+ })
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/home.html")
- t.Execute(w, nil)
+ t.Execute(w, BaseResponse{
+ GlobalVars: globalVariables,
+ })
})
funcMap := template.FuncMap{
@@ -72,19 +162,13 @@ func main() {
},
}
- //searchResTemplate := template.Must(template.ParseFiles("results.gtpl"))
+ // 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.New("document_details.html").Funcs(funcMap).ParseFiles("templates/document_details.html"))
documentDetailsTemplate2 := template.Must(template.ParseFiles("templates/document_details.html"))
- if err != nil {
- fmt.Println("Error parsing template")
- os.Exit(1)
- }
-
http.HandleFunc("/dbentry", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Println(r.Form)
@@ -119,8 +203,12 @@ func main() {
"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")
@@ -129,125 +217,11 @@ func main() {
})
http.HandleFunc("/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")
- }
+ searchHandler(w, r, index, searchResultsTemplate2)
})
http.HandleFunc("/classic/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))
-
- searchResTemplate.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")
- }
+ searchHandler(w, r, index, searchResTemplate)
})
fmt.Println("Listening on port 8080")