-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (61 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"plan/internal/handlers"
"plan/internal/mailing"
"plan/internal/runtime"
"plan/internal/utils"
"github.com/nats-io/nats.go"
)
func main() {
defer log.Println("exiting...")
RESEND_API_KEY := utils.RequireEnv("RESEND_API_KEY")
NATS_URL := utils.RequireEnv("NATS_URL")
NATS_USER := utils.RequireEnv("NATS_USER")
NATS_PASSWORD := utils.RequireEnv("NATS_PASSWORD")
quit := make(chan os.Signal, 1)
signal.Notify(quit,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
context, cancel := context.WithCancel(context.Background())
defer cancel()
var conn *nats.Conn
var err error
outer:
for {
select {
case <-quit:
return
default:
conn, err = nats.Connect(NATS_URL, nats.UserInfo(NATS_USER, NATS_PASSWORD))
if err != nil {
log.Println(err)
log.Println("retrying...")
time.Sleep(time.Second)
} else {
break outer
}
}
}
log.Println("connected to nats")
defer conn.Close()
runtime := runtime.Runtime{
Context: context,
Nats: conn,
Mailer: mailing.NewResend(&mailing.ResendOptions{
ApiKey: RESEND_API_KEY,
}),
}
sub, err := handlers.SendMailHandler(runtime)
if err != nil {
defer sub.Unsubscribe()
}
<-quit
}