diff --git a/config.toml b/config.toml index e04d198..d5ecede 100644 --- a/config.toml +++ b/config.toml @@ -8,4 +8,12 @@ port = "25" user = "no-reply@roese.dev" password = "mhtWMUn049o25xYTLUpfplX5Ze3jRfqh" from = "App " -templatePath = "./templates/" \ No newline at end of file + +[rabbitmq] +host = "localhost:5672" +username = "guest" +password = "guest" + +[templates] +folderPath = "./templates/" +configPath = "./templates/templates.json" diff --git a/go.mod b/go.mod index 5a72d7b..8a41c30 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index b317c78..48b3178 100644 --- a/go.sum +++ b/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/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= diff --git a/main.go b/main.go index 371163b..ca7034b 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/modules/config/config.go b/modules/config/config.go index 2dbf40b..ecc1eec 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -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 diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index 8e760b4..fbeb39c 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -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) diff --git a/modules/rabbitmq/rabbitmq.go b/modules/rabbitmq/rabbitmq.go new file mode 100644 index 0000000..6a2ef2c --- /dev/null +++ b/modules/rabbitmq/rabbitmq.go @@ -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 +} diff --git a/templates/accountActivation.html b/templates/accountActivation.html index 8cea383..46ce355 100644 --- a/templates/accountActivation.html +++ b/templates/accountActivation.html @@ -20,6 +20,6 @@ >%buttonText% -

Or use this Url: {{.url}}

+

%alternativeUrl% {{.url}}

diff --git a/templates/templates.json b/templates/templates.json index 940a2c2..f22e06e 100644 --- a/templates/templates.json +++ b/templates/templates.json @@ -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" } ] }