/m/ method
This commit is contained in:
parent
8588229495
commit
43258cb2b0
20
node/api.go
20
node/api.go
@ -54,6 +54,25 @@ func (es ESConf) EHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(strings.Join(messages, "\n")))
|
||||
}
|
||||
|
||||
// MHandler /m/ schema
|
||||
func (es ESConf) MHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
msgid := vars["msgid"]
|
||||
|
||||
LogRequest(r)
|
||||
|
||||
ch := make(chan []byte)
|
||||
// Get echolist
|
||||
go func() {
|
||||
ch <- es.GetPlainTextMessage(msgid)
|
||||
}()
|
||||
|
||||
message := <-ch
|
||||
|
||||
w.WriteHeader(200)
|
||||
w.Write(message)
|
||||
}
|
||||
|
||||
// Serve ...
|
||||
func Serve(listen string, es ESConf) {
|
||||
r := mux.NewRouter()
|
||||
@ -62,6 +81,7 @@ func Serve(listen string, es ESConf) {
|
||||
|
||||
// Standart schemas
|
||||
r.HandleFunc("/e/{echo}", es.EHandler)
|
||||
r.HandleFunc("/m/{msgid}", es.MHandler)
|
||||
|
||||
http.Handle("/", r)
|
||||
|
||||
|
@ -25,6 +25,50 @@ type Bucket struct {
|
||||
DocCount int `json:"doc_count"`
|
||||
}
|
||||
|
||||
// MakePlainTextMessage ...
|
||||
func MakePlainTextMessage(hit interface{}) string {
|
||||
|
||||
h := make(map[string]interface{})
|
||||
h = hit.(map[string]interface{})
|
||||
s := make(map[string]interface{})
|
||||
s = h["_source"].(map[string]interface{})
|
||||
|
||||
log.Print(s)
|
||||
|
||||
m := []string{"ii/ok", s["echo"].(string), s["date"].(string), s["author"].(string), "null", s["to"].(string), s["subg"].(string), "", s["message"].(string)}
|
||||
|
||||
return strings.Join(m, "\n")
|
||||
}
|
||||
|
||||
// GetPlainTextMessage ...
|
||||
func (es ESConf) GetPlainTextMessage(msgid string) []byte {
|
||||
var message []byte
|
||||
|
||||
searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
|
||||
searchQ := []byte(strings.Join([]string{
|
||||
`{"query": {"match": {"_id": "`, msgid, `"}}}`}, ""))
|
||||
|
||||
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 message
|
||||
}
|
||||
|
||||
esresp, err := gabs.ParseJSON(body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
hits, _ := esresp.Path("hits.hits").Data().([]interface{})
|
||||
|
||||
return []byte(MakePlainTextMessage(hits[0]))
|
||||
}
|
||||
|
||||
// GetEchoMessageHashes ...
|
||||
func (es ESConf) GetEchoMessageHashes(echo string) []string {
|
||||
var hashes []string
|
||||
|
Loading…
Reference in New Issue
Block a user