diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-03-10 18:39:28 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-03-10 19:22:18 -0400 |
commit | d2bdac8b2f154d3d070833ffa8dca7e021c00908 (patch) | |
tree | f7397ce194dc7d81cf5110ff77a82dca10575444 /main.go |
1.0.0
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 107 |
1 files changed, 107 insertions, 0 deletions
@@ -0,0 +1,107 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + + "github.com/FabianWilms/GoReadableID/readableId" + "github.com/google/uuid" + "github.com/gosimple/slug" + "github.com/nats-io/nats.go" + "github.com/nats-io/nats.go/micro" +) + +// 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(" 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)") +} + +func main() { + 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") + natsToken := os.Getenv("NATS_TOKEN") + natsNkey := os.Getenv("NATS_NKEY") + natsNkeySeed := os.Getenv("NATS_NKEY_SEED") + natsCredsFile := os.Getenv("NATS_CREDS_FILE") + + // Set up NATS connection options + opts := []nats.Option{} + + if natsUser != "" && natsPassword != "" { + opts = append(opts, nats.UserInfo(natsUser, natsPassword)) + } else if natsToken != "" { + opts = append(opts, nats.Token(natsToken)) + } else if natsNkey != "" && natsNkeySeed != "" { + log.Fatalln("NKEY connection not supported") + } else if natsCredsFile != "" { + opts = append(opts, nats.UserCredentials(natsCredsFile)) + } + + opts = append(opts, nats.Name("ids")) + + nc, err := nats.Connect(natsURL, opts...) + if err != nil { + log.Fatal("Error connecting to NATS:", err) + } + defer nc.Close() + log.Println("Connected to NATS") + + versionString := "0.0.0" + if v, exists := os.LookupEnv("VERSION"); exists { + versionString = v + } + + srv, err := micro.AddService(nc, micro.Config{ + Name: "ids", + Version: versionString, + }) + if err != nil { + log.Fatalln("Could not create service", err) + } + + err = srv.AddEndpoint("uuid", micro.HandlerFunc(func(req micro.Request) { + req.Respond([]byte(uuid.New().String())) + })) + if err != nil { + log.Fatalln("Could not create uuid endpoint", err) + } + + err = srv.AddEndpoint("readable-id", micro.HandlerFunc(func(req micro.Request) { + req.Respond([]byte(readableId.GetRandomID())) + })) + if err != nil { + log.Fatalln("Could not create readable-id endpoint", err) + } + + err = srv.AddEndpoint("slugify", micro.HandlerFunc(func(req micro.Request) { + req.Respond([]byte(slug.Make(string(req.Data())))) + })) + if err != nil { + log.Fatalln("Could not create slugify endpoint", err) + } + + + select {} +} |