Compare commits
3 Commits
e047b35f77
...
a51282eeec
Author | SHA1 | Date | |
---|---|---|---|
a51282eeec | |||
50142e16be | |||
6630577399 |
@ -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}}
|
||||||
|
84
mail-service/cmd/api/handlers.go
Normal file
84
mail-service/cmd/api/handlers.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
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
|
||||||
|
}
|
40
mail-service/cmd/api/main.go
Normal file
40
mail-service/cmd/api/main.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
27
mail-service/cmd/api/routes.go
Normal file
27
mail-service/cmd/api/routes.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
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
|
||||||
|
}
|
16
mail-service/go.mod
Normal file
16
mail-service/go.mod
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
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
|
||||||
|
)
|
20
mail-service/go.sum
Normal file
20
mail-service/go.sum
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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=
|
8
mail-service/mail-service.dockerfile
Normal file
8
mail-service/mail-service.dockerfile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
FROM alpine:latest
|
||||||
|
|
||||||
|
RUN mkdir /app
|
||||||
|
|
||||||
|
COPY mailApp /app
|
||||||
|
|
||||||
|
CMD ["/app/mailApp"]
|
||||||
|
|
@ -2,6 +2,7 @@ FRONT_END_BINARY=frontApp
|
|||||||
BROKER_BINARY=brokerApp
|
BROKER_BINARY=brokerApp
|
||||||
AUTH_BINARY=authApp
|
AUTH_BINARY=authApp
|
||||||
LOGGER_BINARY=loggerApp
|
LOGGER_BINARY=loggerApp
|
||||||
|
MAIL_BINARY=mailApp
|
||||||
|
|
||||||
## up: starts all containers in the background without forcing build
|
## up: starts all containers in the background without forcing build
|
||||||
up:
|
up:
|
||||||
@ -10,7 +11,7 @@ up:
|
|||||||
@echo "Docker images started!"
|
@echo "Docker images started!"
|
||||||
|
|
||||||
## up_build: stops docker-compose (if running), builds all projects and starts docker compose
|
## up_build: stops docker-compose (if running), builds all projects and starts docker compose
|
||||||
up_build: build_broker build_auth build_logger
|
up_build: build_broker build_auth build_logger build_mail
|
||||||
@echo "Stopping docker images (if running...)"
|
@echo "Stopping docker images (if running...)"
|
||||||
docker compose down
|
docker compose down
|
||||||
@echo "Building (when required) and starting docker images..."
|
@echo "Building (when required) and starting docker images..."
|
||||||
@ -41,6 +42,12 @@ build_logger:
|
|||||||
cd ../logger-service && env GOOS=linux CGO_ENABLED=0 go build -o ${LOGGER_BINARY} ./cmd/api
|
cd ../logger-service && env GOOS=linux CGO_ENABLED=0 go build -o ${LOGGER_BINARY} ./cmd/api
|
||||||
@echo "Done!"
|
@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: builds the frone end binary
|
||||||
build_front:
|
build_front:
|
||||||
@echo "Building front end binary..."
|
@echo "Building front end binary..."
|
||||||
|
@ -31,8 +31,17 @@ services:
|
|||||||
mode: replicated
|
mode: replicated
|
||||||
replicas: 1
|
replicas: 1
|
||||||
|
|
||||||
|
mail-service:
|
||||||
|
build:
|
||||||
|
context: ./../mail-service/
|
||||||
|
dockerfile: ./../mail-service/mail-service.dockerfile
|
||||||
|
restart: always
|
||||||
|
deploy:
|
||||||
|
mode: replicated
|
||||||
|
replicas: 1
|
||||||
|
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres
|
image: postgres:16.4-alpine
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
restart: always
|
restart: always
|
||||||
@ -56,6 +65,22 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./db-data/mongo:/data/db
|
- ./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:
|
adminer:
|
||||||
image: adminer
|
image: adminer
|
||||||
restart: always
|
restart: always
|
||||||
|
Loading…
x
Reference in New Issue
Block a user