MCPcopy
hub / github.com/nats-io/nats.go / main

Function main

examples/nats-rply/main.go:43–119  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41}
42
43func main() {
44 var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
45 var userCreds = flag.String("creds", "", "User Credentials File")
46 var nkeyFile = flag.String("nkey", "", "NKey Seed File")
47 var showTime = flag.Bool("t", false, "Display timestamps")
48 var queueName = flag.String("q", "NATS-RPLY-22", "Queue Group Name")
49 var showHelp = flag.Bool("h", false, "Show help message")
50
51 log.SetFlags(0)
52 flag.Usage = usage
53 flag.Parse()
54
55 if *showHelp {
56 showUsageAndExit(0)
57 }
58
59 args := flag.Args()
60 if len(args) < 2 {
61 showUsageAndExit(1)
62 }
63
64 // Connect Options.
65 opts := []nats.Option{nats.Name("NATS Sample Responder")}
66 opts = setupConnOptions(opts)
67
68 if *userCreds != "" && *nkeyFile != "" {
69 log.Fatal("specify -seed or -creds")
70 }
71
72 // Use UserCredentials
73 if *userCreds != "" {
74 opts = append(opts, nats.UserCredentials(*userCreds))
75 }
76
77 // Use Nkey authentication.
78 if *nkeyFile != "" {
79 opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
80 if err != nil {
81 log.Fatal(err)
82 }
83 opts = append(opts, opt)
84 }
85
86 // Connect to NATS
87 nc, err := nats.Connect(*urls, opts...)
88 if err != nil {
89 log.Fatal(err)
90 }
91
92 subj, reply, i := args[0], args[1], 0
93
94 nc.QueueSubscribe(subj, *queueName, func(msg *nats.Msg) {
95 i++
96 printMsg(msg, i)
97 msg.Respond([]byte(reply))
98 })
99 nc.Flush()
100

Callers

nothing calls this directly

Calls 12

ConnectMethod · 0.80
FatalfMethod · 0.80
showUsageAndExitFunction · 0.70
setupConnOptionsFunction · 0.70
printMsgFunction · 0.70
NameMethod · 0.65
QueueSubscribeMethod · 0.65
RespondMethod · 0.65
DrainMethod · 0.65
StringMethod · 0.45
FlushMethod · 0.45
LastErrorMethod · 0.45

Tested by

no test coverage detected