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 Aug 6, 2019
1 parent a6f57d8 commit f40a719
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 @@ -32,6 +32,7 @@ Server Options:
--tlscert <file> HTTP server certificate file
--tlskey <file> Private key for HTTP server certificate
--apiencoding <type> Encoding for web resources: json, jsonflat (default: json)
--creds <file> NATS User Credentials file
-c, --config <file> Configuration file
Common Options:
Expand All @@ -42,9 +43,10 @@ Configuration Documentation: https://resgate.io/docs/get-started/configu

// 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 @@ -68,6 +70,7 @@ func (c *Config) Init(fs *flag.FlagSet, args []string) error {
port uint
headauth string
addr string
natsCreds string
)

fs.BoolVar(&showHelp, "h", false, "Show this message.")
Expand All @@ -92,6 +95,7 @@ func (c *Config) Init(fs *flag.FlagSet, args []string) error {
fs.StringVar(&c.APIEncoding, "apiencoding", "", "Encoding for web resources.")
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.")
fs.BoolVar(&c.Debug, "debug", false, "Enable debugging.")

if err := fs.Parse(args); err != nil {
Expand Down Expand Up @@ -146,6 +150,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
}
case "i":
fallthrough
case "addr":
Expand Down Expand Up @@ -192,6 +202,7 @@ func main() {
}
serv, err := 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 @@ -75,8 +76,14 @@ func (c *Client) Connect() error {

c.Logf("Connecting to NATS at %s", c.URL)

// 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 f40a719

Please sign in to comment.