Skip to content

Commit

Permalink
Better option handling for TLS connections
Browse files Browse the repository at this point in the history
  • Loading branch information
derekcollison committed Jan 17, 2016
1 parent 565ebc4 commit 90f704d
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 96 deletions.
5 changes: 4 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ func ExampleConnect() {
nc, _ := nats.Connect(nats.DefaultURL)
nc.Close()

nc, _ = nats.Connect("nats://derek:secretpassword@nats.apcera.com:4222")
nc, _ = nats.Connect("nats://derek:secretpassword@demo.nats.io:4222")
nc.Close()

nc, _ = nats.Connect("tls://derek:secretpassword@demo.nats.io:4443")
nc.Close()

opts := nats.Options{
Expand Down
19 changes: 5 additions & 14 deletions examples/nats-pub.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore

package main

import (
"flag"
"log"
"strings"

"github.com/nats-io/nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-pub -s tls://demo.nats.io:4443 foo hello
func usage() {
log.Fatalf("Usage: nats-pub [-s server (%s)] [--tls] <subject> <msg> \n", nats.DefaultURL)
log.Fatalf("Usage: nats-pub [-s server (%s)] <subject> <msg> \n", nats.DefaultURL)
}

func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var tls = flag.Bool("tls", false, "Use TLS Secure Connection")

log.SetFlags(0)
flag.Usage = usage
Expand All @@ -28,17 +27,9 @@ func main() {
usage()
}

opts := nats.DefaultOptions
opts.Servers = strings.Split(*urls, ",")
for i, s := range opts.Servers {
opts.Servers[i] = strings.Trim(s, " ")
}

opts.Secure = *tls

nc, err := opts.Connect()
nc, err := nats.Connect(*urls)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
log.Fatal(err)
}
defer nc.Close()

Expand Down
16 changes: 4 additions & 12 deletions examples/nats-qsub.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore

package main
Expand All @@ -8,13 +8,13 @@ import (
"log"
"os"
"runtime"
"strings"

"github.com/nats-io/nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-qsub -s tls://demo.nats.io:4443 foo
func usage() {
log.Fatalf("Usage: nats-sub [-s server] [--tls] [-t] <subject> <queue-group>\n")
log.Fatalf("Usage: nats-sub [-s server] [-t] <subject> <queue-group>\n")
}

func printMsg(m *nats.Msg, i int) {
Expand All @@ -24,7 +24,6 @@ func printMsg(m *nats.Msg, i int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var showTime = flag.Bool("t", false, "Display timestamps")
var tls = flag.Bool("tls", false, "Use Secure Connection")

log.SetFlags(0)
flag.Usage = usage
Expand All @@ -35,14 +34,7 @@ func main() {
usage()
}

opts := nats.DefaultOptions
opts.Servers = strings.Split(*urls, ",")
for i, s := range opts.Servers {
opts.Servers[i] = strings.Trim(s, " ")
}
opts.Secure = *tls

nc, err := opts.Connect()
nc, err := nats.Connect(*urls)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
Expand Down
17 changes: 4 additions & 13 deletions examples/nats-req.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore

package main

import (
"flag"
"log"
"strings"
"time"

"github.com/nats-io/nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-req -s tls://demo.nats.io:4443 foo hello
func usage() {
log.Fatalf("Usage: nats-req [-s server (%s)] [--tls] <subject> <msg> \n", nats.DefaultURL)
log.Fatalf("Usage: nats-req [-s server (%s)] <subject> <msg> \n", nats.DefaultURL)
}

func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var tls = flag.Bool("tls", false, "Use Secure Connection")

log.SetFlags(0)
flag.Usage = usage
Expand All @@ -29,15 +28,7 @@ func main() {
usage()
}

opts := nats.DefaultOptions
opts.Servers = strings.Split(*urls, ",")
for i, s := range opts.Servers {
opts.Servers[i] = strings.Trim(s, " ")
}

opts.Secure = *tls

nc, err := opts.Connect()
nc, err := nats.Connect(*urls)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
Expand Down
16 changes: 4 additions & 12 deletions examples/nats-rply.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore

package main
Expand All @@ -7,13 +7,13 @@ import (
"flag"
"log"
"runtime"
"strings"

"github.com/nats-io/nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-rply -s tls://demo.nats.io:4443 foo hello
func usage() {
log.Fatalf("Usage: nats-rply [-s server] [--tls] [-t] <subject> <reponse>\n")
log.Fatalf("Usage: nats-rply [-s server][-t] <subject> <reponse>\n")
}

func printMsg(m *nats.Msg, i int) {
Expand All @@ -23,7 +23,6 @@ func printMsg(m *nats.Msg, i int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var showTime = flag.Bool("t", false, "Display timestamps")
var tls = flag.Bool("tls", false, "Use Secure Connection")

log.SetFlags(0)
flag.Usage = usage
Expand All @@ -34,14 +33,7 @@ func main() {
usage()
}

opts := nats.DefaultOptions
opts.Servers = strings.Split(*urls, ",")
for i, s := range opts.Servers {
opts.Servers[i] = strings.Trim(s, " ")
}
opts.Secure = *tls

nc, err := opts.Connect()
nc, err := nats.Connect(*urls)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
Expand Down
16 changes: 4 additions & 12 deletions examples/nats-sub.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore

package main
Expand All @@ -7,13 +7,13 @@ import (
"flag"
"log"
"runtime"
"strings"

"github.com/nats-io/nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-sub -s tls://demo.nats.io:4443 foo
func usage() {
log.Fatalf("Usage: nats-sub [-s server] [--tls] [-t] <subject> \n")
log.Fatalf("Usage: nats-sub [-s server] [-t] <subject> \n")
}

func printMsg(m *nats.Msg, i int) {
Expand All @@ -23,7 +23,6 @@ func printMsg(m *nats.Msg, i int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var showTime = flag.Bool("t", false, "Display timestamps")
var tls = flag.Bool("tls", false, "Use Secure Connection")

log.SetFlags(0)
flag.Usage = usage
Expand All @@ -34,14 +33,7 @@ func main() {
usage()
}

opts := nats.DefaultOptions
opts.Servers = strings.Split(*urls, ",")
for i, s := range opts.Servers {
opts.Servers[i] = strings.Trim(s, " ")
}
opts.Secure = *tls

nc, err := opts.Connect()
nc, err := nats.Connect(*urls)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
Expand Down
Loading

0 comments on commit 90f704d

Please sign in to comment.