update
This commit is contained in:
50
main.go
50
main.go
@@ -2,11 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"email-notification/app"
|
||||
"email-notification/config"
|
||||
"email-notification/model"
|
||||
"email-notification/query"
|
||||
"fmt"
|
||||
"email-notification/middleware"
|
||||
"email-notification/route"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -15,51 +13,19 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Test")
|
||||
log.Println("Email Notification Service")
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
http.Handle("/", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Welcome!"))
|
||||
}))
|
||||
|
||||
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"))
|
||||
})
|
||||
route.Route(db)
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user