func (a *UpdateAction) Do(n notices.Notice) (err error) { // A litle (or very) dirty. Add "hidden" parameters as notification data // and remove them after executing template (better copy first level fields in a new map?) n["__attrName"] = a.ActionData.Parameters["name"] n["__attrValue"] = a.ActionData.Parameters["value"] n["__attrType"] = a.ActionData.Parameters["type"] text := util.ExpandFromMap(updateTemplateText, n) mylog.Debug("UpdateAction text to send:", text) delete(n, "__attrName") delete(n, "__attrValue") delete(n, "__attrType") req, err := http.NewRequest("POST", config.UpdateEndpoint(), strings.NewReader(text)) if err != nil { mylog.Alert("UpdateAction.Do", err) } req.Header.Add("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { mylog.Alert("UpdateAction.Do", err) return err } respDump, err := httputil.DumpResponse(resp, true) mylog.Debugf("UpdateAction.Do server response: (%q,%v)\n", string(respDump), err) return err }
func (a *SMSAction) Do(n notices.Notice) (err error) { if mylog.Debugging { defer func() { mylog.Debugf("exit SMSAction.Do %+v", err) }() } text := util.ExpandFromMap(a.ActionData.Template, n) mylog.Debug("SMSAction text to send:", text) msg := fmt.Sprintf(`{"to":["tel:%s"], "message": %q}`, a.Parameters["to"], text) mylog.Debug("SMSAction msg to send:", msg) req, err := http.NewRequest("POST", config.SMSEndpoint(), strings.NewReader(msg)) if err != nil { mylog.Alert("SMSAction.Do", err) } req.Header.Add("API_KEY", config.APIKey()) req.Header.Add("API_SECRET", config.APISecret()) req.Header.Add("Content-Type", "application/json") mylog.Debug("SMSAction.Do msg:", msg) resp, err := httpClient.Do(req) if err != nil { mylog.Alert("SMSAction.Do", err) return err } respDump, err := httputil.DumpResponse(resp, true) mylog.Debugf("SMSAction.Do server response: (%q,%v)\n", string(respDump), err) return err }
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) }
func PanicHandler(h http.Handler) http.Handler { return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { defer func() { if excp := recover(); excp != nil { mylog.Alert(excp) debug.PrintStack() http.Error(w, "Internal panic. Contact support team.", 500) } }() h.ServeHTTP(w, r) }) }
func main() { var err error fmt.Println("Hello World!") mylog.SetLevel("debug") if err = config.LoadConfig("cepiot.conf"); err != nil { mylog.Alert(err) os.Exit(-1) } if err = rules.Initialize(); err != nil { mylog.Alert(err) os.Exit(-1) } router := httprouter.New() notices.AddRoutes(router) actions.AddRoutes(router) rules.AddRoutes(router) err = http.ListenAndServe(config.Port(), util.PanicHandler(router)) if err != nil { mylog.Alert(err) } }
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) }
func (a *EmailAction) Do(n notices.Notice) (err error) { if mylog.Debugging { mylog.Debugf("enter EmailAction.Do %+v %+v", a, n) defer func() { mylog.Debugf("exit EmailAction.Do %+v", err) }() } text := util.ExpandFromMap(a.ActionData.Template, n) mylog.Debugf("text to send %q", text) err = smtp.SendMail(config.SMTPServer(), nil, a.Parameters["from"], []string{a.Parameters["to"]}, []byte(text)) if err != nil { mylog.Alert("EmailAction.Do", err) } return err }