Compare commits
No commits in common. "a51282eeec61c3435b20a53edcd36ede6840f55b" and "e047b35f774601a232fcf4b413405a032b864c8f" have entirely different histories.
a51282eeec
...
e047b35f77
@ -12,7 +12,6 @@ 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 {
|
||||
@ -25,13 +24,6 @@ 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,
|
||||
@ -55,8 +47,6 @@ 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"))
|
||||
}
|
||||
@ -91,21 +81,6 @@ 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
|
||||
|
@ -10,7 +10,6 @@
|
||||
<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>
|
||||
@ -38,7 +37,7 @@
|
||||
<script>
|
||||
let brokerBtn = document.getElementById("brokerBtn");
|
||||
let authBrokerBtn = document.getElementById("authBrokerBtn");
|
||||
let mailBtn = document.getElementById("mailBtn");
|
||||
let logBtn = document.getElementById("logBtn");
|
||||
let output = document.getElementById("output");
|
||||
let sent = document.getElementById("payload");
|
||||
let received = document.getElementById("received");
|
||||
@ -127,39 +126,5 @@
|
||||
});
|
||||
});
|
||||
|
||||
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}}
|
||||
|
@ -1,84 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/tsawler/toolbox"
|
||||
mail "github.com/xhit/go-simple-mail/v2"
|
||||
)
|
||||
|
||||
type MailData struct {
|
||||
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 := 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.StatusAccepted, payload)
|
||||
}
|
||||
|
||||
func (app *Config) SendMsg(m MailData) error {
|
||||
server := mail.NewSMTPClient()
|
||||
server.Host = app.Host
|
||||
server.Port = app.Port
|
||||
|
||||
server.KeepAlive = false
|
||||
server.ConnectTimeout = 10 * time.Second
|
||||
server.SendTimeout = 10 * time.Second
|
||||
|
||||
smtpClient, err := server.Connect()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Maybe add a template
|
||||
|
||||
email := mail.NewMSG()
|
||||
email.SetFrom(m.From).
|
||||
AddTo(m.To).
|
||||
SetSubject(m.Subject).
|
||||
SetBody(mail.TextHTML, m.Content)
|
||||
|
||||
err = email.Send(smtpClient)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Email sent to %s", m.To)
|
||||
|
||||
return nil
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/tsawler/toolbox"
|
||||
)
|
||||
|
||||
const webPort = "80"
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port int
|
||||
UserName string
|
||||
Password string
|
||||
Encryption string
|
||||
Tools toolbox.Tools
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := Config{
|
||||
Host: "mailhog",
|
||||
Port: 1025,
|
||||
}
|
||||
|
||||
log.Printf("Starting mail service on port %s\n", webPort)
|
||||
|
||||
// define http server
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%s", webPort),
|
||||
Handler: app.routes(),
|
||||
}
|
||||
|
||||
err := srv.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
)
|
||||
|
||||
func (app *Config) routes() http.Handler {
|
||||
mux := chi.NewRouter()
|
||||
|
||||
mux.Use(cors.Handler(cors.Options{
|
||||
AllowedOrigins: []string{"https://*", "http://*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
ExposedHeaders: []string{"Link"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 300,
|
||||
}))
|
||||
|
||||
mux.Use(middleware.Heartbeat("/ping"))
|
||||
|
||||
mux.Post("/send-mail", app.SendMail)
|
||||
return mux
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
module mail
|
||||
|
||||
go 1.22.5
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.1.0
|
||||
github.com/go-chi/cors v1.2.1
|
||||
github.com/tsawler/toolbox v1.3.1
|
||||
github.com/xhit/go-simple-mail/v2 v2.16.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-test/deep v1.1.1 // indirect
|
||||
github.com/stretchr/testify v1.9.0 // indirect
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect
|
||||
)
|
@ -1,20 +0,0 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
||||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
|
||||
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
|
||||
github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
|
||||
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 h1:PM5hJF7HVfNWmCjMdEfbuOBNXSVF2cMFGgQTPdKCbwM=
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208/go.mod h1:BzWtXXrXzZUvMacR0oF/fbDDgUPO8L36tDMmRAf14ns=
|
||||
github.com/tsawler/toolbox v1.3.1 h1:zqnt5L5dmWiBrs2JgE1VeHJJO/IMStFKQgWxc+eriEE=
|
||||
github.com/tsawler/toolbox v1.3.1/go.mod h1:bYUEtJ09HFx534XcjXdTIzv7MCKsg9SrhSGELFe6HI4=
|
||||
github.com/xhit/go-simple-mail/v2 v2.16.0 h1:ouGy/Ww4kuaqu2E2UrDw7SvLaziWTB60ICLkIkNVccA=
|
||||
github.com/xhit/go-simple-mail/v2 v2.16.0/go.mod h1:b7P5ygho6SYE+VIqpxA6QkYfv4teeyG4MKqB3utRu98=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
@ -1,8 +0,0 @@
|
||||
FROM alpine:latest
|
||||
|
||||
RUN mkdir /app
|
||||
|
||||
COPY mailApp /app
|
||||
|
||||
CMD ["/app/mailApp"]
|
||||
|
@ -2,7 +2,6 @@ FRONT_END_BINARY=frontApp
|
||||
BROKER_BINARY=brokerApp
|
||||
AUTH_BINARY=authApp
|
||||
LOGGER_BINARY=loggerApp
|
||||
MAIL_BINARY=mailApp
|
||||
|
||||
## up: starts all containers in the background without forcing build
|
||||
up:
|
||||
@ -11,7 +10,7 @@ up:
|
||||
@echo "Docker images started!"
|
||||
|
||||
## up_build: stops docker-compose (if running), builds all projects and starts docker compose
|
||||
up_build: build_broker build_auth build_logger build_mail
|
||||
up_build: build_broker build_auth build_logger
|
||||
@echo "Stopping docker images (if running...)"
|
||||
docker compose down
|
||||
@echo "Building (when required) and starting docker images..."
|
||||
@ -42,12 +41,6 @@ build_logger:
|
||||
cd ../logger-service && env GOOS=linux CGO_ENABLED=0 go build -o ${LOGGER_BINARY} ./cmd/api
|
||||
@echo "Done!"
|
||||
|
||||
## build_mail: builds the mail binary as a linux executable
|
||||
build_mail:
|
||||
@echo "Building mail binary..."
|
||||
cd ../mail-service && env GOOS=linux CGO_ENABLED=0 go build -o ${MAIL_BINARY} ./cmd/api
|
||||
@echo "Done!"
|
||||
|
||||
## build_front: builds the frone end binary
|
||||
build_front:
|
||||
@echo "Building front end binary..."
|
||||
|
@ -31,17 +31,8 @@ services:
|
||||
mode: replicated
|
||||
replicas: 1
|
||||
|
||||
mail-service:
|
||||
build:
|
||||
context: ./../mail-service/
|
||||
dockerfile: ./../mail-service/mail-service.dockerfile
|
||||
restart: always
|
||||
deploy:
|
||||
mode: replicated
|
||||
replicas: 1
|
||||
|
||||
postgres:
|
||||
image: postgres:16.4-alpine
|
||||
image: postgres
|
||||
ports:
|
||||
- "5432:5432"
|
||||
restart: always
|
||||
@ -65,22 +56,6 @@ services:
|
||||
volumes:
|
||||
- ./db-data/mongo:/data/db
|
||||
|
||||
mailhog:
|
||||
image: mailhog/mailhog
|
||||
ports:
|
||||
- "1025:1025"
|
||||
- "8025:8025"
|
||||
|
||||
rabbitmq:
|
||||
image: 'rabbitmq:3.13-alpine'
|
||||
ports:
|
||||
- "5672:5672"
|
||||
deploy:
|
||||
mode: replicated
|
||||
replicas: 1
|
||||
volumes:
|
||||
- ./db-data/rabbitmq:/var/lib/rabbitmq/
|
||||
|
||||
adminer:
|
||||
image: adminer
|
||||
restart: always
|
||||
|
Loading…
x
Reference in New Issue
Block a user