From a51282eeec61c3435b20a53edcd36ede6840f55b Mon Sep 17 00:00:00 2001 From: vinchent Date: Mon, 2 Sep 2024 21:32:59 +0200 Subject: [PATCH] debug and make mail work --- broker-service/cmd/api/handlers.go | 25 +++++++++++++ front-end/cmd/web/templates/test.page.gohtml | 37 +++++++++++++++++++- mail-service/cmd/api/handlers.go | 17 +++++---- mail-service/cmd/api/main.go | 2 +- mail-service/cmd/api/routes.go | 2 +- 5 files changed, 73 insertions(+), 10 deletions(-) diff --git a/broker-service/cmd/api/handlers.go b/broker-service/cmd/api/handlers.go index 02620aa..e012054 100644 --- a/broker-service/cmd/api/handlers.go +++ b/broker-service/cmd/api/handlers.go @@ -12,6 +12,7 @@ type RequestPayload struct { Action string `string:"action"` Auth AuthPayload `json:"auth,omitempty"` Log LogPayload `json:"log,omitempty"` + Mail MailPayload `json:"mail,omitempty"` } type AuthPayload struct { @@ -24,6 +25,13 @@ type LogPayload struct { Data string `json:"data"` } +type MailPayload struct { + From string `json:"from"` + To string `json:"to"` + Subject string `json:"subject"` + Content string `json:"content"` +} + func (app *Config) Broker(w http.ResponseWriter, r *http.Request) { payload := jsonResponse{ Error: false, @@ -47,6 +55,8 @@ func (app *Config) HandleSubmission(w http.ResponseWriter, r *http.Request) { app.authenticate(w, requestPayload.Auth) case "log": app.LogItem(w, requestPayload.Log) + case "mail": + app.SendMail(w, requestPayload.Mail) default: app.errorJSON(w, errors.New("unknown action")) } @@ -81,6 +91,21 @@ func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) { app.callService(w, loggerService) } +func (app *Config) SendMail(w http.ResponseWriter, entry MailPayload) { + log.Println(entry) + mailService := Microservice{ + Input: entry, + Addr: "http://mail-service/send-mail", + ErrCode: statusError{ + ExpectedCode: http.StatusAccepted, + ErrCode: http.StatusInternalServerError, + Err: errors.New("internal error"), + }, + SuccessMsg: "Mail sent!", + } + app.callService(w, mailService) +} + type statusError struct { ExpectedCode int ErrCode int diff --git a/front-end/cmd/web/templates/test.page.gohtml b/front-end/cmd/web/templates/test.page.gohtml index 8ebccf7..50bc01f 100644 --- a/front-end/cmd/web/templates/test.page.gohtml +++ b/front-end/cmd/web/templates/test.page.gohtml @@ -10,6 +10,7 @@ Test Broker Test Auth Test Log + Test Mail
Output shows here... @@ -37,7 +38,7 @@ {{end}} diff --git a/mail-service/cmd/api/handlers.go b/mail-service/cmd/api/handlers.go index 5c8576a..be423cc 100644 --- a/mail-service/cmd/api/handlers.go +++ b/mail-service/cmd/api/handlers.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "errors" "fmt" "log" @@ -13,37 +12,41 @@ import ( ) type MailData struct { - From string - To string - Subject string - Content string + From string `json:"from"` + To string `json:"to"` + Subject string `json:"subject"` + Content string `json:"content"` } func (app *Config) SendMail(w http.ResponseWriter, r *http.Request) { var mailData MailData - err := json.NewDecoder(r.Body).Decode(&mailData) + err := app.Tools.ReadJSON(w, r, &mailData) if err != nil { log.Println(err) app.Tools.ErrorJSON(w, err) + return } + log.Println("Sending mail") if mailData.From == "" || mailData.To == "" || (mailData.Subject == "" && mailData.Content == "") { log.Println("Invalid mail") app.Tools.ErrorJSON(w, errors.New("invalid mail"), http.StatusBadRequest) + return } err = app.SendMsg(mailData) if err != nil { log.Println(err) app.Tools.ErrorJSON(w, err) + return } var payload toolbox.JSONResponse payload.Error = false payload.Message = fmt.Sprintf("mail sent to %s", mailData.To) - app.Tools.WriteJSON(w, http.StatusOK, payload) + app.Tools.WriteJSON(w, http.StatusAccepted, payload) } func (app *Config) SendMsg(m MailData) error { diff --git a/mail-service/cmd/api/main.go b/mail-service/cmd/api/main.go index 3f3b43a..2b5cb11 100644 --- a/mail-service/cmd/api/main.go +++ b/mail-service/cmd/api/main.go @@ -21,7 +21,7 @@ type Config struct { func main() { app := Config{ - Host: "localhost", + Host: "mailhog", Port: 1025, } diff --git a/mail-service/cmd/api/routes.go b/mail-service/cmd/api/routes.go index c0c7d1a..c2e2cd2 100644 --- a/mail-service/cmd/api/routes.go +++ b/mail-service/cmd/api/routes.go @@ -23,5 +23,5 @@ func (app *Config) routes() http.Handler { mux.Use(middleware.Heartbeat("/ping")) mux.Post("/send-mail", app.SendMail) - return nil + return mux }