More connection options

This commit is contained in:
2025-02-01 11:41:33 -05:00
parent 34521571b5
commit 21c6514b87

14
main.go
View File

@@ -20,6 +20,10 @@ func printHelp() {
fmt.Println(" NATS_URL - NATS connection URL (default: nats://127.0.0.1:4222)")
fmt.Println(" NATS_USER - NATS username for authentication (optional)")
fmt.Println(" NATS_PASSWORD - NATS password for authentication (optional)")
fmt.Println(" NATS_TOKEN - NATS token for authentication (optional)")
fmt.Println(" NATS_NKEY - NATS NKEY for authentication (optional)")
fmt.Println(" NATS_NKEY_SEED - NATS NKEY seed for authentication (optional)")
fmt.Println(" NATS_CREDS_FILE - Path to NATS credentials file (optional)")
fmt.Println(" HTTP_PORT - HTTP port to listen on (default: 8080)")
}
@@ -38,6 +42,10 @@ func main() {
}
natsUser := os.Getenv("NATS_USER")
natsPassword := os.Getenv("NATS_PASSWORD")
natsToken := os.Getenv("NATS_TOKEN")
natsNkey := os.Getenv("NATS_NKEY")
natsNkeySeed := os.Getenv("NATS_NKEY_SEED")
natsCredsFile := os.Getenv("NATS_CREDS_FILE")
// Read HTTP port from environment variables
httpPort := os.Getenv("HTTP_PORT")
@@ -50,6 +58,12 @@ func main() {
if natsUser != "" && natsPassword != "" {
opts = append(opts, nats.UserInfo(natsUser, natsPassword))
} else if natsToken != "" {
opts = append(opts, nats.Token(natsToken))
} else if natsNkey != "" && natsNkeySeed != "" {
opts = append(opts, nats.Nkey(natsNkey, []byte(natsNkeySeed)))
} else if natsCredsFile != "" {
opts = append(opts, nats.UserCredentials(natsCredsFile))
}
nc, err := nats.Connect(natsURL, opts...)