Skip to content

Commit

Permalink
Merge pull request #26 from Chiiruno/dev/email-handler
Browse files Browse the repository at this point in the history
handlers/email: Ability to toggle sending emails or not
  • Loading branch information
Dean Karn authored Jun 28, 2018
2 parents 91a5908 + d79ea53 commit 998a083
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
35 changes: 31 additions & 4 deletions handlers/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (

// Email is an instance of the email logger
type Email struct {
enabled bool
formatter Formatter
formatFunc FormatFunc
timestampFormat string
Expand All @@ -52,6 +53,7 @@ type Email struct {
// New returns a new instance of the email logger
func New(host string, port int, username string, password string, from string, to []string) *Email {
e := &Email{
enabled: true,
timestampFormat: log.DefaultTimeFormat,
host: host,
port: port,
Expand All @@ -67,6 +69,9 @@ func New(host string, port int, username string, password string, from string, t

// SetTemplate sets Email's html template to be used for email body
func (email *Email) SetTemplate(htmlTemplate string) {
email.rw.Lock()
defer email.rw.Unlock()

// parse email htmlTemplate, will panic if fails
email.template = template.Must(template.New("email").Funcs(
template.FuncMap{
Expand Down Expand Up @@ -96,12 +101,18 @@ func (email *Email) Template() *template.Template {
// SetTimestampFormat sets Email's timestamp output format
// Default is : "2006-01-02T15:04:05.000000000Z07:00"
func (email *Email) SetTimestampFormat(format string) {
email.rw.Lock()
defer email.rw.Unlock()

email.timestampFormat = format
}

// SetFormatFunc sets FormatFunc each worker will call to get
// a Formatter func
func (email *Email) SetFormatFunc(fn FormatFunc) {
email.rw.Lock()
defer email.rw.Unlock()

email.formatFunc = fn
}

Expand All @@ -119,6 +130,14 @@ func (email *Email) SetEmailConfig(host string, port int, username string, passw
email.formatter = email.formatFunc(email)
}

// SetEnabled enables or disables the email handler sending emails
func (email *Email) SetEnabled(enabled bool) {
email.rw.Lock()
defer email.rw.Unlock()

email.enabled = enabled
}

func defaultFormatFunc(email *Email) Formatter {
b := new(bytes.Buffer)

Expand Down Expand Up @@ -146,20 +165,28 @@ func defaultFormatFunc(email *Email) Formatter {

// Log handles the log entry
func (email *Email) Log(e log.Entry) {
email.rw.RLock()

if !email.enabled {
email.rw.RUnlock()
return
}

email.once.Do(func() {
email.formatter = email.formatFunc(email)
})

d := gomail.NewDialer(email.host, email.port, email.username, email.password)

email.rw.RUnlock()

var s gomail.SendCloser
var err error
var open bool
var alreadyTriedSending bool
var message *gomail.Message
var count uint8

email.rw.RLock()
d := gomail.NewDialer(email.host, email.port, email.username, email.password)
email.rw.RUnlock()

for {
count = 0
alreadyTriedSending = false
Expand Down
15 changes: 12 additions & 3 deletions handlers/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (c *Client) r() string {
}

func handleClient(c *Client, closePrematurly bool) string {

var msg []byte

c.w("220 Welcome to the Jungle")
Expand Down Expand Up @@ -82,7 +81,6 @@ func handleClient(c *Client, closePrematurly bool) string {
}

func TestEmailHandler(t *testing.T) {

tests := []struct {
expected string
}{
Expand Down Expand Up @@ -174,7 +172,6 @@ func TestBadEmailTemplate(t *testing.T) {
}

func TestBadSend(t *testing.T) {

email := New("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
log.AddHandler(email, log.InfoLevel)

Expand Down Expand Up @@ -211,6 +208,18 @@ func TestBadSend(t *testing.T) {
log.Info("info")
}

func TestBadEnabled(t *testing.T) {
email := New("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
email.SetEnabled(false)
log.AddHandler(email, log.InfoLevel)

if email.enabled {
t.Errorf("Expected 'false' Got 'true'")
}

log.Info("info")
}

func testFormatFunc(email *Email) Formatter {
var err error
b := new(bytes.Buffer)
Expand Down

0 comments on commit 998a083

Please sign in to comment.