From 8de5cc89c5be4ac3c69c50675dfee5c3e4fac3a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=A1n=20C=20McCord?= Date: Wed, 16 May 2018 14:24:50 -0400 Subject: [PATCH] better logging --- app/controllers/contact.go | 21 +++++++++------------ conf/app.conf | 2 +- conf/routes | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/controllers/contact.go b/app/controllers/contact.go index 8027371..1b8c4ff 100644 --- a/app/controllers/contact.go +++ b/app/controllers/contact.go @@ -15,12 +15,6 @@ import ( var contactEmailT *template.Template -// EmailContact describes an email EmailContact -type EmailContact struct { - Name string `json:"name"` - Email string `json:"email"` -} - func init() { contactEmailT = template.Must(template.New("contactEmail").Parse(contactEmailTemplate)) } @@ -30,10 +24,12 @@ type Contact struct { *revel.Controller } -// Request handles a customer contact request -func (c Contact) Request(name, email string) revel.Result { +// ContactRequest handles a customer contact request +func (c Contact) ContactRequest(name, email string) revel.Result { - emailBody, err := renderContactEmail(name, email) + c.Log.Info("received contact request", "name", name, "email", email, "source", c.ClientIP) + + emailBody, err := c.renderContactEmail(name, email) if err != nil { return c.Controller.RenderError(errors.Wrap(err, "failed to render email for contact request")) } @@ -43,7 +39,7 @@ func (c Contact) Request(name, email string) revel.Result { Name: "CyCore Systems, Inc", Email: "sys@cycoresys.com", }, - To: getEmailContacts(), + To: c.getEmailContacts(), Subject: "Contact Request", HTMLContent: emailBody, Tags: []string{"contact-request"}, @@ -56,7 +52,7 @@ func (c Contact) Request(name, email string) revel.Result { return c.Redirect(routes.App.Index()) } -func renderContactEmail(name, email string) (string, error) { +func (c Contact) renderContactEmail(name, email string) (string, error) { buf := new(bytes.Buffer) err := contactEmailT.Execute(buf, struct { Name string @@ -73,12 +69,13 @@ func renderContactEmail(name, email string) (string, error) { return buf.String(), nil } -func getEmailContacts() []sendinblue.Address { +func (c Contact) getEmailContacts() []sendinblue.Address { var ret []sendinblue.Address if err := json.Unmarshal([]byte(os.Getenv("CONTACT_RECIPIENTS")), &ret); err != nil { // Fall back to default if we fail to load from environment + c.Log.Warn("failed to load recipients from environment", "error", err) ret = append(ret, sendinblue.Address{ Name: "System Receiver", Email: "sys@cycoresys.com", diff --git a/conf/app.conf b/conf/app.conf index c99408e..089c9df 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -20,7 +20,7 @@ app.behind.proxy = false # The IP address on which to listen. -http.addr = +http.addr = 0.0.0.0 # The port on which to listen. http.port = 9000 diff --git a/conf/routes b/conf/routes index 034b5f5..0b8977f 100644 --- a/conf/routes +++ b/conf/routes @@ -7,7 +7,7 @@ module:testrunner GET / App.Index # Handle contact requests -POST /contact/request Contact.Request +POST /contact/request Contact.ContactRequest # Ignore favicon requests GET /favicon.ico 404