mirror of
https://git.robbyzambito.me/http-nats-proxy
synced 2025-12-21 00:34:50 +00:00
Add env vars
Add support for user and pass connection. Add support for specifying the HTTP port
This commit is contained in:
43
main.go
43
main.go
@@ -1,19 +1,58 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
"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() {
|
func main() {
|
||||||
// Connect to the NATS server
|
helpFlag := flag.Bool("help", false, "Display help information about available environment variables")
|
||||||
nc, err := nats.Connect(nats.DefaultURL)
|
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 {
|
if err != nil {
|
||||||
log.Fatal("Error connecting to NATS:", err)
|
log.Fatal("Error connecting to NATS:", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user