pidgin/carrier

a78bd0a5531e
Parents d67b5771ee54
Children 3fe33eeb5a02
A bunch more work, potentially working, but need to deploy to test
--- a/Dockerfile Sun May 12 05:00:08 2019 -0500
+++ b/Dockerfile Tue May 14 21:38:27 2019 -0500
@@ -1,4 +1,4 @@
-FROM golang:stretch as build
+FROM golang:stretch AS build
WORKDIR /app
@@ -11,4 +11,3 @@
CMD ["/carrier"]
COPY --from=build /app/carrier /carrier
-
--- a/config/config.go Sun May 12 05:00:08 2019 -0500
+++ b/config/config.go Tue May 14 21:38:27 2019 -0500
@@ -9,8 +9,11 @@
UptimeRobotToken string `envconfig:"UPTIME_ROBOT_TOKEN" required:"true"`
- TwilioAccountSID string `envconfig:"TWILIO_ACCOUNT_SID" required:"true"`
- TwilioAuthToken string `envconfig:"TWILIO_AUTH_TOKEN" required:"true"`
+ TwilioWebhookToken string `envconfig:"TWILIO_WEBHOOK_TOKEN" required:"true"`
+ TwilioAccountSID string `envconfig:"TWILIO_ACCOUNT_SID" required:"true"`
+ TwilioAuthToken string `envconfig:"TWILIO_AUTH_TOKEN" required:"true"`
+ TwilioPhoneNumber string `envconfig:"TWILIO_PHONE_NUMBER" required:"true"`
+ TwilioRecipient string `envconfig:"TWILIO_RECIPIENT" required:"true"`
}
var (
--- a/go.mod Sun May 12 05:00:08 2019 -0500
+++ b/go.mod Tue May 14 21:38:27 2019 -0500
@@ -1,6 +1,7 @@
module bitbucket.org/pidgin/carrier
require (
+ bitbucket.org/ckvist/twilio v0.0.0-20170512072134-13c593a1721b
github.com/kelseyhightower/envconfig v1.3.0
github.com/sirupsen/logrus v1.4.1
)
--- a/go.sum Sun May 12 05:00:08 2019 -0500
+++ b/go.sum Tue May 14 21:38:27 2019 -0500
@@ -1,3 +1,5 @@
+bitbucket.org/ckvist/twilio v0.0.0-20170512072134-13c593a1721b h1:EgVTZyg+IROzDLG86gbfoLk4k6nwypmTHmjXDgYMzD4=
+bitbucket.org/ckvist/twilio v0.0.0-20170512072134-13c593a1721b/go.mod h1:tZckALWIGoGfdoe/aKzxBL5qViz4qXogqZx1UViXWME=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kelseyhightower/envconfig v1.3.0 h1:IvRS4f2VcIQy6j4ORGIf9145T/AsUB+oY8LyvN8BXNM=
github.com/kelseyhightower/envconfig v1.3.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
--- a/server/server.go Sun May 12 05:00:08 2019 -0500
+++ b/server/server.go Tue May 14 21:38:27 2019 -0500
@@ -4,6 +4,8 @@
"net/http"
log "github.com/sirupsen/logrus"
+
+ "bitbucket.org/pidgin/carrier/uptimerobot"
)
type Server struct {
@@ -32,7 +34,7 @@
mux := http.NewServeMux()
mux.HandleFunc("/healthz", healthzHandler)
- mux.HandleFunc("/uptime-robot", uptimeRobotHandler)
+ mux.HandleFunc("/uptime-robot", uptimerobot.Handler)
return &Server{
errChan: errChan,
--- a/server/uptimerobot.go Sun May 12 05:00:08 2019 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-package server
-
-import (
- "net/http"
-)
-
-// monitorID=*monitorID*&monitorURL=*monitorURL*&monitorFriendlyName=*monitorFriendlyName*&alertType=*alertType*&alertTypeFriendlyName=*alertTypeFriendlyName*&alertDetails=*alertDetails*&alertDuration=*alertDuration*&monitorAlertContacts=*monitorAlertContacts*
-
-func uptimeRobotHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- w.WriteHeader(http.StatusMethodNotAllowed)
- return
- }
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/uptimerobot/uptimerobot.go Tue May 14 21:38:27 2019 -0500
@@ -0,0 +1,83 @@
+package uptimerobot
+
+import (
+ "fmt"
+ "net/http"
+
+ "bitbucket.org/ckvist/twilio/twirest"
+
+ "bitbucket.org/pidgin/carrier/config"
+)
+
+var (
+ client *twirest.TwilioClient
+)
+
+func validate(r *http.Request) int {
+ cfg := config.Get()
+
+ if r.Method != http.MethodPost {
+ return http.StatusMethodNotAllowed
+ }
+
+ err := r.ParseForm()
+ if err != nil {
+ return http.StatusBadRequest
+ }
+
+ // check that we have a token
+ tokens, found := r.Form["token"]
+ if !found || len(tokens) == 0 {
+ return http.StatusBadRequest
+ }
+
+ // verify the first token
+ if tokens[0] != cfg.TwilioWebhookToken {
+ return http.StatusBadRequest
+ }
+
+ return 0
+}
+
+func sendSMS(content string) (twirest.TwilioResponse, error) {
+ cfg := config.Get()
+
+ if client == nil {
+ client = twirest.NewClient(cfg.TwilioAccountSID, cfg.TwilioAuthToken)
+ }
+
+ // create the sms
+ msg := twirest.SendMessage{
+ Text: content,
+ To: cfg.TwilioRecipient,
+ From: cfg.TwilioPhoneNumber,
+ }
+
+ return client.Request(msg)
+}
+
+func Handler(w http.ResponseWriter, r *http.Request) {
+ // validate that the request has everything we need/want
+ if status := validate(r); status != 0 {
+ w.WriteHeader(status)
+ return
+ }
+
+ // build the content of our sms
+ content := fmt.Sprintf(
+ "%s is %s",
+ r.PostForm["monitorFriendlyName"][0],
+ r.PostForm["alertTypeFriendlyName"][0],
+ )
+
+ // now send the sms
+ resp, err := sendSMS(content)
+ if err != nil {
+ w.WriteHeader(resp.Status.Http)
+ fmt.Fprintf(w, "%s", err)
+ return
+ }
+
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprintf(w, "%s", resp.Status)
+}