package main import ( "context" "fmt" "log" "log/slog" "os" "os/signal" "syscall" "github.com/nats-io/nats.go" server "github.com/wrpc/wit-bindgen-wrpc/examples/go/hello-server/bindings" wrpcnats "github.com/wrpc/wrpc/go/nats" ) type Handler struct{} func (Handler) Hello(ctx context.Context) (string, error) { slog.InfoContext(ctx, "handling `wrpc-examples:hello/handler.hello`") return "hello from Go", nil } func run() (err error) { nc, err := nats.Connect(nats.DefaultURL) 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") stop, err := server.Serve(wrpc, Handler{}) if err != nil { return fmt.Errorf("failed to serve `server` 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 `server` world: %w", err) } return nil } func init() { slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ Level: slog.LevelInfo, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { if a.Key == slog.TimeKey { return slog.Attr{} } return a }, }))) } func main() { if err := run(); err != nil { log.Fatal(err) } }