add mail service
This commit is contained in:
parent
6630577399
commit
50142e16be
81
mail-service/cmd/api/handlers.go
Normal file
81
mail-service/cmd/api/handlers.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/tsawler/toolbox"
|
||||||
|
mail "github.com/xhit/go-simple-mail/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MailData struct {
|
||||||
|
From string
|
||||||
|
To string
|
||||||
|
Subject string
|
||||||
|
Content string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *Config) SendMail(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var mailData MailData
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&mailData)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
app.Tools.ErrorJSON(w, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mailData.From == "" || mailData.To == "" ||
|
||||||
|
(mailData.Subject == "" && mailData.Content == "") {
|
||||||
|
log.Println("Invalid mail")
|
||||||
|
app.Tools.ErrorJSON(w, errors.New("invalid mail"), http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = app.SendMsg(mailData)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
app.Tools.ErrorJSON(w, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload toolbox.JSONResponse
|
||||||
|
payload.Error = false
|
||||||
|
payload.Message = fmt.Sprintf("mail sent to %s", mailData.To)
|
||||||
|
|
||||||
|
app.Tools.WriteJSON(w, http.StatusOK, 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: "localhost",
|
||||||
|
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 nil
|
||||||
|
}
|
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,6 +31,15 @@ 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:16.4-alpine
|
image: postgres:16.4-alpine
|
||||||
ports:
|
ports:
|
||||||
|
Loading…
Reference in New Issue
Block a user