diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-02-01 11:37:19 -0500 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-02-01 11:43:45 -0500 |
commit | 34521571b50bab2460ca52a64b6e460e9334081f (patch) | |
tree | 5dca56e8a59350b5e3415951203290cad9c84ea4 | |
parent | aa161c45252e6755fc8baecc0000608ee7b58e62 (diff) |
Add env vars
Add support for user and pass connection.
Add support for specifying the HTTP port
-rw-r--r-- | main.go | 43 |
1 files changed, 41 insertions, 2 deletions
@@ -1,19 +1,58 @@ package main import ( + "flag" "fmt" "io/ioutil" "log" "net/http" + "os" "strings" "time" "github.com/nats-io/nats.go" ) +// printHelp outputs the usage information. +func printHelp() { + fmt.Println("Usage: HTTP-to-NATS proxy server") + fmt.Println("\nThe following environment variables are supported:") + 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(" HTTP_PORT - HTTP port to listen on (default: 8080)") +} + func main() { - // Connect to the NATS server - nc, err := nats.Connect(nats.DefaultURL) + helpFlag := flag.Bool("help", false, "Display help information about available environment variables") + flag.Parse() + if *helpFlag { + printHelp() + os.Exit(0) + } + + // Read NATS connection info from environment variables + natsURL := os.Getenv("NATS_URL") + if natsURL == "" { + natsURL = nats.DefaultURL // defaults to "nats://127.0.0.1:4222" + } + natsUser := os.Getenv("NATS_USER") + natsPassword := os.Getenv("NATS_PASSWORD") + + // Read HTTP port from environment variables + httpPort := os.Getenv("HTTP_PORT") + if httpPort == "" { + httpPort = "8080" + } + + // Set up NATS connection options + opts := []nats.Option{} + + if natsUser != "" && natsPassword != "" { + opts = append(opts, nats.UserInfo(natsUser, natsPassword)) + } + + nc, err := nats.Connect(natsURL, opts...) if err != nil { log.Fatal("Error connecting to NATS:", err) } |