-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmain.go
59 lines (49 loc) · 1.4 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"time"
"github.com/nats-io/nats.go"
// This is the package containing the generated *.pb.go and *.nrpc.go
// files.
"github.com/nats-rpc/nrpc/examples/helloworld/helloworld"
)
// server implements the helloworld.GreeterServer interface.
type server struct{}
// SayHello is an implementation of the SayHello method from the definition of
// the Greeter service.
func (s *server) SayHello(ctx context.Context, req *helloworld.HelloRequest) (resp *helloworld.HelloReply, err error) {
return &helloworld.HelloReply{Message: "Hello " + req.Name}, nil
}
func main() {
var natsURL = nats.DefaultURL
if len(os.Args) == 2 {
natsURL = os.Args[1]
}
// Connect to the NATS server.
nc, err := nats.Connect(natsURL, nats.Timeout(5*time.Second))
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Our server implementation.
s := &server{}
// The NATS handler from the helloworld.nrpc.proto file.
h := helloworld.NewGreeterHandler(context.TODO(), nc, s)
// Start a NATS subscription using the handler. You can also use the
// QueueSubscribe() method for a load-balanced set of servers.
sub, err := nc.Subscribe(h.Subject(), h.Handler)
if err != nil {
log.Fatal(err)
}
defer sub.Unsubscribe()
// Keep running until ^C.
fmt.Println("server is running, ^C quits.")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
close(c)
}