-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (89 loc) · 2.52 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"flag"
"fmt"
"github.com/belogik/goes"
"github.com/bootic/bootic_autocomplete/lib"
"log"
"net/http"
"net/url"
"runtime"
"strings"
)
type esQuery struct {
}
func (q *esQuery) Build(ctx *lib.Context) (query map[string]interface{}) {
size := ctx.PerPage
from := ctx.PerPage * (ctx.Page - 1)
query = map[string]interface{}{
"query": map[string]interface{}{
"filtered": map[string]interface{}{
"query": map[string]interface{}{
"query_string": map[string]interface{}{
"query": ctx.Q,
"default_operator": "AND",
},
},
"filter": map[string]interface{}{
"and": []interface{}{
map[string]interface{}{
"term": map[string]interface{}{
"status": []string{"visible"},
},
},
map[string]interface{}{
"terms": map[string]interface{}{
"account.status": []string{"active", "free", "trial"},
},
},
},
},
},
},
"size": size,
"from": from,
}
return query
}
type esSearcher struct {
conn *goes.Connection
esIndex []string
esType []string
}
func (s *esSearcher) Search(query map[string]interface{}) (*goes.Response, error) {
extraArgs := make(url.Values, 1)
return s.conn.Search(query, s.esIndex, s.esType, extraArgs)
}
func main() {
maxProcs := runtime.NumCPU()
runtime.GOMAXPROCS(maxProcs)
var (
http_host string
es_host string
cdn_host string
es_index string
es_type string
)
flag.StringVar(&http_host, "httphost", "localhost:3000", "HTTP host:port for search endpoint")
flag.StringVar(&es_host, "eshost", "localhost:9200", "HTTP host:port for ElasticSearch server")
flag.StringVar(&cdn_host, "cdnhost", "https://o.btcdn.co", "CDN host for item images")
flag.StringVar(&es_index, "esindex", "products", "ElasticSearch index")
flag.StringVar(&es_type, "estype", "product", "ElasticSearch document type")
flag.Parse()
es_host_and_port := strings.Split(es_host, ":")
config := map[string]string{
"cdn_host": cdn_host,
}
conn := goes.NewConnection(es_host_and_port[0], es_host_and_port[1])
searcher := &esSearcher{
conn: conn,
esIndex: []string{es_index},
esType: []string{es_type},
}
presenter := &lib.JsonPresenter{}
http.Handle("/search", lib.HttpHandler(searcher, &esQuery{}, presenter, config))
http.Handle("/ws", lib.WsHandler(searcher, &esQuery{}, presenter, config))
http.Handle("/", http.FileServer(http.Dir("./public")))
log.Println(fmt.Sprintf("serving http requests on %s", http_host))
log.Fatal(http.ListenAndServe(http_host, nil))
}