debug and make mail work
This commit is contained in:
parent
50142e16be
commit
a51282eeec
@ -12,6 +12,7 @@ type RequestPayload struct {
|
|||||||
Action string `string:"action"`
|
Action string `string:"action"`
|
||||||
Auth AuthPayload `json:"auth,omitempty"`
|
Auth AuthPayload `json:"auth,omitempty"`
|
||||||
Log LogPayload `json:"log,omitempty"`
|
Log LogPayload `json:"log,omitempty"`
|
||||||
|
Mail MailPayload `json:"mail,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthPayload struct {
|
type AuthPayload struct {
|
||||||
@ -24,6 +25,13 @@ type LogPayload struct {
|
|||||||
Data string `json:"data"`
|
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) {
|
func (app *Config) Broker(w http.ResponseWriter, r *http.Request) {
|
||||||
payload := jsonResponse{
|
payload := jsonResponse{
|
||||||
Error: false,
|
Error: false,
|
||||||
@ -47,6 +55,8 @@ func (app *Config) HandleSubmission(w http.ResponseWriter, r *http.Request) {
|
|||||||
app.authenticate(w, requestPayload.Auth)
|
app.authenticate(w, requestPayload.Auth)
|
||||||
case "log":
|
case "log":
|
||||||
app.LogItem(w, requestPayload.Log)
|
app.LogItem(w, requestPayload.Log)
|
||||||
|
case "mail":
|
||||||
|
app.SendMail(w, requestPayload.Mail)
|
||||||
default:
|
default:
|
||||||
app.errorJSON(w, errors.New("unknown action"))
|
app.errorJSON(w, errors.New("unknown action"))
|
||||||
}
|
}
|
||||||
@ -81,6 +91,21 @@ func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) {
|
|||||||
app.callService(w, loggerService)
|
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 {
|
type statusError struct {
|
||||||
ExpectedCode int
|
ExpectedCode int
|
||||||
ErrCode int
|
ErrCode int
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
<a id="brokerBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Broker</a>
|
<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="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="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;">
|
<div id="output" class="mt-5" style="outline: 1px solid silver; padding: 2em;">
|
||||||
<span class="text-muted">Output shows here...</span>
|
<span class="text-muted">Output shows here...</span>
|
||||||
@ -37,7 +38,7 @@
|
|||||||
<script>
|
<script>
|
||||||
let brokerBtn = document.getElementById("brokerBtn");
|
let brokerBtn = document.getElementById("brokerBtn");
|
||||||
let authBrokerBtn = document.getElementById("authBrokerBtn");
|
let authBrokerBtn = document.getElementById("authBrokerBtn");
|
||||||
let logBtn = document.getElementById("logBtn");
|
let mailBtn = document.getElementById("mailBtn");
|
||||||
let output = document.getElementById("output");
|
let output = document.getElementById("output");
|
||||||
let sent = document.getElementById("payload");
|
let sent = document.getElementById("payload");
|
||||||
let received = document.getElementById("received");
|
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>
|
</script>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@ -13,37 +12,41 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MailData struct {
|
type MailData struct {
|
||||||
From string
|
From string `json:"from"`
|
||||||
To string
|
To string `json:"to"`
|
||||||
Subject string
|
Subject string `json:"subject"`
|
||||||
Content string
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Config) SendMail(w http.ResponseWriter, r *http.Request) {
|
func (app *Config) SendMail(w http.ResponseWriter, r *http.Request) {
|
||||||
var mailData MailData
|
var mailData MailData
|
||||||
err := json.NewDecoder(r.Body).Decode(&mailData)
|
err := app.Tools.ReadJSON(w, r, &mailData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
app.Tools.ErrorJSON(w, err)
|
app.Tools.ErrorJSON(w, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
log.Println("Sending mail")
|
||||||
|
|
||||||
if mailData.From == "" || mailData.To == "" ||
|
if mailData.From == "" || mailData.To == "" ||
|
||||||
(mailData.Subject == "" && mailData.Content == "") {
|
(mailData.Subject == "" && mailData.Content == "") {
|
||||||
log.Println("Invalid mail")
|
log.Println("Invalid mail")
|
||||||
app.Tools.ErrorJSON(w, errors.New("invalid mail"), http.StatusBadRequest)
|
app.Tools.ErrorJSON(w, errors.New("invalid mail"), http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = app.SendMsg(mailData)
|
err = app.SendMsg(mailData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
app.Tools.ErrorJSON(w, err)
|
app.Tools.ErrorJSON(w, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var payload toolbox.JSONResponse
|
var payload toolbox.JSONResponse
|
||||||
payload.Error = false
|
payload.Error = false
|
||||||
payload.Message = fmt.Sprintf("mail sent to %s", mailData.To)
|
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 {
|
func (app *Config) SendMsg(m MailData) error {
|
||||||
|
@ -21,7 +21,7 @@ type Config struct {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := Config{
|
app := Config{
|
||||||
Host: "localhost",
|
Host: "mailhog",
|
||||||
Port: 1025,
|
Port: 1025,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,5 +23,5 @@ func (app *Config) routes() http.Handler {
|
|||||||
mux.Use(middleware.Heartbeat("/ping"))
|
mux.Use(middleware.Heartbeat("/ping"))
|
||||||
|
|
||||||
mux.Post("/send-mail", app.SendMail)
|
mux.Post("/send-mail", app.SendMail)
|
||||||
return nil
|
return mux
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user