package main import ( "fmt" "log" "log/slog" "os" "os/signal" "syscall" "github.com/nats-io/nats.go" wrpcnats "github.com/wrpc/wrpc/go/nats" integration "github.com/wrpc/wit-bindgen-wrpc/tests/go" "github.com/wrpc/wit-bindgen-wrpc/tests/go/bindings/sync_server" ) func run(url string) error { nc, err := nats.Connect(url) if err != nil { return fmt.Errorf("failed to connect to NATS.io: %w", err) } defer nc.Close() defer func() { if dErr := nc.Drain(); dErr != nil { if err == nil { err = fmt.Errorf("failed to drain NATS.io connection: %w", dErr) } else { slog.Error("failed to drain NATS.io connection", "err", dErr) } } }() wrpc := wrpcnats.NewClient(nc, "go") var h integration.SyncHandler stop, err := sync_server.Serve(wrpc, h, h) if err != nil { return fmt.Errorf("failed to serve world: %w", err) } signalCh := make(chan os.Signal, 1) signal.Notify(signalCh, syscall.SIGINT) <-signalCh if err = stop(); err != nil { return fmt.Errorf("failed to stop serving world: %w", err) } return nil } func init() { slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ Level: slog.LevelDebug, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { if a.Key == slog.TimeKey { return slog.Attr{} } return a }, }))) } func main() { if err := run(os.Args[1]); err != nil { log.Fatal(err) } }