Skip to content

Commit 4cbcd29

Browse files
author
Thibault Richard
committed
bam!
1 parent ae5bf5e commit 4cbcd29

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

main.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,42 @@ func main() {
3939

4040
func router(r *gin.Engine) {
4141
r.POST("/event", sendEvent)
42-
r.GET("/health", getStatus)
42+
r.GET("/health", getHealth)
4343
}
4444

4545
func watch() {
4646
tick := time.NewTicker(time.Second * time.Duration(tick))
4747
for range tick.C {
4848
for _, event := range events {
4949
if time.Since(event.Timestamp) > time.Second*time.Duration(timeout) {
50-
log.WithField("ID", event.ID).Errorf("No event since %d seconds", timeout)
50+
alert(event)
5151
}
5252
}
5353
}
5454
}
5555

56+
func alert(event Event) {
57+
mutex.Lock()
58+
defer mutex.Unlock()
59+
event.Status = "ERROR"
60+
events[event.ID] = event
61+
log.WithField("ID", event.ID).Errorf("No event since %d seconds", timeout)
62+
sendEmail(event)
63+
}
64+
65+
func sendEmail(event Event) {
66+
// TODO
67+
log.WithField("ID", event.ID).Error("Send alert mail")
68+
}
69+
5670
type Event struct {
5771
ID string
72+
Status string
5873
Timestamp time.Time
5974
Value interface{}
6075
}
6176

6277
func sendEvent(c *gin.Context) {
63-
6478
var obj map[string]interface{}
6579
err := c.BindJSON(&obj)
6680
if err != nil {
@@ -75,15 +89,16 @@ func sendEvent(c *gin.Context) {
7589
}
7690

7791
ID = c.Request.RemoteAddr + "/" + ID
78-
79-
mutex.Lock()
80-
defer mutex.Unlock()
81-
82-
events[ID] = Event{
92+
event := Event{
8393
ID: ID,
94+
Status: "OK",
8495
Timestamp: time.Now(),
8596
Value: obj,
8697
}
98+
99+
mutex.Lock()
100+
defer mutex.Unlock()
101+
events[ID] = event
87102
}
88103

89104
func extractID(obj map[string]interface{}) (string, error) {
@@ -95,7 +110,7 @@ func extractID(obj map[string]interface{}) (string, error) {
95110
return "", errors.New("No field found to extract ID")
96111
}
97112

98-
func getStatus(c *gin.Context) {
113+
func getHealth(c *gin.Context) {
99114
mutex.RLock()
100115
defer mutex.RUnlock()
101116

0 commit comments

Comments
 (0)