Skip to content

Commit 998a083

Browse files
author
Dean Karn
authored
Merge pull request #26 from Chiiruno/dev/email-handler
handlers/email: Ability to toggle sending emails or not
2 parents 91a5908 + d79ea53 commit 998a083

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

handlers/email/email.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535

3636
// Email is an instance of the email logger
3737
type Email struct {
38+
enabled bool
3839
formatter Formatter
3940
formatFunc FormatFunc
4041
timestampFormat string
@@ -52,6 +53,7 @@ type Email struct {
5253
// New returns a new instance of the email logger
5354
func New(host string, port int, username string, password string, from string, to []string) *Email {
5455
e := &Email{
56+
enabled: true,
5557
timestampFormat: log.DefaultTimeFormat,
5658
host: host,
5759
port: port,
@@ -67,6 +69,9 @@ func New(host string, port int, username string, password string, from string, t
6769

6870
// SetTemplate sets Email's html template to be used for email body
6971
func (email *Email) SetTemplate(htmlTemplate string) {
72+
email.rw.Lock()
73+
defer email.rw.Unlock()
74+
7075
// parse email htmlTemplate, will panic if fails
7176
email.template = template.Must(template.New("email").Funcs(
7277
template.FuncMap{
@@ -96,12 +101,18 @@ func (email *Email) Template() *template.Template {
96101
// SetTimestampFormat sets Email's timestamp output format
97102
// Default is : "2006-01-02T15:04:05.000000000Z07:00"
98103
func (email *Email) SetTimestampFormat(format string) {
104+
email.rw.Lock()
105+
defer email.rw.Unlock()
106+
99107
email.timestampFormat = format
100108
}
101109

102110
// SetFormatFunc sets FormatFunc each worker will call to get
103111
// a Formatter func
104112
func (email *Email) SetFormatFunc(fn FormatFunc) {
113+
email.rw.Lock()
114+
defer email.rw.Unlock()
115+
105116
email.formatFunc = fn
106117
}
107118

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

133+
// SetEnabled enables or disables the email handler sending emails
134+
func (email *Email) SetEnabled(enabled bool) {
135+
email.rw.Lock()
136+
defer email.rw.Unlock()
137+
138+
email.enabled = enabled
139+
}
140+
122141
func defaultFormatFunc(email *Email) Formatter {
123142
b := new(bytes.Buffer)
124143

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

147166
// Log handles the log entry
148167
func (email *Email) Log(e log.Entry) {
168+
email.rw.RLock()
169+
170+
if !email.enabled {
171+
email.rw.RUnlock()
172+
return
173+
}
174+
149175
email.once.Do(func() {
150176
email.formatter = email.formatFunc(email)
151177
})
178+
179+
d := gomail.NewDialer(email.host, email.port, email.username, email.password)
180+
181+
email.rw.RUnlock()
182+
152183
var s gomail.SendCloser
153184
var err error
154185
var open bool
155186
var alreadyTriedSending bool
156187
var message *gomail.Message
157188
var count uint8
158189

159-
email.rw.RLock()
160-
d := gomail.NewDialer(email.host, email.port, email.username, email.password)
161-
email.rw.RUnlock()
162-
163190
for {
164191
count = 0
165192
alreadyTriedSending = false

handlers/email/email_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func (c *Client) r() string {
4545
}
4646

4747
func handleClient(c *Client, closePrematurly bool) string {
48-
4948
var msg []byte
5049

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

8483
func TestEmailHandler(t *testing.T) {
85-
8684
tests := []struct {
8785
expected string
8886
}{
@@ -174,7 +172,6 @@ func TestBadEmailTemplate(t *testing.T) {
174172
}
175173

176174
func TestBadSend(t *testing.T) {
177-
178175
email := New("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
179176
log.AddHandler(email, log.InfoLevel)
180177

@@ -211,6 +208,18 @@ func TestBadSend(t *testing.T) {
211208
log.Info("info")
212209
}
213210

211+
func TestBadEnabled(t *testing.T) {
212+
email := New("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
213+
email.SetEnabled(false)
214+
log.AddHandler(email, log.InfoLevel)
215+
216+
if email.enabled {
217+
t.Errorf("Expected 'false' Got 'true'")
218+
}
219+
220+
log.Info("info")
221+
}
222+
214223
func testFormatFunc(email *Email) Formatter {
215224
var err error
216225
b := new(bytes.Buffer)

0 commit comments

Comments
 (0)