From 41f504d10c730dd589cf0e67bb0d24e27c2bcbb5 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sun, 1 May 2022 13:57:30 -0400 Subject: [PATCH] add contact endpoint --- go.mod | 1 + go.sum | 2 ++ internal/api/api.go | 2 ++ internal/api/contact.go | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 internal/api/contact.go diff --git a/go.mod b/go.mod index 1106b9a..6241748 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/joho/godotenv v1.4.0 github.com/sideshow/apns2 v0.23.0 github.com/sirupsen/logrus v1.8.1 + github.com/smtp2go-oss/smtp2go-go v1.0.1 // indirect github.com/spf13/cobra v1.4.0 github.com/stretchr/testify v1.7.1 github.com/valyala/fastjson v1.6.3 diff --git a/go.sum b/go.sum index ddf98f8..4a5576e 100644 --- a/go.sum +++ b/go.sum @@ -296,6 +296,8 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/smtp2go-oss/smtp2go-go v1.0.1 h1:rwcoNLjOyigOzCjKp/guylKY/xJpoeypSxgtcC/g6DA= +github.com/smtp2go-oss/smtp2go-go v1.0.1/go.mod h1:lkv36awQXRBWAvnd517FFESKvne8465KCu90lPThcEY= github.com/soveran/redisurl v0.0.0-20180322091936-eb325bc7a4b8/go.mod h1:FVJ8jbHu7QrNFs3bZEsv/L5JjearIAY9N0oXh2wk+6Y= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/cobra v0.0.2/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= diff --git a/internal/api/api.go b/internal/api/api.go index ecab0a7..1766c31 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -107,6 +107,8 @@ func (a *api) Routes() *mux.Router { r.HandleFunc("/v1/receipt", a.checkReceiptHandler).Methods("POST") r.HandleFunc("/v1/receipt/{apns}", a.checkReceiptHandler).Methods("POST") + r.HandleFunc("/v1/contact", a.contactHandler).Methods("POST") + r.HandleFunc("/v1/test/bugsnag", a.testBugsnagHandler).Methods("POST") r.Use(a.loggingMiddleware) diff --git a/internal/api/contact.go b/internal/api/contact.go new file mode 100644 index 0000000..9a0d62f --- /dev/null +++ b/internal/api/contact.go @@ -0,0 +1,35 @@ +package api + +import ( + "encoding/json" + "net/http" + + "github.com/smtp2go-oss/smtp2go-go" +) + +type sendMessageRequest struct { + Title string `json:"title"` + Body string `json:"body"` +} + +func (a *api) contactHandler(w http.ResponseWriter, r *http.Request) { + smr := &sendMessageRequest{} + if err := json.NewDecoder(r.Body).Decode(smr); err != nil { + a.errorResponse(w, r, 500, err.Error()) + return + } + + msg := &smtp2go.Email{ + From: "🤖 Apollo API ", + To: []string{"ultrasurvey@apolloapp.io"}, + Subject: smr.Title, + TextBody: smr.Body, + } + + _, err := smtp2go.Send(msg) + if err != nil { + a.errorResponse(w, r, 500, err.Error()) + return + } + w.WriteHeader(http.StatusOK) +}