diff options
-rw-r--r-- | main.go | 69 | ||||
-rw-r--r-- | templates/home.html | 38 | ||||
-rw-r--r-- | templates/search_results.html | 73 |
3 files changed, 180 insertions, 0 deletions
@@ -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) diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..35e3ff6 --- /dev/null +++ b/templates/home.html @@ -0,0 +1,38 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>DogeKnows!</title> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css"> + <script src="https://kit.fontawesome.com/32c297b57b.js" crossorigin="anonymous"></script> + </head> + <body> + <section class="hero is-medium is-primary"> + <div class="hero-body"> + <h1 class="title"> + DogeKnows + </h1> + <p class="subtitle"> + Full-text search FDA 510(k) database + </p> + </div> + </section> + <section class="section"> + <div class="container"> + <form action="/2.0/search" method="GET"> + <div class="field has-addons"> + <div class="control is-expanded has-icons-left"> + <input class="input is-large" type="text" name="query" value="{{.OriginalQuery.Query}}" placeholder="Search Query" spellcheck="false"> + <span class="icon is-small is-left"> + <i class="fa-regular fa-magnifying-glass"></i> + </span> + </div> + <div class="control"> + <input class="button is-primary is-large" type="submit" value="Search"> + </div> + </div> + </form> + </div> + </body> +</html>
\ No newline at end of file diff --git a/templates/search_results.html b/templates/search_results.html new file mode 100644 index 0000000..d6b65e4 --- /dev/null +++ b/templates/search_results.html @@ -0,0 +1,73 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>{{.OriginalQuery.Query}} - DogeKnows</title> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css"> + <script src="https://kit.fontawesome.com/32c297b57b.js" crossorigin="anonymous"></script> + </head> + <body> + <section class="hero is-small is-primary"> + <div class="hero-body"> + <h1 class="title"> + DogeKnows + </h1> + <p class="subtitle"> + Full-text search FDA 510(k) database + </p> + </div> + </section> + <section class="section"> + <div class="container"> + <div class="container"> + <form action="/2.0/search" method="GET"> + <div class="field has-addons"> + <div class="control is-expanded has-icons-left"> + <input class="input" type="text" name="query" value="{{.OriginalQuery.Query}}" placeholder="Search Query" spellcheck="false"> + <span class="icon is-left"> + <i class="fa-regular fa-magnifying-glass"></i> + </span> + </div> + <div class="control"> + <input class="button is-primary" type="submit" value="Search"> + </div> + </div> + </form> + </div> +<p>Showing {{.NumResults}} of {{.TotalResults}} (Estimated) </p> +</div> +</section> +<section class="section"> + <div class="container"> + <table class="table"> + <tr> + <th>510(k) Number</th> + <th>Title</th> + <th>Applicant</th> + <th>Hit Details</th> + <th>Submission Date</th> + <th>Predicates</th> + </tr> + {{ range .SearchResults }} + <tr> + <td><a href="https://www.accessdata.fda.gov/scripts/cdrh/cfdocs/cfPMN/pmn.cfm?ID={{.id}}">{{ .id }}</a></td> + <td>{{ .title }}</td> + <td>{{ .applicant }}</td> + <td>{{unescapeHTML ._formatted.full_text}}</td> + <td>{{ .submission_date }}</td> + <td>{{ range .predicates}} + <a href="https://www.accessdata.fda.gov/scripts/cdrh/cfdocs/cfPMN/pmn.cfm?ID={{.}}">{{ . }}</a>, + {{ end }} + </td> + </tr> + {{ end }} + </table> +</div> +</section> + {{ if .MoreResults }} + <a href="/2.0/search?query={{.OriginalQuery.Query}}&offset={{.LastOffset}}"> <p>Previous Page</p></a> + <a href="/2.0/search?query={{.OriginalQuery.Query}}&offset={{.Offset}}"> <p>Next Page</p></a> + {{ end }} +</body> +</html>
\ No newline at end of file |