summaryrefslogtreecommitdiff
path: root/main.go
blob: f0282e3a8a21b997ba7726b340400ea4d02db9b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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)
}