forked from Logimethods/docker-nats-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
38 lines (33 loc) · 942 Bytes
/
main.go
File metadata and controls
38 lines (33 loc) · 942 Bytes
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
package main
import (
"fmt"
"os"
"math"
"time"
"strings"
"github.com/nats-io/nats"
)
func main() {
fmt.Println("NATS_PASSWORD:",os.Getenv("NATS_PASSWORD"),":")
user, _ := os.LookupEnv("NATS_USERNAME")
fmt.Println("NATS_USERNAME=",user,"=")
pwd, _ := os.LookupEnv("NATS_PASSWORD")
fmt.Println("NATS_PASSWORD=",pwd,"=")
nc, err := nats.Connect("nats://nats:4222", nats.UserInfo(user, pwd))
if (err != nil) {
fmt.Println("error ", err.Error())
}
for ((err != nil) && strings.Contains(err.Error(), "no servers available for connection")) {
nc, err = nats.Connect("nats://nats:4222", nats.UserInfo(user, pwd))
}
if (err == nil) {
defer nc.Close()
nc.Publish("foo", []byte("Hello World"))
fmt.Println("Value published")
} else {
fmt.Println(err)
}
// Wait forever
// https://blog.sgmansfield.com/2016/06/how-to-block-forever-in-go/
<-time.After(time.Duration(math.MaxInt64))
}