Added rabbitmq
parent
d41a73cb9a
commit
9a1f66b92c
10
config.toml
10
config.toml
|
@ -8,4 +8,12 @@ port = "25"
|
||||||
user = "no-reply@roese.dev"
|
user = "no-reply@roese.dev"
|
||||||
password = "mhtWMUn049o25xYTLUpfplX5Ze3jRfqh"
|
password = "mhtWMUn049o25xYTLUpfplX5Ze3jRfqh"
|
||||||
from = "App <no-reply@roese.dev>"
|
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
1
go.mod
|
@ -6,4 +6,5 @@ require (
|
||||||
github.com/BurntSushi/toml v0.3.1
|
github.com/BurntSushi/toml v0.3.1
|
||||||
github.com/gofiber/fiber/v2 v2.12.0
|
github.com/gofiber/fiber/v2 v2.12.0
|
||||||
github.com/sirupsen/logrus v1.8.1
|
github.com/sirupsen/logrus v1.8.1
|
||||||
|
github.com/streadway/amqp v1.0.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -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/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 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
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 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
|
|
11
main.go
11
main.go
|
@ -3,13 +3,12 @@ package main
|
||||||
import (
|
import (
|
||||||
"git.umbach.dev/app-idea/mailer/modules/config"
|
"git.umbach.dev/app-idea/mailer/modules/config"
|
||||||
"git.umbach.dev/app-idea/mailer/modules/mailer"
|
"git.umbach.dev/app-idea/mailer/modules/mailer"
|
||||||
"git.umbach.dev/app-idea/mailer/routers/router"
|
"git.umbach.dev/app-idea/mailer/modules/rabbitmq"
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := fiber.New()
|
//app := fiber.New()
|
||||||
|
|
||||||
config.LoadConfig()
|
config.LoadConfig()
|
||||||
|
|
||||||
|
@ -21,7 +20,9 @@ func main() {
|
||||||
|
|
||||||
mailer.InitMailer()
|
mailer.InitMailer()
|
||||||
|
|
||||||
router.SetupRoutes(app)
|
//router.SetupRoutes(app)
|
||||||
|
|
||||||
app.Listen(cfg.Host)
|
rabbitmq.Init()
|
||||||
|
|
||||||
|
//app.Listen(cfg.Host)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ var Cfg Config
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Server server
|
Server server
|
||||||
Mail mail
|
Mail mail
|
||||||
|
RabbitMq rabbitmq
|
||||||
Templates templates
|
Templates templates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +27,12 @@ type mail struct {
|
||||||
TemplatePath string
|
TemplatePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type rabbitmq struct {
|
||||||
|
Host string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
type templates struct {
|
type templates struct {
|
||||||
FolderPath string
|
FolderPath string
|
||||||
ConfigPath string
|
ConfigPath string
|
||||||
|
|
|
@ -44,7 +44,7 @@ func NewMail(to []string, templateId int, languageId int, bodyData interface{})
|
||||||
|
|
||||||
body, err := mail.RenderTemplate()
|
body, err := mail.RenderTemplate()
|
||||||
|
|
||||||
log.Infoln("body", body, bodyData)
|
log.Infoln("get body")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("error parsing", err)
|
log.Fatalln("error parsing", err)
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -20,6 +20,6 @@
|
||||||
>%buttonText%</a
|
>%buttonText%</a
|
||||||
>
|
>
|
||||||
|
|
||||||
<p>Or use this Url: {{.url}}</p>
|
<p>%alternativeUrl% <a href="{{.url}}">{{.url}}</a></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,31 +1,38 @@
|
||||||
{
|
{
|
||||||
"templates": [
|
"templates": [
|
||||||
{
|
{
|
||||||
"id": 0,
|
"_id": 0,
|
||||||
"fileName": "accountActivation",
|
"fileName": "accountActivation",
|
||||||
"languages": [
|
"languages": [
|
||||||
{
|
{
|
||||||
"id": 0,
|
"_id": 0,
|
||||||
"subject": "Konto aktivieren",
|
"subject": "Konto aktivieren",
|
||||||
"texts": {
|
"texts": {
|
||||||
"header": "Hallo {{.name}},",
|
"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.",
|
"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,
|
"_id": 1,
|
||||||
"subject": "Account activation"
|
"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",
|
"fileName": "accountActivation",
|
||||||
"languages": [
|
"languages": [
|
||||||
{
|
{
|
||||||
"id": 0,
|
"_id": 0,
|
||||||
"subject": "Test"
|
"subject": "Account wurde erstellt"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue