summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-03-10 18:39:28 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-03-10 19:22:18 -0400
commitd2bdac8b2f154d3d070833ffa8dca7e021c00908 (patch)
treef7397ce194dc7d81cf5110ff77a82dca10575444 /main.go
Initial commitHEADmaster
1.0.0
Diffstat (limited to 'main.go')
-rw-r--r--main.go107
1 files changed, 107 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..2678ca0
--- /dev/null
+++ b/main.go
@@ -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 {}
+}