func Start() { if !g.Config().Http.Enabled { return } addr := g.Config().Http.Listen if addr == "" { return } s := &http.Server{ Addr: addr, MaxHeaderBytes: 1 << 30, } log.Println("http listening", addr) log.Fatalln(s.ListenAndServe()) }
//sdp function of send mail //mail acount set by cfg file func SendMail(mail *model.Mail) { defer func() { <-MailWorkerChan }() tmp_mail := smtp.Mail{ Tos: mail.Tos, Subject: TransContent(mail.Subject), Content: TransContent(mail.Content), } err := MailSender.SendMail(&tmp_mail) if err != nil { log.Printf("邮件:%s 发送给:%s, 发送失败,错误:%s\n", mail.Subject, mail.Tos, err.Error()) } if err == nil { recordMsg := fmt.Sprintf("警报:%s\n已通过 %s 发送给 %s\n", mail.Subject, "mail", mail.Tos) RecordAlarm(recordMsg) } proc.IncreMailCount() if g.Config().Debug { log.Println("==mail==>>>>", mail) } }
func SendSms(imsms *model.IMSms) { defer func() { <-IMSmsWorkerChan }() // fmt.Println(sms.Tos) err := IMSender.SendMsg(strings.Split(imsms.Tos, ","), TransContent(imsms.Content)) if err != nil { log.Printf("IM消息:%s to:%s 发送失败。错误:%s\n", imsms.Content, imsms.Tos, err.Error()) } if err == nil { recordMsg := fmt.Sprintf("警报:%s\n已通过 %s 发送给 %s\n", imsms.Content, "99u", imsms.Tos) RecordAlarm(recordMsg) } proc.IncreIMSmsCount() if g.Config().Debug { log.Println("==im-sms==>>>>", imsms) } }
func ConsumePhone() { if g.Config().Acount.Phone == nil { return } PhoneSender.SetKeyAndSecret(g.Config().Acount.Phone.Key, g.Config().Acount.Phone.Serect) // nexmo.Status_url = "http://61.147.187.235:8011/hello" queue := g.Config().Queue.Phone for { L := redis.PopAllPhone(queue) if len(L) == 0 { time.Sleep(time.Millisecond * 200) continue } SendPhoneList(L) } }
func RecordPhoneAlarm(recordMsg string) { tos := g.Config().Acount.IM.PhoneGroup if tos == "" { return } err := IMSender.SendMsg(strings.Split(tos, ","), recordMsg) if err != nil { log.Printf("record phone alarm fail:%s with err:%s", recordMsg, err.Error()) } }
func ConsumeIMSms() { if g.Config().Acount.IM == nil { return } ac := im.Acount{ Uri: g.Config().Acount.IM.Uri, Password: g.Config().Acount.IM.Password, } IMSender.SetAcount(&ac) queue := g.Config().Queue.IMSms for { L := redis.PopAllIMSms(queue) if len(L) == 0 { time.Sleep(time.Millisecond * 200) continue } SendSmsList(L) } }
func SendPhone(phone *model.Phone) { defer func() { <-PhoneWorkerChan }() // fmt.Println(sms.Tos) PhoneSender.SetRepeat("2") PhoneSender.SetLanguage("zh-cn") if g.Config().Acount.Phone.Callback != "" { PhoneSender.SetCallBack(g.Config().Acount.Phone.Callback) } for _, to := range strings.Split(phone.Tos, ",") { if len(to) == 0 { continue } PhoneSender.SetToChinaZoneCode(to) PhoneSender.SetVoiceMsg(TransContent(phone.Content)) err, resp := PhoneSender.Call() if err != nil { log.Printf("报警电话:%s to:%s 调用API失败,错误:%s\n", phone.Content, to, err.Error()) } if err == nil { recordMsg := fmt.Sprintf("警报:%s\n已通过 %s 发送给 %s 呼叫编码%s\n", phone.Content, "phone", to, resp.Call_id) RecordAlarm(recordMsg) RecordPhoneAlarm(recordMsg) } proc.IncrePhoneCount() } if g.Config().Debug { log.Println("==phone==>>>>", phone) } }
func ConsumeMail() { if g.Config().Acount.Mail == nil { return } //init mail acount var acount smtp.MailAcount acount.Password = g.Config().Acount.Mail.Password acount.Server = g.Config().Acount.Mail.Server acount.User = g.Config().Acount.Mail.User MailSender.SetMailAcount(&acount) queue := g.Config().Queue.Mail for { L := redis.PopAllMail(queue) if len(L) == 0 { time.Sleep(time.Millisecond * 200) continue } SendMailList(L) } }
func InitConnPool() { redisConfig := g.Config().Redis ConnPool = &redis.Pool{ MaxIdle: redisConfig.MaxIdle, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", redisConfig.Addr) if err != nil { return nil, err } return c, err }, TestOnBorrow: PingRedis, } }
func configCommonRoutes() { http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok")) }) http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(g.VERSION)) }) http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) { RenderDataJson(w, file.SelfDir()) }) http.HandleFunc("/config/reload", func(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.RemoteAddr, "127.0.0.1") { g.ParseConfig(g.ConfigFile) RenderDataJson(w, g.Config()) } else { w.Write([]byte("no privilege")) } }) }