Debian Codegraph @

examples/nats-pub/main.go

raw
1 // Copyright 2012-2022 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package main
15
16 import (
17 "flag"
18 "log"
19 "os"
20
21 "github.com/nats-io/nats.go"
22 )
23
24 // NOTE: Can test with demo servers.
25 // nats-pub -s demo.nats.io <subject> <msg>
26
27 func usage() {
28 log.Printf("Usage: nats-pub [-s server] [-creds file] [-nkey file] [-tlscert file] [-tlskey file] [-tlscacert file] <subject> <msg>\n")
29 flag.PrintDefaults()
30 }
31
32 func showUsageAndExit(exitcode int) {
33 usage()
34 os.Exit(exitcode)
35 }
36
37 func main() {
38 var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
39 var userCreds = flag.String("creds", "", "User Credentials File")
40 var nkeyFile = flag.String("nkey", "", "NKey Seed File")
41 var tlsClientCert = flag.String("tlscert", "", "TLS client certificate file")
42 var tlsClientKey = flag.String("tlskey", "", "Private key file for client certificate")
43 var tlsCACert = flag.String("tlscacert", "", "CA certificate to verify peer against")
44 var reply = flag.String("reply", "", "Sets a specific reply subject")
45 var showHelp = flag.Bool("h", false, "Show help message")
46
47 log.SetFlags(0)
48 flag.Usage = usage
49 flag.Parse()
50
51 if *showHelp {
52 showUsageAndExit(0)
53 }
54
55 args := flag.Args()
56 if len(args) != 2 {
57 showUsageAndExit(1)
58 }
59
60 // Connect Options.
61 opts := []nats.Option{nats.Name("NATS Sample Publisher")}
62
63 if *userCreds != "" && *nkeyFile != "" {
64 log.Fatal("specify -seed or -creds")
65 }
66
67 // Use UserCredentials
68 if *userCreds != "" {
69 opts = append(opts, nats.UserCredentials(*userCreds))
70 }
71
72 // Use TLS client authentication
73 if *tlsClientCert != "" && *tlsClientKey != "" {
74 opts = append(opts, nats.ClientCert(*tlsClientCert, *tlsClientKey))
75 }
76
77 // Use specific CA certificate
78 if *tlsCACert != "" {
79 opts = append(opts, nats.RootCAs(*tlsCACert))
80 }
81
82 // Use Nkey authentication.
83 if *nkeyFile != "" {
84 opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
85 if err != nil {
86 log.Fatal(err)
87 }
88 opts = append(opts, opt)
89 }
90
91 // Connect to NATS
92 nc, err := nats.Connect(*urls, opts...)
93 if err != nil {
94 log.Fatal(err)
95 }
96 defer nc.Close()
97
98 subj, msg := args[0], []byte(args[1])
99
100 if reply != nil && *reply != "" {
101 nc.PublishRequest(subj, *reply, msg)
102 } else {
103 nc.Publish(subj, msg)
104 }
105
106 nc.Flush()
107
108 if err := nc.LastError(); err != nil {
109 log.Fatal(err)
110 } else {
111 log.Printf("Published [%s] : '%s'\n", subj, msg)
112 }
113 }