Exemple #1
0
func (rs *controller) Post(w http.ResponseWriter, request *http.Request, p httprouter.Params) {
	defer request.Body.Close()

	data, err := ioutil.ReadAll(request.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	var rule Rule
	err = json.Unmarshal(data, &rule)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	err = Save(&rule)
	if err != nil {
		if mgo.IsDup(err) {
			http.Error(w, err.Error(), http.StatusBadRequest)
		} else {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
		return
	}
	axn, err := rule.GetAction()
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	actions.Add(axn)
	req, err := http.NewRequest("POST", config.RuleEndpoint(), strings.NewReader(rule.EPL))
	if err != nil {
		mylog.Alert("rule controller POST", err)
	}
	resp, err := httpClient.Do(req)
	if err != nil {
		mylog.Alert("rule controller POST", err, resp)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if resp.StatusCode/100 != 2 {
		mylog.Alert("rule controller POST", resp.Status)
		http.Error(w, resp.Status, http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusCreated)
	dataBack, err := json.Marshal(rule)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.Write(dataBack)

}
Exemple #2
0
func (rs *controller) Del(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	err := Delete(p[0].Value)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	req, err := http.NewRequest("DELETE", config.RuleEndpoint()+"/"+p[0].Value, nil)
	if err != nil {
		mylog.Alert("rule controller DELETE", err)
	}
	resp, err := httpClient.Do(req)
	if err != nil {
		mylog.Alert("rule controller DELETE", err, resp)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if resp.StatusCode/100 != 2 {
		mylog.Alert("rule controller DELETE", resp.Status)
		http.Error(w, resp.Status, http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
	fmt.Fprintln(w, "deleted", p[0].Value)
}