//go:generate cargo run --bin wit-bindgen-wrpc go --out-dir bindings --package wrpc.io/examples/go/resources-server/bindings wit package app import ( "context" "fmt" "log/slog" "os" "sync" "github.com/google/uuid" "wrpc.io/examples/go/resources-server/bindings/exports/wrpc_examples/resources/resources" wrpc "wrpc.io/go" ) 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 }, }))) } type Foo struct { cancel context.CancelFunc id uuid.UUID } func (Foo) Bar(ctx context.Context) (string, error) { return "bar", nil } type Handler struct { sync.Map } func (h *Handler) Foo(ctx context.Context) (wrpc.Own[resources.Foo], error) { id, err := uuid.NewV7() if err != nil { return nil, fmt.Errorf("failed to generate UUIDv7: %w", err) } ctx, cancel := context.WithCancel(ctx) h.Store(id.String(), Foo{id: id, cancel: cancel}) go func() { <-ctx.Done() h.Delete(id) }() return wrpc.Own[resources.Foo](id.String()), nil } func (h *Handler) Foo_Foo(ctx context.Context, v wrpc.Own[resources.Foo]) (string, error) { stored, ok := h.Load(string(v)) if !ok { return "", fmt.Errorf("unknown resource ID `%s`", string(v)) } foo := stored.(Foo) foo.cancel() return "foo", nil } func (h *Handler) Foo_Bar(ctx context.Context, v wrpc.Borrow[resources.Foo]) (string, error) { stored, ok := h.Load(string(v)) if !ok { return "", fmt.Errorf("unknown resource ID `%s`", string(v)) } return stored.(Foo).Bar(ctx) } func (h *Handler) Bar(ctx context.Context, v wrpc.Borrow[resources.Foo]) (string, error) { stored, ok := h.Load(string(v)) if !ok { return "", fmt.Errorf("unknown resource ID `%s`", string(v)) } return stored.(Foo).Bar(ctx) }