Skip to content

Commit

Permalink
template notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
bittersweet committed Oct 11, 2015
1 parent 43fb2ab commit fcfb380
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions udp_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type Stat struct {
Value types.JsonText `json:"value"`
}

type Notifier struct {
Class string
Template string
}

func (s *Stat) persist() {
var incomingId int
err := db.QueryRow(`INSERT INTO incoming(received_at, data) VALUES($1, $2) RETURNING id`, time.Now(), s.Value).Scan(&incomingId)
Expand All @@ -38,6 +43,35 @@ func (s *Stat) notify() {
sendEmail(s.Key, string(s.Value))
}

func (s *Stat) specialNotify() {
var err error
var doc bytes.Buffer

n := Notifier{
"mark",
"{{.Number}}: is pretty awesome!, {{.Yeah}} nigga",
}

t := template.New("notificationTemplate")
t, err = t.Parse(n.Template)
if err != nil {
log.Fatal("t.Parse of n.Template", err)
}

jmap := map[string]string{
"Number": "1000test",
"Yeah": "yeayeah",
}
err = t.Execute(&doc, jmap)
if err != nil {
log.Fatal("t.Execute ", err)
}

fmt.Println(doc)
sendEmail(s.Key, string(doc.Bytes()))

}

func countRows() int {
var rows int
err := db.QueryRow("select count(*) from incoming").Scan(&rows)
Expand Down Expand Up @@ -68,7 +102,8 @@ func listenToUDP(conn *net.UDPConn) {
}

stat.persist()
stat.notify()
// stat.notify()
stat.specialNotify()
}
}

Expand All @@ -86,8 +121,6 @@ func handleCount(w http.ResponseWriter, r *http.Request) {
log.Fatal("MarshalIndent", err)
}

sendEmail("testClass", "body")

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(output)
}
Expand Down Expand Up @@ -120,12 +153,12 @@ func sendEmail(class string, data string) {
if err != nil {
log.Fatal("t.Parse ", err)
}
bodyString := fmt.Sprintf("<h1>class: %s</h1>\n<p>data: %s</p>", class, data)
// bodyString := fmt.Sprintf("<h1>class: %s</h1>\\n<p>data: %s</p>", class, data)
context := &EmailData{
"Springest Dev <[email protected]>",
"[email protected]",
"Email subject line",
bodyString,
data,
}
err = t.Execute(&doc, context)
if err != nil {
Expand Down

0 comments on commit fcfb380

Please sign in to comment.