debug and make mail work

This commit is contained in:
vinchent 2024-09-02 21:32:59 +02:00
parent 50142e16be
commit a51282eeec
5 changed files with 73 additions and 10 deletions

View File

@ -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

View File

@ -10,6 +10,7 @@
<a id="brokerBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Broker</a>
<a id="authBrokerBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Auth</a>
<a id="logBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Log</a>
<a id="mailBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Mail</a>
<div id="output" class="mt-5" style="outline: 1px solid silver; padding: 2em;">
<span class="text-muted">Output shows here...</span>
@ -37,7 +38,7 @@
<script>
let brokerBtn = document.getElementById("brokerBtn");
let authBrokerBtn = document.getElementById("authBrokerBtn");
let logBtn = document.getElementById("logBtn");
let mailBtn = document.getElementById("mailBtn");
let output = document.getElementById("output");
let sent = document.getElementById("payload");
let received = document.getElementById("received");
@ -126,5 +127,39 @@
});
});
mailBtn.addEventListener("click", () => {
const payload = {
action: "mail",
mail: {
from: "admin@example.com",
to: "me@here.com",
subject: "This is not a phishing mail",
content: "Send me money now!",
}
};
const headers = new Headers();
headers.append("Content-Type", "application/json");
const body = {
method: 'POST',
body: JSON.stringify(payload),
headers: headers,
}
fetch("http:\/\/localhost:8080/handle", body)
.then((response) => response.json())
.then((data) => {
sent.innerHTML = JSON.stringify(payload, undefined, 4);
received.innerHTML = JSON.stringify(data, undefined, 4);
if (data.error) {
console.log(data.message);
output.innerHTML += `<br><strong>Error:</strong>: ${data.message}`;
} else {
output.innerHTML += `<br><strong>Response from broker service</strong>: ${data.message}`;
}
})
.catch((error) => {
output.innerHTML += "<br><br>Error: " + error;
});
});
</script>
{{end}}

View File

@ -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 {

View File

@ -21,7 +21,7 @@ type Config struct {
func main() {
app := Config{
Host: "localhost",
Host: "mailhog",
Port: 1025,
}

View File

@ -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
}