update
This commit is contained in:
91
main.go
91
main.go
@@ -1,8 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"email-notification/app"
|
||||
"email-notification/config"
|
||||
"email-notification/model"
|
||||
"email-notification/query"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -18,81 +21,47 @@ func main() {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
config.NewDatabase()
|
||||
db := config.NewDatabase().WithContext(context.Background())
|
||||
|
||||
to := []string{"teddy.nutech@gmail.com"}
|
||||
cc := []string{"teddy.nutech@gmail.com"}
|
||||
subject := "test"
|
||||
message := "test"
|
||||
|
||||
// conn, err := amqp091.Dial("amqp://guest:guest@36.66.3.44:7012/")
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to connect to RabbitMQ: ", err)
|
||||
// }
|
||||
// defer conn.Close()
|
||||
fmt.Println(to, cc, subject, message)
|
||||
|
||||
// ch, err := conn.Channel()
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to open a channel: ", err)
|
||||
// }
|
||||
// defer ch.Close()
|
||||
http.HandleFunc("/birthday-notification", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
http.HandleFunc("/notif-email", func(w http.ResponseWriter, r *http.Request) {
|
||||
data := new([]model.Birthday)
|
||||
|
||||
// ch.Publish("notification", "email", false, false, amqp091.Publishing{
|
||||
// ContentType: "application/json",
|
||||
// Body: []byte("test-email"),
|
||||
// })
|
||||
err := query.GetBirthday(db, data, "1", "1")
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
}
|
||||
|
||||
go func() {
|
||||
fmt.Println("sending email...")
|
||||
err := app.SendEmail(to, cc, subject, message)
|
||||
if err != nil {
|
||||
fmt.Println("error", err.Error())
|
||||
}
|
||||
fmt.Println("email sent")
|
||||
}()
|
||||
fmt.Println("hasil query data", data)
|
||||
|
||||
w.Write([]byte{})
|
||||
for _, v := range *data {
|
||||
fmt.Println("Birthday", v.Fullname)
|
||||
sendTo := []string{v.Email}
|
||||
go func() {
|
||||
fmt.Println("sending email...")
|
||||
err := app.SendEmail(sendTo, sendTo, subject, message)
|
||||
if err != nil {
|
||||
fmt.Println("error", err.Error())
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
} else {
|
||||
fmt.Println("email sent")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
w.Write([]byte("success"))
|
||||
})
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
|
||||
http.ListenAndServe(":"+port, nil)
|
||||
|
||||
// q, err := ch.QueueDeclare(
|
||||
// "email", // name
|
||||
// false,
|
||||
// false,
|
||||
// false,
|
||||
// false,
|
||||
// nil,
|
||||
// )
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to declare a queue:", err)
|
||||
// }
|
||||
|
||||
// msgs, err := ch.Consume(
|
||||
// "email",
|
||||
// "", // consumer
|
||||
// true, // auto-ack
|
||||
// false, // exclusive
|
||||
// false, // no-local
|
||||
// false, // no-wait
|
||||
// nil,
|
||||
// )
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to register a consumer: ", err)
|
||||
// }
|
||||
|
||||
// forever := make(chan bool)
|
||||
|
||||
// go func() {
|
||||
// for d := range msgs {
|
||||
// log.Printf("Received a message: %s", d.Body)
|
||||
// }
|
||||
// }()
|
||||
|
||||
// fmt.Println(" [*] Waiting for messages. To exit press CTRL+C")
|
||||
// <-forever
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user