From 75c45195655ef5c2dbad40d71609c316f4376bed Mon Sep 17 00:00:00 2001 From: miraclesu Date: Fri, 7 Apr 2017 15:37:14 +0800 Subject: [PATCH] =?UTF-8?q?noticer:=20=E5=A2=9E=E5=8A=A0=20http=20?= =?UTF-8?q?=E5=8F=91=E9=80=81=E9=80=9A=E7=9F=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/web/server.go | 15 +++++++++++---- conf/conf.go | 2 +- models/noticer.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/bin/web/server.go b/bin/web/server.go index 4dc8a93..1e197a2 100644 --- a/bin/web/server.go +++ b/bin/web/server.go @@ -35,10 +35,17 @@ func main() { } if conf.Config.Mail.Enable { - noticer, err := models.NewMail(10 * time.Second) - if err != nil { - log.Error(err.Error()) - return + var noticer models.Noticer + + if len(conf.Config.Mail.HttpAPI) > 0 { + noticer = &models.HttpAPI{} + } else { + mailer, err := models.NewMail(10 * time.Second) + if err != nil { + log.Error(err.Error()) + return + } + noticer = mailer } go models.StartNoticer(noticer) } diff --git a/conf/conf.go b/conf/conf.go index 4e75a19..4e6c3b3 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -81,7 +81,7 @@ type MailConf struct { Enable bool To []string // 如果配置,则按 http api 方式发送,否则按 smtp 方式发送 - HttpApi string + HttpAPI string // 如果此时间段内没有邮件发送,则关闭 SMTP 连接,单位/秒 Keepalive int64 *gomail.Dialer diff --git a/models/noticer.go b/models/noticer.go index 2ec50c6..7c90431 100644 --- a/models/noticer.go +++ b/models/noticer.go @@ -1,8 +1,11 @@ package models import ( + "bytes" "encoding/json" "fmt" + "io/ioutil" + "net/http" "time" client "github.com/coreos/etcd/clientv3" @@ -73,7 +76,7 @@ func (m *Mail) Serve() { m.timer.Reset(time.Duration(m.cf.Keepalive) * time.Second) if !m.open { if m.sc, err = m.cf.Dialer.Dial(); err != nil { - log.Warnf("send msg[%+v] err: %s", msg, err.Error()) + log.Warnf("smtp send msg[%+v] err: %s", msg, err.Error()) continue } m.open = true @@ -85,7 +88,7 @@ func (m *Mail) Serve() { sm.SetHeader("Subject", msg.Subject) sm.SetBody("text/plain", msg.Body) if err := gomail.Send(m.sc, sm); err != nil { - log.Warnf("send msg[%+v] err: %s", msg, err.Error()) + log.Warnf("smtp send msg[%+v] err: %s", msg, err.Error()) } case <-m.timer.C: if m.open { @@ -104,6 +107,43 @@ func (m *Mail) Send(msg *Message) { m.msgChan <- msg } +type HttpAPI struct{} + +func (h *HttpAPI) Serve() {} + +func (h *HttpAPI) Send(msg *Message) { + body, err := json.Marshal(msg) + if err != nil { + log.Warnf("http api send msg[%+v] err: %s", msg, err.Error()) + return + } + + req, err := http.NewRequest("POST", conf.Config.Mail.HttpAPI, bytes.NewBuffer(body)) + if err != nil { + return + } + + req.Header.Set("Content-Type", "application/json") + resp, err := http.DefaultClient.Do(req) + if err != nil { + log.Warnf("http api send msg[%+v] err: %s", msg, err.Error()) + return + } + defer resp.Body.Close() + + if resp.StatusCode == 200 { + return + } + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Warnf("http api send msg[%+v] err: %s", msg, err.Error()) + return + } + log.Warnf("http api send msg[%+v] err: %s", msg, string(data)) + return +} + func StartNoticer(n Noticer) { go n.Serve() rch := DefalutClient.Watch(conf.Config.Noticer, client.WithPrefix())