-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
58 lines (45 loc) · 1.1 KB
/
setup.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
package cluster
import (
"fmt"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"time"
)
const InternalTopic = "_cluster"
var NServer *server.Server
func setup(instance *Instance) (err error) {
//clusterStr := "nats://host.docker.internal:9622,nats://10.10.10.120:9622"
opts := &server.Options{
HTTPPort: instance.httpMonitorPort,
Port: instance.natsPort,
Cluster: server.ClusterOpts{
Name: instance.clusterName,
Port: instance.clusterPort,
},
Routes: server.RoutesFromStr(instance.natsSeed),
}
// Initialize new server with options
NServer, err = server.NewServer(opts)
NServer.ConfigureLogger()
if err != nil {
return err
}
return nil
}
func run(background bool) (err error) {
if NServer == nil {
return fmt.Errorf("server not initialized")
}
NServer.Start()
// Wait for server to be ready for connections
if !NServer.ReadyForConnections(4 * time.Second) {
return fmt.Errorf("not ready for connection")
}
if !background {
NServer.WaitForShutdown()
}
return nil
}
func client() (*nats.Conn, error) {
return nats.Connect(NServer.ClientURL())
}