-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (62 loc) · 1.92 KB
/
main.go
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
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"log"
"net/http"
"strings"
"github.com/ajwallacemusic/musical-instruments-search-api/server"
"github.com/elastic/go-elasticsearch/v8"
"github.com/gorilla/mux"
)
func elasticsearchHandler(es *elasticsearch.Client,
f func(es *elasticsearch.Client, w http.ResponseWriter, r *http.Request)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { f(es, w, r) })
}
func main() {
//setup and initialize ES connection
var responseMap map[string]interface{}
//for local docker (non docker-compose) ES instance use host.docker.internal
cfg := elasticsearch.Config{
Addresses: []string{
"http://es01:9200",
},
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
// Get cluster info
res, err := es.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
//index sample data
server.IndexBulk(es)
// Check response status
if res.IsError() {
log.Fatalf("Error: %s", res.String())
}
// Deserialize the response into a map.
if err := json.NewDecoder(res.Body).Decode(&responseMap); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
// Print client and server version numbers.
log.Printf("Client: %s", elasticsearch.Version)
log.Printf("Server: %s", responseMap["version"].(map[string]interface{})["number"])
log.Println(strings.Repeat("~", 37))
//set up http routing
r := mux.NewRouter()
r.Handle("/query", elasticsearchHandler(es, server.QueryElasticsearch)).Methods("POST")
/*TODO other endpoints
r.HandleFunc("/upsert")
r.HandleFunc("/deleteAllDocs")
r.HandleFunc("/createIndex")
r.HandleFunc("/deleteIndex")
r.HandleFunc("/fullRefresh")
*/
//swagger
fs := http.FileServer(http.Dir("./swagger-ui/"))
r.PathPrefix("/api/docs/").Handler(http.StripPrefix("/api/docs/", fs))
log.Fatal(http.ListenAndServe(":8080", r))
}