Skip to content

Commit

Permalink
Merge pull request #15 from delaneyj/embeddednats
Browse files Browse the repository at this point in the history
move embedded nats to its own dir
  • Loading branch information
delaneyj authored Oct 4, 2024
2 parents e019320 + 0afa37b commit 18ca4c4
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 416 deletions.
5 changes: 3 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version: "3"

vars:
VERSION: 0.2.21
VERSION: 0.3.0

interval: 200ms

Expand All @@ -13,7 +13,8 @@ tasks:
- go get -u github.com/valyala/quicktemplate/qtc
- go install github.com/valyala/quicktemplate/qtc@latest
- go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
bump:

libpub:
cmds:
- git push origin
- git tag v{{.VERSION}}
Expand Down
36 changes: 36 additions & 0 deletions cmd/examples/embeddednats/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/delaneyj/toolbelt/embeddednats"
)

func main() {
// create ze builder
ctx := context.Background()
ns, err := embeddednats.New(ctx,
embeddednats.WithDirectory("/var/tmp/deleteme"),
embeddednats.WithShouldClearData(true),
)
if err != nil {
panic(err)
}

// behold ze server
ns.NatsServer.Start()

ns.WaitForServer()
nc, err := ns.Client()
if err != nil {
panic(err)
}
nc.Publish("foo", []byte("hello world"))

sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
}
101 changes: 101 additions & 0 deletions embeddednats/nats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package embeddednats

import (
"context"
"log"
"os"

"github.com/cenkalti/backoff"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)

type options struct {
DataDirectory string
ShouldClearData bool
NATSSeverOptions *server.Options
}

type Option func(*options)

func WithDirectory(dir string) Option {
return func(o *options) {
o.DataDirectory = dir
}
}

func WithShouldClearData(shouldClearData bool) Option {
return func(o *options) {
o.ShouldClearData = shouldClearData
}
}

func WithNATSServerOptions(natsServerOptions *server.Options) Option {
return func(o *options) {
o.NATSSeverOptions = natsServerOptions
}
}

type Server struct {
NatsServer *server.Server
}

func New(ctx context.Context, opts ...Option) (*Server, error) {
options := &options{
DataDirectory: "./data/nats",
}
for _, o := range opts {
o(options)
}

if options.ShouldClearData {
if err := os.RemoveAll(options.DataDirectory); err != nil {
return nil, err
}
}

if options.NATSSeverOptions == nil {
options.NATSSeverOptions = &server.Options{
JetStream: true,
StoreDir: options.DataDirectory,
}
}

// Initialize new server with options
ns, err := server.NewServer(options.NATSSeverOptions)
if err != nil {
panic(err)
}

// Start the server via goroutine
ns.Start()

return &Server{
NatsServer: ns,
}, nil
}

func (n *Server) Close() error {
if n.NatsServer != nil {
n.NatsServer.Shutdown()
}
return nil
}

func (n *Server) WaitForServer() {
b := backoff.NewExponentialBackOff()

for {
d := b.NextBackOff()
ready := n.NatsServer.ReadyForConnections(d)
if ready {
break
}

log.Printf("NATS server not ready, waited %s, retrying...", d)
}
}

func (n *Server) Client() (*nats.Conn, error) {
return nats.Connect(n.NatsServer.ClientURL())
}
69 changes: 34 additions & 35 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
module github.com/delaneyj/toolbelt

go 1.21
go 1.23.0

toolchain go1.23.1

require (
github.com/CAFxX/httpcompression v0.0.9
github.com/alecthomas/chroma/v2 v2.11.1
github.com/alecthomas/kong v0.8.1
github.com/alecthomas/chroma/v2 v2.14.0
github.com/alecthomas/kong v1.2.1
github.com/autosegment/ksuid v1.1.0
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/chewxy/math32 v1.10.1
github.com/chewxy/math32 v1.11.1
github.com/denisbrodbeck/machineid v1.0.1
github.com/dustin/go-humanize v1.0.1
github.com/gertd/go-pluralize v0.2.1
github.com/go-chi/chi/v5 v5.0.10
github.com/go-chi/chi/v5 v5.1.0
github.com/go-ping/ping v1.1.0
github.com/go-rod/rod v0.116.1
github.com/goccy/go-json v0.10.2
github.com/go-rod/rod v0.116.2
github.com/goccy/go-json v0.10.3
github.com/iancoleman/strcase v0.3.0
github.com/joho/godotenv v1.5.1
github.com/linode/linodego v1.25.0
github.com/maragudk/gomponents v0.20.1
github.com/linode/linodego v1.41.0
github.com/maragudk/gomponents v0.22.0
github.com/melbahja/goph v1.4.0
github.com/nats-io/nats-server/v2 v2.10.17
github.com/nats-io/nats.go v1.36.0
github.com/nats-io/nats-server/v2 v2.10.21
github.com/nats-io/nats.go v1.37.0
github.com/rzajac/zflake v0.8.0
github.com/samber/lo v1.44.0
github.com/samber/lo v1.47.0
github.com/sqlc-dev/plugin-sdk-go v1.23.0
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasttemplate v1.2.2
github.com/valyala/quicktemplate v1.8.0
github.com/zeebo/xxh3 v1.0.2
golang.org/x/crypto v0.24.0
golang.org/x/oauth2 v0.18.0
golang.org/x/sync v0.7.0
golang.org/x/crypto v0.27.0
golang.org/x/oauth2 v0.23.0
golang.org/x/sync v0.8.0
google.golang.org/protobuf v1.34.2
k8s.io/apimachinery v0.28.4
zombiezen.com/go/sqlite v1.3.0
k8s.io/apimachinery v0.31.1
zombiezen.com/go/sqlite v1.4.0
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/go-resty/resty/v2 v2.10.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/go-resty/resty/v2 v2.15.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/nats-io/jwt/v2 v2.5.7 // indirect
github.com/minio/highwayhash v1.0.3 // indirect
github.com/nats-io/jwt/v2 v2.7.0 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
Expand All @@ -61,19 +62,17 @@ require (
github.com/ysmood/goob v0.4.0 // indirect
github.com/ysmood/got v0.40.0 // indirect
github.com/ysmood/gson v0.7.3 // indirect
github.com/ysmood/leakless v0.8.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.22.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.64.0 // indirect
github.com/ysmood/leakless v0.9.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.7.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f // indirect
google.golang.org/grpc v1.67.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
modernc.org/libc v1.54.0 // indirect
modernc.org/libc v1.61.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.30.1 // indirect
modernc.org/sqlite v1.33.1 // indirect
)
Loading

0 comments on commit 18ca4c4

Please sign in to comment.