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 |
1.0.0
-rw-r--r-- | Dockerfile | 20 | ||||
-rw-r--r-- | go.mod | 19 | ||||
-rw-r--r-- | go.sum | 20 | ||||
-rw-r--r-- | justfile | 13 | ||||
-rw-r--r-- | main.go | 107 |
5 files changed, 179 insertions, 0 deletions
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..28137e1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Build stage +FROM golang:1.24 AS builder + +WORKDIR /usr/src/app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN go build -v -o /usr/local/bin/app ./... + +# Final stage +FROM debian:12-slim + +RUN apt-get update && apt-get install -y ca-certificates +WORKDIR /usr/src/app + +COPY --from=builder /usr/local/bin/app /usr/local/bin/app + +CMD ["app"] @@ -0,0 +1,19 @@ +module git.robbyzambito.me/nats-make-id/v2 + +go 1.23.5 + +require ( + github.com/FabianWilms/GoReadableID v0.0.0-20170714182827-7d7450b775a9 + github.com/google/uuid v1.6.0 + github.com/nats-io/nats.go v1.39.1 +) + +require ( + github.com/gosimple/slug v1.15.0 // indirect + github.com/gosimple/unidecode v1.0.1 // indirect + github.com/klauspost/compress v1.17.9 // indirect + github.com/nats-io/nkeys v0.4.9 // indirect + github.com/nats-io/nuid v1.0.1 // indirect + golang.org/x/crypto v0.31.0 // indirect + golang.org/x/sys v0.28.0 // indirect +) @@ -0,0 +1,20 @@ +github.com/FabianWilms/GoReadableID v0.0.0-20170714182827-7d7450b775a9 h1:8ILHYzaYKXPhfKgbi7ZAvJHEUtkC0RUl6UZrLxVXmO4= +github.com/FabianWilms/GoReadableID v0.0.0-20170714182827-7d7450b775a9/go.mod h1:l6KVrKECKN0wNquDGjBLg/s0ky+IoMft/Sv+LFGQzug= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gosimple/slug v1.15.0 h1:wRZHsRrRcs6b0XnxMUBM6WK1U1Vg5B0R7VkIf1Xzobo= +github.com/gosimple/slug v1.15.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ= +github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o= +github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc= +github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= +github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/nats-io/nats.go v1.39.1 h1:oTkfKBmz7W047vRxV762M67ZdXeOtUgvbBaNoQ+3PPk= +github.com/nats-io/nats.go v1.39.1/go.mod h1:MgRb8oOdigA6cYpEPhXJuRVH6UE/V4jblJ2jQ27IXYM= +github.com/nats-io/nkeys v0.4.9 h1:qe9Faq2Gxwi6RZnZMXfmGMZkg3afLLOtrU+gDZJ35b0= +github.com/nats-io/nkeys v0.4.9/go.mod h1:jcMqs+FLG+W5YO36OX6wFIFcmpdAns+w1Wm6D3I/evE= +github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= +github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/justfile b/justfile new file mode 100644 index 0000000..9bf893a --- /dev/null +++ b/justfile @@ -0,0 +1,13 @@ +set dotenv-load := true + +@default: + just --list + +build: + docker build --tag ewr.vultrcr.com/zambyte/nats-make-id:latest . + +push: build + docker push ewr.vultrcr.com/zambyte/nats-make-id:latest + +run: build + docker run ewr.vultrcr.com/zambyte/nats-make-id:latest
\ No newline at end of file @@ -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 {} +} |