Added rabbitmq

master
RuisPipe 2021-06-25 21:46:32 +02:00
parent d41a73cb9a
commit 9a1f66b92c
9 changed files with 127 additions and 16 deletions

View File

@ -8,4 +8,12 @@ port = "25"
user = "no-reply@roese.dev"
password = "mhtWMUn049o25xYTLUpfplX5Ze3jRfqh"
from = "App <no-reply@roese.dev>"
templatePath = "./templates/"
[rabbitmq]
host = "localhost:5672"
username = "guest"
password = "guest"
[templates]
folderPath = "./templates/"
configPath = "./templates/templates.json"

1
go.mod
View File

@ -6,4 +6,5 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/gofiber/fiber/v2 v2.12.0
github.com/sirupsen/logrus v1.8.1
github.com/streadway/amqp v1.0.0 // indirect
)

2
go.sum
View File

@ -13,6 +13,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

11
main.go
View File

@ -3,13 +3,12 @@ package main
import (
"git.umbach.dev/app-idea/mailer/modules/config"
"git.umbach.dev/app-idea/mailer/modules/mailer"
"git.umbach.dev/app-idea/mailer/routers/router"
"github.com/gofiber/fiber/v2"
"git.umbach.dev/app-idea/mailer/modules/rabbitmq"
log "github.com/sirupsen/logrus"
)
func main() {
app := fiber.New()
//app := fiber.New()
config.LoadConfig()
@ -21,7 +20,9 @@ func main() {
mailer.InitMailer()
router.SetupRoutes(app)
//router.SetupRoutes(app)
app.Listen(cfg.Host)
rabbitmq.Init()
//app.Listen(cfg.Host)
}

View File

@ -9,6 +9,7 @@ var Cfg Config
type Config struct {
Server server
Mail mail
RabbitMq rabbitmq
Templates templates
}
@ -26,6 +27,12 @@ type mail struct {
TemplatePath string
}
type rabbitmq struct {
Host string
Username string
Password string
}
type templates struct {
FolderPath string
ConfigPath string

View File

@ -44,7 +44,7 @@ func NewMail(to []string, templateId int, languageId int, bodyData interface{})
body, err := mail.RenderTemplate()
log.Infoln("body", body, bodyData)
log.Infoln("get body")
if err != nil {
log.Fatalln("error parsing", err)

View File

@ -0,0 +1,85 @@
package rabbitmq
import (
"encoding/json"
"fmt"
"git.umbach.dev/app-idea/mailer/modules/config"
"git.umbach.dev/app-idea/mailer/modules/mailer"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
var Conn *amqp.Connection
var MailQueue amqp.Queue
var Channel *amqp.Channel
type MailInput struct {
Key string `json:"k"`
Mail string `json:"m"`
TemplateId int `json:"t"`
LanguageId int `json:"l"`
BodyData map[string]interface{} `json:"d"`
}
func getConnectionString() string {
cfg := &config.Cfg.RabbitMq
return fmt.Sprintf("amqp://%s:%s@%s/", cfg.Username, cfg.Password, cfg.Host)
}
func Init() {
conn, err := amqp.Dial(getConnectionString())
if err != nil {
log.Fatalln("Failed to connect to RabbitMQ", err)
}
Conn = conn
ch, err := conn.Channel()
if err != nil {
log.Fatalln("Failed to open a channel", err)
}
defer ch.Close()
Channel = ch
log.Debug("RabbitMQ connected")
msgs, err := ch.Consume(
"mails",
"",
true,
false,
false,
false,
nil,
)
forever := make(chan []byte)
go func() {
for d := range msgs {
fmt.Printf("Recieved Message: %s\n", d.Body)
log.Info("aa")
mail := MailInput{}
if err := json.Unmarshal(d.Body, &mail); err != nil {
log.Fatal(err)
}
log.Infoln("input", mail.BodyData)
mailer.NewMail([]string{"app@roese.dev"}, mail.TemplateId, mail.LanguageId, mail.BodyData)
}
}()
fmt.Println("Successfully Connected to our RabbitMQ Instance")
fmt.Println(" [*] - Waiting for messages")
<-forever
}

View File

@ -20,6 +20,6 @@
>%buttonText%</a
>
<p>Or use this Url: {{.url}}</p>
<p>%alternativeUrl% <a href="{{.url}}">{{.url}}</a></p>
</body>
</html>

View File

@ -1,31 +1,38 @@
{
"templates": [
{
"id": 0,
"_id": 0,
"fileName": "accountActivation",
"languages": [
{
"id": 0,
"_id": 0,
"subject": "Konto aktivieren",
"texts": {
"header": "Hallo {{.name}},",
"text": "Vielen Dank für das erstellen eines Kontos auf Roese.dev. Um dein Konto nutzen zu können, musst du es noch aktivieren.",
"buttonText": "Account aktivieren"
"buttonText": "Account aktivieren",
"alternativeUrl": "Oder benutze diesen Link"
}
},
{
"id": 1,
"subject": "Account activation"
"_id": 1,
"subject": "Account activation",
"texts": {
"header": "Hello {{.name}},",
"text": "Thank you for creating an account on Roese.dev. To use your account, you still need to activate it.",
"buttonText": "Activate account",
"alternativeUrl": "Or use this url"
}
}
]
},
{
"id": 1,
"_id": 1,
"fileName": "accountActivation",
"languages": [
{
"id": 0,
"subject": "Test"
"_id": 0,
"subject": "Account wurde erstellt"
}
]
}