Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(go-examples): reorganize #326

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/go/hello-client/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:generate cargo run --bin wit-bindgen-wrpc go --out-dir bindings --package wrpc.io/examples/go/hello-client/bindings wit

package app

import (
"context"
"fmt"
"log/slog"
"os"

"wrpc.io/examples/go/hello-client/bindings/wrpc_examples/hello/handler"
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
},
})))
}

func Run(prefix string, client wrpc.Invoker) error {
greeting, err := handler.Hello(context.Background(), client)
if err != nil {
return fmt.Errorf("failed to call `wrpc-examples:hello/handler.hello`: %w", err)
}
fmt.Printf("%s: %s\n", prefix, greeting)
return nil
}
20 changes: 3 additions & 17 deletions examples/go/hello-client/cmd/hello-client-nats/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package main

import (
"context"
"fmt"
"log"
"log/slog"
"os"

"github.com/nats-io/nats.go"
"wrpc.io/examples/go/hello-client/bindings/wrpc_examples/hello/handler"
app "wrpc.io/examples/go/hello-client"
wrpcnats "wrpc.io/go/nats"
)

Expand All @@ -33,26 +32,13 @@ func run() (err error) {
}
for _, prefix := range prefixes {
client := wrpcnats.NewClient(nc, wrpcnats.WithPrefix(prefix))
greeting, err := handler.Hello(context.Background(), client)
if err != nil {
return fmt.Errorf("failed to call `wrpc-examples:hello/handler.hello`: %w", err)
if err := app.Run(prefix, client); err != nil {
return err
}
fmt.Printf("%s: %s\n", prefix, greeting)
}
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)
Expand Down
3 changes: 0 additions & 3 deletions examples/go/hello-client/hello_client.go

This file was deleted.

27 changes: 27 additions & 0 deletions examples/go/hello-server/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:generate cargo run --bin wit-bindgen-wrpc go --out-dir bindings --package wrpc.io/examples/go/hello-server/bindings wit

package app

import (
"context"
"log/slog"
"os"
)

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 Handler struct{}

func (Handler) Hello(ctx context.Context) (string, error) {
slog.InfoContext(ctx, "handling `wrpc-examples:hello/handler.hello`")
return "hello from Go", nil
}
22 changes: 2 additions & 20 deletions examples/go/hello-server/cmd/hello-server-nats/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"log"
"log/slog"
Expand All @@ -10,17 +9,11 @@ import (
"syscall"

"github.com/nats-io/nats.go"
app "wrpc.io/examples/go/hello-server"
server "wrpc.io/examples/go/hello-server/bindings"
wrpcnats "wrpc.io/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 {
Expand All @@ -38,7 +31,7 @@ func run() (err error) {
}()

client := wrpcnats.NewClient(nc, wrpcnats.WithPrefix("go"))
stop, err := server.Serve(client, Handler{})
stop, err := server.Serve(client, app.Handler{})
if err != nil {
return fmt.Errorf("failed to serve `server` world: %w", err)
}
Expand All @@ -53,17 +46,6 @@ func run() (err error) {
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)
Expand Down
3 changes: 0 additions & 3 deletions examples/go/hello-server/hello_server.go

This file was deleted.

79 changes: 79 additions & 0 deletions examples/go/resources-server/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//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)
}
72 changes: 2 additions & 70 deletions examples/go/resources-server/cmd/resources-server-nats/main.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,19 @@
package main

import (
"context"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"

"github.com/google/uuid"
"github.com/nats-io/nats.go"
app "wrpc.io/examples/go/resources-server"
server "wrpc.io/examples/go/resources-server/bindings"
"wrpc.io/examples/go/resources-server/bindings/exports/wrpc_examples/resources/resources"
wrpc "wrpc.io/go"
wrpcnats "wrpc.io/go/nats"
)

type Foo struct {
cancel context.CancelFunc
id uuid.UUID
}

func (Foo) Bar(ctx context.Context) (string, error) {
return "bar", nil
}

type ResourcesHandler struct {
sync.Map
}

func (h *ResourcesHandler) Foo(ctx context.Context) (wrpc.Own[resources.Foo], error) {
id, err := uuid.NewV7()
if err != nil {
return "", 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 *ResourcesHandler) 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 *ResourcesHandler) 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 *ResourcesHandler) 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 run() (err error) {
nc, err := nats.Connect(nats.DefaultURL)
if err != nil {
Expand All @@ -88,7 +31,7 @@ func run() (err error) {
}()

client := wrpcnats.NewClient(nc, wrpcnats.WithPrefix("go"))
stop, err := server.Serve(client, &ResourcesHandler{})
stop, err := server.Serve(client, &app.Handler{})
if err != nil {
return fmt.Errorf("failed to serve `server` world: %w", err)
}
Expand All @@ -103,17 +46,6 @@ func run() (err error) {
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)
Expand Down
3 changes: 0 additions & 3 deletions examples/go/resources-server/resources_server.go

This file was deleted.

Loading
Loading