diff --git a/aws/ses.go b/aws/ses.go index 771dd86..d60cf99 100644 --- a/aws/ses.go +++ b/aws/ses.go @@ -15,25 +15,34 @@ var ( // SESEmail represents an email context type SESEmail struct { - To string - Cc string `default:""` - Bcc string `default:""` - Subject string - Body string + To string `json:"to"` + Cc string `json:"cc" default:""` + Bcc string `json:"bcc" default:""` + Subject string `json:"subject"` + Body string `json:"body"` } -// NewSESEmail returns an initialized SES email context -func NewSESEmail(to, cc, bcc, subject, body string) *SESEmail { +// NewSESEmail returns an initialized SES email context. +func NewSESEmail() *SESEmail { // Load assumed user role AWS configuration cfg := AssumedRoleConfig() // Initialize AWS Simple Email Service Client client = sesv2.NewFromConfig(cfg) - return &SESEmail{To: to, Cc: cc, Bcc: bcc, Subject: subject, Body: body} + return &SESEmail{} } -// Send sends an email using AWS SES +// BuildMessage builds email context. +func (e *SESEmail) BuildMessage(to, cc, bcc, subject, body string) { + e.To = to + e.Cc = cc + e.Bcc = bcc + e.Subject = subject + e.Body = body +} + +// Send sends an email using AWS SES. func (e *SESEmail) Send() error { - // Build email context + // Input email content input := &sesv2.SendEmailInput{ FromEmailAddress: &from, Destination: &types.Destination{ diff --git a/controllers/controllers.go b/controllers/controllers.go index 0744f81..cfcd587 100644 --- a/controllers/controllers.go +++ b/controllers/controllers.go @@ -1,11 +1,39 @@ package controllers import ( + "encoding/json" "fmt" "net/http" + + "github.com/cisagov/con-pca-tasks/aws" + "github.com/go-chi/chi/v5" ) -// HealthCheckHandler indicates that the server is up and running. -func HealthCheckHandler(w http.ResponseWriter, r *http.Request) { +// healthCheckHandler indicates that the server is up and running. +func healthCheckHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Up and running!") } + +// notificationEmailsHandler manages notificaiton emails. +func notificationEmailsHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + decoder := json.NewDecoder(r.Body) + + var e aws.SESEmail + decoder.Decode(&e) + + email := aws.NewSESEmail() + email.BuildMessage(e.To, e.Cc, e.Bcc, e.Subject, e.Body) + email.Send() + + fmt.Fprintf(w, "Notification email sent!") +} + +func TasksRouter() http.Handler { + r := chi.NewRouter() + r.Get("/", healthCheckHandler) + r.Post("/notifications/send", notificationEmailsHandler) + + return r +} diff --git a/controllers/controllers_test.go b/controllers/controllers_test.go index 98ed3b6..701e130 100644 --- a/controllers/controllers_test.go +++ b/controllers/controllers_test.go @@ -8,9 +8,9 @@ import ( ) func TestHealthCheckHandler(t *testing.T) { - req := httptest.NewRequest(http.MethodGet, "/", nil) + req := httptest.NewRequest(http.MethodGet, "/tasks", nil) w := httptest.NewRecorder() - HealthCheckHandler(w, req) + healthCheckHandler(w, req) res := w.Result() defer res.Body.Close() data, err := io.ReadAll(res.Body) @@ -21,3 +21,18 @@ func TestHealthCheckHandler(t *testing.T) { t.Errorf("expected 'Up and running!', but got %v", string(data)) } } + +func TestNotificationEmailsHandler(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/tasks", nil) + w := httptest.NewRecorder() + notificationEmailsHandler(w, req) + res := w.Result() + defer res.Body.Close() + data, err := io.ReadAll(res.Body) + if err != nil { + t.Errorf("expected error to be nil got %v", err) + } + if string(data) != "Notification email sent!" { + t.Errorf("expected 'Notification email sent!', but got %v", string(data)) + } +} diff --git a/main.go b/main.go index 1405cd2..532094f 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ func main() { version() mux := chi.NewRouter() - mux.Get("/", controllers.HealthCheckHandler) + mux.Mount("/tasks", controllers.TasksRouter()) port := ":8080" log.Printf("listening on port %s, version %s", port, Version)