From 84e2cc0ee50c8c4d0630a6344a497ff16172ede8 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Sun, 12 Feb 2017 13:19:02 +0300 Subject: [PATCH] Initial --- main.go | 31 ++++++++++++++++++++++++++ node/api.go | 37 +++++++++++++++++++++++++++++++ node/elastic.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 main.go create mode 100644 node/api.go create mode 100644 node/elastic.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..d70245d --- /dev/null +++ b/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "flag" + "gitea.difrex.ru/Umbrella/lessmore/node" +) + +var ( + listen string + es string + esMessagesIndex string + esMessagesType string +) + +// init ... +func init() { + flag.StringVar(&listen, "listen", "127.0.0.1:15582", "Address to listen") + flag.StringVar(&es, "es", "htt://127.0.0.1:9200", "ES host") + flag.StringVar(&esMessagesIndex, "esindex", "idec3", "ES index") + flag.StringVar(&esMessagesType, "estype", "post", "ES index type") + flag.Parse() +} + +// main ... +func main() { + esconf := node.ESConf{} + esconf.Host = es + esconf.Index = esMessagesIndex + esconf.Type = esMessagesType + node.Serve(listen, esconf) +} diff --git a/node/api.go b/node/api.go new file mode 100644 index 0000000..ca9f3fb --- /dev/null +++ b/node/api.go @@ -0,0 +1,37 @@ +package node + +import ( + "github.com/gorilla/mux" + "log" + "net/http" + "time" +) + +// ListTXTHandler ... +func (es ESConf) ListTXTHandler(w http.ResponseWriter, r *http.Request) { + + // Get echolist + echoes, err := es.GetListTXT() + if err != nil { + w.WriteHeader(500) + } + + w.WriteHeader(200) + w.Write(echoes) +} + +// Serve ... +func Serve(listen string, es ESConf) { + r := mux.NewRouter() + r.HandleFunc("/list.txt", es.ListTXTHandler) + http.Handle("/list.txt", r) + + srv := http.Server{ + Handler: r, + Addr: listen, + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + + log.Fatal(srv.ListenAndServe()) +} diff --git a/node/elastic.go b/node/elastic.go new file mode 100644 index 0000000..a9b2532 --- /dev/null +++ b/node/elastic.go @@ -0,0 +1,58 @@ +package node + +import ( + "bytes" + // "encoding/json" + "gitea.difrex.ru/Umbrella/fetcher/i2es" + "github.com/Jeffail/gabs" + "io/ioutil" + "net/http" + "strconv" + "strings" +) + +const ( + echoAgg = "echo_uniq" +) + +// ESConf ... +type ESConf i2es.ESConf + +// Bucket ... +type Bucket struct { + Key string `json:"key"` + DocCount int `json:"doc_count"` +} + +// GetListTXT ... +func (es ESConf) GetListTXT() ([]byte, error) { + searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/") + searchQ := []byte(`{"aggs": {"echo_uniq": { "terms": { "field": "echo","size": 1000}}}}`) + + req, err := http.NewRequest("POST", searchURI, bytes.NewBuffer(searchQ)) + client := &http.Client{} + resp, err := client.Do(req) + + // defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return []byte(""), err + } + + esresp, err := gabs.ParseJSON(body) + if err != nil { + panic(err) + } + var buckets []Bucket + buckets, _ = esresp.Path("aggregations.echo_uniq.buckets").Data().([]Bucket) + + var echoes []string + for _, bucket := range buckets { + c := strconv.Itoa(bucket.DocCount) + echostr := strings.Join([]string{bucket.Key, ":", c, ":"}, "") + echoes = append(echoes, echostr) + } + + return []byte(strings.Join(echoes, "\n")), nil +}