Esempio n. 1
0
func (r *Receiver) handleAPI(w http.ResponseWriter, req *http.Request) {
	log.Printf("[DEBUG] Received API call")

	el, err := elastic.NewElastic(r.Config)
	if err != nil {
		http.Error(w, "Error initializing Elastic", 500)
		return
	}
	r.Elastic = el

	if req.Method == "GET" {
		body, _ := r.Elastic.SearchAll()
		fmt.Fprintf(w, "%s", body)
		return
	}

	if req.Method != "POST" { // ie PUT, DELETE, etc.
		http.Error(w, "Unsupported method", 405)
		return
	}

	decoder := json.NewDecoder(req.Body)
	var j config.APISearch
	err = decoder.Decode(&j)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	body, _ := r.Elastic.SearchKeyword(j.Key, j.Value)
	fmt.Fprintf(w, "%s", body)
}
Esempio n. 2
0
func (r *Receiver) ConnectElastic() error {
	el, err := elastic.NewElastic(r.Config)
	if err != nil {
		return err
	}

	r.Elastic = el
	return nil
}
Esempio n. 3
0
func (r *Receiver) ConnectElastic() (map[string]config.ElasticMetadata, error) {
	el, err := elastic.NewElastic(r.Config)
	if err != nil {
		return nil, err
	}

	r.Elastic = el
	var registration, _ = r.Elastic.LoadRegistration()

	return registration, err
}