| 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 |
"time" |
| 21 |
|
| 22 |
"github.com/nats-io/nats.go" |
| 23 |
) |
| 24 |
|
| 25 |
// NOTE: Can test with demo servers. |
| 26 |
// nats-req -s demo.nats.io <subject> <msg> |
| 27 |
|
| 28 |
func usage() { |
| 29 |
log.Printf("Usage: nats-req [-s server] [-creds file] [-nkey file] <subject> <msg>\n") |
| 30 |
flag.PrintDefaults() |
| 31 |
} |
| 32 |
|
| 33 |
func showUsageAndExit(exitcode int) { |
| 34 |
usage() |
| 35 |
os.Exit(exitcode) |
| 36 |
} |
| 37 |
|
| 38 |
func main() { |
| 39 |
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)") |
| 40 |
var userCreds = flag.String("creds", "", "User Credentials File") |
| 41 |
var nkeyFile = flag.String("nkey", "", "NKey Seed File") |
| 42 |
var showHelp = flag.Bool("h", false, "Show help message") |
| 43 |
|
| 44 |
log.SetFlags(0) |
| 45 |
flag.Usage = usage |
| 46 |
flag.Parse() |
| 47 |
|
| 48 |
if *showHelp { |
| 49 |
showUsageAndExit(0) |
| 50 |
} |
| 51 |
|
| 52 |
args := flag.Args() |
| 53 |
if len(args) < 2 { |
| 54 |
showUsageAndExit(1) |
| 55 |
} |
| 56 |
|
| 57 |
// Connect Options. |
| 58 |
opts := []nats.Option{nats.Name("NATS Sample Requestor")} |
| 59 |
|
| 60 |
if *userCreds != "" && *nkeyFile != "" { |
| 61 |
log.Fatal("specify -seed or -creds") |
| 62 |
} |
| 63 |
|
| 64 |
// Use UserCredentials |
| 65 |
if *userCreds != "" { |
| 66 |
opts = append(opts, nats.UserCredentials(*userCreds)) |
| 67 |
} |
| 68 |
|
| 69 |
// Use Nkey authentication. |
| 70 |
if *nkeyFile != "" { |
| 71 |
opt, err := nats.NkeyOptionFromSeed(*nkeyFile) |
| 72 |
if err != nil { |
| 73 |
log.Fatal(err) |
| 74 |
} |
| 75 |
opts = append(opts, opt) |
| 76 |
} |
| 77 |
|
| 78 |
// Connect to NATS |
| 79 |
nc, err := nats.Connect(*urls, opts...) |
| 80 |
if err != nil { |
| 81 |
log.Fatal(err) |
| 82 |
} |
| 83 |
defer nc.Close() |
| 84 |
subj, payload := args[0], []byte(args[1]) |
| 85 |
|
| 86 |
msg, err := nc.Request(subj, payload, 2*time.Second) |
| 87 |
if err != nil { |
| 88 |
if nc.LastError() != nil { |
| 89 |
log.Fatalf("%v for request", nc.LastError()) |
| 90 |
} |
| 91 |
log.Fatalf("%v for request", err) |
| 92 |
} |
| 93 |
|
| 94 |
log.Printf("Published [%s] : '%s'", subj, payload) |
| 95 |
log.Printf("Received [%v] : '%s'", msg.Subject, string(msg.Data)) |
| 96 |
} |