68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"email-notification/app"
|
|
"email-notification/config"
|
|
"email-notification/model"
|
|
"email-notification/query"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Println("Test")
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file")
|
|
}
|
|
|
|
db := config.NewDatabase().WithContext(context.Background())
|
|
|
|
to := []string{"teddy.nutech@gmail.com"}
|
|
cc := []string{"teddy.nutech@gmail.com"}
|
|
subject := "test"
|
|
message := "test"
|
|
|
|
fmt.Println(to, cc, subject, message)
|
|
|
|
http.HandleFunc("/birthday-notification", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data := new([]model.Birthday)
|
|
|
|
err := query.GetBirthday(db, data, "1", "1")
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
w.Write([]byte(err.Error()))
|
|
}
|
|
|
|
fmt.Println("hasil query data", data)
|
|
|
|
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)
|
|
}
|