65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/mailgun/mailgun-go/v4"
|
|
)
|
|
|
|
const yourDomain string = "sandbox403bedf0d09d4e539131fabf0ab7c11f.mailgun.org" // e.g. mg.yourcompany.com
|
|
const privateAPIKey string = "key-2b58bc1193c549df83d8d5ed54e1c982"
|
|
|
|
func main() {
|
|
for {
|
|
log.Println("Checking Fridge")
|
|
if !endPointAlive() {
|
|
log.Println("He Dead")
|
|
sendMail()
|
|
} else {
|
|
log.Println("He Alive")
|
|
}
|
|
time.Sleep(20 * time.Second)
|
|
}
|
|
}
|
|
|
|
func endPointAlive() bool {
|
|
|
|
res, err := http.Get("http://localhost:2122/heartbeat")
|
|
if err != nil {
|
|
log.Println(err)
|
|
return false
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
|
log.Println("Status Code ", res.StatusCode)
|
|
return false
|
|
} else {
|
|
return true
|
|
}
|
|
}
|
|
|
|
func sendMail() {
|
|
mg := mailgun.NewMailgun(yourDomain, privateAPIKey)
|
|
|
|
sender := "robviren@gmail.com"
|
|
subject := "Fridge Lost Power!"
|
|
body := "Make Rob go fix it!"
|
|
recipient := "rcviren@gmail.com"
|
|
|
|
message := mg.NewMessage(sender, subject, body, recipient)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
defer cancel()
|
|
|
|
resp, id, err := mg.Send(ctx, message)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("ID: %s Resp: %s\n", id, resp)
|
|
}
|