From bec4e596415c623d7294ec87680bc029e407fc23 Mon Sep 17 00:00:00 2001 From: navanchauhan Date: Mon, 19 Sep 2022 18:19:17 -0600 Subject: initial v0.1 --- main.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 main.go (limited to 'main.go') diff --git a/main.go b/main.go new file mode 100644 index 0000000..f0282e3 --- /dev/null +++ b/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "html/template" + "net/http" + "os" + + "github.com/joho/godotenv" + "github.com/meilisearch/meilisearch-go" +) + +type SearchQuery struct { + Query string + MaxResults int64 +} + +type SearchResponse struct { + Success bool + SearchResults []interface{} +} + +func main() { + err := godotenv.Load(".env") + if err != nil { + fmt.Println("Error loading .env file") + os.Exit(1) + } + meili_host, ok := os.LookupEnv("MEILI_HOST") + if !ok { + fmt.Println("Error loading MEILI_HOST from .env file") + os.Exit(1) + } + + client := meilisearch.NewClient(meilisearch.ClientConfig{ + Host: meili_host, + }) + + index := client.Index("fda510k") + + tmpl := template.Must(template.ParseFiles("index.html")) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + tmpl.Execute(w, nil) + return + } + query := SearchQuery{ + Query: r.FormValue("query"), + MaxResults: 100, + } + + res, err := index.Search(query.Query, &meilisearch.SearchRequest{ + Limit: query.MaxResults, + }) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + fmt.Println(res.Hits) + + tmpl.Execute(w, SearchResponse{Success: true, SearchResults: res.Hits}) + }) + + fmt.Println("Listening on port 8080") + http.ListenAndServe(":8080", nil) +} -- cgit v1.2.3