Skip to content

Commit

Permalink
GH-63: Added support for NATS User Credentials file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Jan 27, 2019
1 parent c0fcf38 commit 4d01c0c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Server Options:
--tls Enable TLS for HTTP (default: false)
--tlscert <file> HTTP server certificate file
--tlskey <file> Private key for HTTP server certificate
--creds <file> NATS User Credentials file
-c, --config <file> Configuration file
Common Options:
Expand All @@ -38,9 +39,10 @@ Common Options:

// Config holds server configuration
type Config struct {
NatsURL string `json:"natsUrl"`
RequestTimeout int `json:"requestTimeout"`
Debug bool `json:"debug,omitempty"`
NatsURL string `json:"natsUrl"`
NatsCreds *string `json:"natsCreds"`
RequestTimeout int `json:"requestTimeout"`
Debug bool `json:"debug,omitempty"`
server.Config
}

Expand All @@ -63,6 +65,7 @@ func (c *Config) Init(fs *flag.FlagSet, args []string) error {
configFile string
port uint
headauth string
natsCreds string
)

fs.BoolVar(&showHelp, "h", false, "Show this message.")
Expand All @@ -84,6 +87,7 @@ func (c *Config) Init(fs *flag.FlagSet, args []string) error {
fs.StringVar(&c.TLSKey, "tlskey", "", "Private key for HTTP server certificate.")
fs.IntVar(&c.RequestTimeout, "r", 0, "Timeout in milliseconds for NATS requests.")
fs.IntVar(&c.RequestTimeout, "reqtimeout", 0, "Timeout in milliseconds for NATS requests.")
fs.StringVar(&natsCreds, "creds", "", "NATS User Credentials file.")

if err := fs.Parse(args); err != nil {
printAndDie(fmt.Sprintf("error parsing arguments: %s", err.Error()), true)
Expand Down Expand Up @@ -137,6 +141,12 @@ func (c *Config) Init(fs *flag.FlagSet, args []string) error {
} else {
c.HeaderAuth = &headauth
}
case "natscreds":
if natsCreds == "" {
c.NatsCreds = nil
} else {
c.NatsCreds = &natsCreds
}
}
})

Expand Down Expand Up @@ -179,6 +189,7 @@ func main() {
}
serv := server.NewService(&nats.Client{
URL: cfg.NatsURL,
Creds: cfg.NatsCreds,
RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Millisecond,
Logger: l,
}, cfg.Config)
Expand Down
9 changes: 8 additions & 1 deletion nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const logPrefix = "[NATS] "
type Client struct {
RequestTimeout time.Duration
URL string
Creds *string
Logger logger.Logger

mq *nats.Conn
Expand Down Expand Up @@ -73,8 +74,14 @@ func (c *Client) Connect() error {
c.mu.Lock()
defer c.mu.Unlock()

// Create connection options
opts := []nats.Option{nats.NoReconnect()}
if c.Creds != nil {
opts = append(opts, nats.UserCredentials(*c.Creds))
}

// No reconnects as all resources are instantly stale anyhow
nc, err := nats.Connect(c.URL, nats.NoReconnect())
nc, err := nats.Connect(c.URL, opts...)
if err != nil {
return err
}
Expand Down

0 comments on commit 4d01c0c

Please sign in to comment.