Skip to content

Commit

Permalink
Implement index endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bittersweet committed Oct 11, 2015
1 parent 3a949f1 commit 730dd27
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 6 deletions.
1 change: 1 addition & 0 deletions create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
psql notifier -f schema.sql
2 changes: 2 additions & 0 deletions drop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psql notifier -c "drop table incoming";
psql notifier -c "drop table notifiers";
2 changes: 1 addition & 1 deletion email.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func sendEmail(class string, data []byte) {
// already experienced a connection reset by peer locally
}

func sendEmailNotification(s *Stat, notifier *dbNotifier) {
func sendEmailNotification(s *Stat, notifier *Notifier) {
var err error
var doc bytes.Buffer

Expand Down
39 changes: 39 additions & 0 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,50 @@ package main

import (
"encoding/json"
"html/template"
"log"
"net/http"
"time"
)

func trackTime(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s\n", name, elapsed)
}

func handleIndex(w http.ResponseWriter, r *http.Request) {
defer trackTime(time.Now(), "handleIndex")
defer r.Body.Close()

t, err := template.ParseFiles("templates/index.html")
if err != nil {
log.Fatal("ParseFiles", err)
}

notifiers := []Notifier{}
err = db.Select(&notifiers, "SELECT * FROM notifiers")
if err != nil {
log.Fatal("db.Select notifiers ", err)
}

incoming := []Incoming{}
err = db.Select(&incoming, "SELECT * FROM incoming ORDER BY id desc")
if err != nil {
log.Fatal("db.Select incoming ", err)
}

data := map[string]interface{}{
"notifiers": notifiers,
"incoming": incoming,
}
err = t.Execute(w, data)
if err != nil {
log.Fatal("t.Execute", err)
}
}

func handleCount(w http.ResponseWriter, r *http.Request) {
defer trackTime(time.Now(), "handleCount")
defer r.Body.Close()

count := countRows()
Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
go run *.go
go run udp_listener.go slack.go email.go endpoints.go
2 changes: 1 addition & 1 deletion slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"text/template"
)

func sendSlackNotification(s *Stat, notifier *dbNotifier) {
func sendSlackNotification(s *Stat, notifier *Notifier) {
var err error
var doc bytes.Buffer

Expand Down
41 changes: 41 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<html>
<head>
</head>
<body>
Notifiers:
<table>
<tr>
<th>id</th>
<th>type</th>
<th>class</th>
<th>template</th>
</tr>
{{range $notifier := .notifiers}}</td>
<tr>
<td>{{$notifier.Id}}</td>
<td>{{$notifier.NotificationType}}</td>
<td>{{$notifier.Class}}</td>
<td style="background: red;">{{$notifier.Template}}</td>
</tr>
{{ end }}
</table>

Latest incoming items:
<table>
<tr>
<th>id</th>
<th>class</th>
<th>received at</th>
<th>data</th>
</tr>
{{range $item := .incoming}}</td>
<tr>
<td>{{$item.Id}}</td>
<td>{{$item.Class}}</td>
<td>{{$item.ReceivedAt}}</td>
<td>{{$item.FormattedData}}</td>
</tr>
{{ end }}
</table>
</body>
</html>
17 changes: 14 additions & 3 deletions udp_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Stat struct {
Value types.JsonText `json:"value"`
}

type dbNotifier struct {
type Notifier struct {
Id int `db:"id"`
NotificationType string `db:"notification_type"`
Class string `db:"class"`
Expand All @@ -30,6 +30,17 @@ type dbNotifier struct {
// email address, slack channel, phone number, how to store?
}

type Incoming struct {
Id int `db:"id"`
Class string `db:"class"`
ReceivedAt time.Time `db:"received_at"`
Data string `db:"data"`
}

func (i *Incoming) FormattedData() string {
return string(i.Data)
}

func (s *Stat) toMap() map[string]interface{} {
m := map[string]interface{}{}
s.Value.Unmarshal(&m)
Expand All @@ -47,7 +58,7 @@ func (s *Stat) persist() {
}

func (s *Stat) notify() {
notifiers := []dbNotifier{}
notifiers := []Notifier{}
err := db.Select(&notifiers, "SELECT * FROM notifiers WHERE class=$1", s.Key)
if err != nil {
log.Fatal("db.Select ", err)
Expand Down Expand Up @@ -110,7 +121,7 @@ func main() {
}

go listenToUDP(conn)
// http.HandleFunc("/", handleIndex)
http.HandleFunc("/", handleIndex)
http.HandleFunc("/v1/count", handleCount)

db, err = sqlx.Connect("postgres", "user=markmulder dbname=notifier sslmode=disable")
Expand Down

0 comments on commit 730dd27

Please sign in to comment.