Skip to content

Commit 3f89b06

Browse files
committed
refactor(go-examples): reorganize
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
1 parent 918b66c commit 3f89b06

File tree

21 files changed

+531
-454
lines changed

21 files changed

+531
-454
lines changed

examples/go/hello-client/app.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:generate cargo run --bin wit-bindgen-wrpc go --out-dir bindings --package wrpc.io/examples/go/hello-client/bindings wit
2+
3+
package app
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"log/slog"
9+
"os"
10+
11+
"wrpc.io/examples/go/hello-client/bindings/wrpc_examples/hello/handler"
12+
wrpc "wrpc.io/go"
13+
)
14+
15+
func init() {
16+
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
17+
Level: slog.LevelInfo, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
18+
if a.Key == slog.TimeKey {
19+
return slog.Attr{}
20+
}
21+
return a
22+
},
23+
})))
24+
}
25+
26+
func Run(prefix string, client wrpc.Invoker) error {
27+
greeting, err := handler.Hello(context.Background(), client)
28+
if err != nil {
29+
return fmt.Errorf("failed to call `wrpc-examples:hello/handler.hello`: %w", err)
30+
}
31+
fmt.Printf("%s: %s\n", prefix, greeting)
32+
return nil
33+
}

examples/go/hello-client/cmd/hello-client-nats/main.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"log"
76
"log/slog"
87
"os"
98

109
"github.com/nats-io/nats.go"
11-
"wrpc.io/examples/go/hello-client/bindings/wrpc_examples/hello/handler"
10+
app "wrpc.io/examples/go/hello-client"
1211
wrpcnats "wrpc.io/go/nats"
1312
)
1413

@@ -33,26 +32,13 @@ func run() (err error) {
3332
}
3433
for _, prefix := range prefixes {
3534
client := wrpcnats.NewClient(nc, wrpcnats.WithPrefix(prefix))
36-
greeting, err := handler.Hello(context.Background(), client)
37-
if err != nil {
38-
return fmt.Errorf("failed to call `wrpc-examples:hello/handler.hello`: %w", err)
35+
if err := app.Run(prefix, client); err != nil {
36+
return err
3937
}
40-
fmt.Printf("%s: %s\n", prefix, greeting)
4138
}
4239
return nil
4340
}
4441

45-
func init() {
46-
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
47-
Level: slog.LevelInfo, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
48-
if a.Key == slog.TimeKey {
49-
return slog.Attr{}
50-
}
51-
return a
52-
},
53-
})))
54-
}
55-
5642
func main() {
5743
if err := run(); err != nil {
5844
log.Fatal(err)

examples/go/hello-client/hello_client.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/go/hello-server/app.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:generate cargo run --bin wit-bindgen-wrpc go --out-dir bindings --package wrpc.io/examples/go/hello-server/bindings wit
2+
3+
package app
4+
5+
import (
6+
"context"
7+
"log/slog"
8+
"os"
9+
)
10+
11+
func init() {
12+
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
13+
Level: slog.LevelInfo, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
14+
if a.Key == slog.TimeKey {
15+
return slog.Attr{}
16+
}
17+
return a
18+
},
19+
})))
20+
}
21+
22+
type Handler struct{}
23+
24+
func (Handler) Hello(ctx context.Context) (string, error) {
25+
slog.InfoContext(ctx, "handling `wrpc-examples:hello/handler.hello`")
26+
return "hello from Go", nil
27+
}

examples/go/hello-server/cmd/hello-server-nats/main.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"log"
76
"log/slog"
@@ -10,17 +9,11 @@ import (
109
"syscall"
1110

1211
"github.com/nats-io/nats.go"
12+
app "wrpc.io/examples/go/hello-server"
1313
server "wrpc.io/examples/go/hello-server/bindings"
1414
wrpcnats "wrpc.io/go/nats"
1515
)
1616

17-
type Handler struct{}
18-
19-
func (Handler) Hello(ctx context.Context) (string, error) {
20-
slog.InfoContext(ctx, "handling `wrpc-examples:hello/handler.hello`")
21-
return "hello from Go", nil
22-
}
23-
2417
func run() (err error) {
2518
nc, err := nats.Connect(nats.DefaultURL)
2619
if err != nil {
@@ -38,7 +31,7 @@ func run() (err error) {
3831
}()
3932

4033
client := wrpcnats.NewClient(nc, wrpcnats.WithPrefix("go"))
41-
stop, err := server.Serve(client, Handler{})
34+
stop, err := server.Serve(client, app.Handler{})
4235
if err != nil {
4336
return fmt.Errorf("failed to serve `server` world: %w", err)
4437
}
@@ -53,17 +46,6 @@ func run() (err error) {
5346
return nil
5447
}
5548

56-
func init() {
57-
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
58-
Level: slog.LevelInfo, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
59-
if a.Key == slog.TimeKey {
60-
return slog.Attr{}
61-
}
62-
return a
63-
},
64-
})))
65-
}
66-
6749
func main() {
6850
if err := run(); err != nil {
6951
log.Fatal(err)

examples/go/hello-server/hello_server.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/go/resources-server/app.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//go:generate cargo run --bin wit-bindgen-wrpc go --out-dir bindings --package wrpc.io/examples/go/resources-server/bindings wit
2+
3+
package app
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"log/slog"
9+
"os"
10+
"sync"
11+
12+
"github.com/google/uuid"
13+
"wrpc.io/examples/go/resources-server/bindings/exports/wrpc_examples/resources/resources"
14+
wrpc "wrpc.io/go"
15+
)
16+
17+
func init() {
18+
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
19+
Level: slog.LevelInfo, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
20+
if a.Key == slog.TimeKey {
21+
return slog.Attr{}
22+
}
23+
return a
24+
},
25+
})))
26+
}
27+
28+
type Foo struct {
29+
cancel context.CancelFunc
30+
id uuid.UUID
31+
}
32+
33+
func (Foo) Bar(ctx context.Context) (string, error) {
34+
return "bar", nil
35+
}
36+
37+
type Handler struct {
38+
sync.Map
39+
}
40+
41+
func (h *Handler) Foo(ctx context.Context) (wrpc.Own[resources.Foo], error) {
42+
id, err := uuid.NewV7()
43+
if err != nil {
44+
return nil, fmt.Errorf("failed to generate UUIDv7: %w", err)
45+
}
46+
ctx, cancel := context.WithCancel(ctx)
47+
h.Store(id.String(), Foo{id: id, cancel: cancel})
48+
go func() {
49+
<-ctx.Done()
50+
h.Delete(id)
51+
}()
52+
return wrpc.Own[resources.Foo](id.String()), nil
53+
}
54+
55+
func (h *Handler) Foo_Foo(ctx context.Context, v wrpc.Own[resources.Foo]) (string, error) {
56+
stored, ok := h.Load(string(v))
57+
if !ok {
58+
return "", fmt.Errorf("unknown resource ID `%s`", string(v))
59+
}
60+
foo := stored.(Foo)
61+
foo.cancel()
62+
return "foo", nil
63+
}
64+
65+
func (h *Handler) Foo_Bar(ctx context.Context, v wrpc.Borrow[resources.Foo]) (string, error) {
66+
stored, ok := h.Load(string(v))
67+
if !ok {
68+
return "", fmt.Errorf("unknown resource ID `%s`", string(v))
69+
}
70+
return stored.(Foo).Bar(ctx)
71+
}
72+
73+
func (h *Handler) Bar(ctx context.Context, v wrpc.Borrow[resources.Foo]) (string, error) {
74+
stored, ok := h.Load(string(v))
75+
if !ok {
76+
return "", fmt.Errorf("unknown resource ID `%s`", string(v))
77+
}
78+
return stored.(Foo).Bar(ctx)
79+
}

examples/go/resources-server/cmd/resources-server-nats/main.go

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,19 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"log"
76
"log/slog"
87
"os"
98
"os/signal"
10-
"sync"
119
"syscall"
1210

13-
"github.com/google/uuid"
1411
"github.com/nats-io/nats.go"
12+
app "wrpc.io/examples/go/resources-server"
1513
server "wrpc.io/examples/go/resources-server/bindings"
16-
"wrpc.io/examples/go/resources-server/bindings/exports/wrpc_examples/resources/resources"
17-
wrpc "wrpc.io/go"
1814
wrpcnats "wrpc.io/go/nats"
1915
)
2016

21-
type Foo struct {
22-
cancel context.CancelFunc
23-
id uuid.UUID
24-
}
25-
26-
func (Foo) Bar(ctx context.Context) (string, error) {
27-
return "bar", nil
28-
}
29-
30-
type ResourcesHandler struct {
31-
sync.Map
32-
}
33-
34-
func (h *ResourcesHandler) Foo(ctx context.Context) (wrpc.Own[resources.Foo], error) {
35-
id, err := uuid.NewV7()
36-
if err != nil {
37-
return "", fmt.Errorf("failed to generate UUIDv7: %w", err)
38-
}
39-
ctx, cancel := context.WithCancel(ctx)
40-
h.Store(id.String(), Foo{id: id, cancel: cancel})
41-
go func() {
42-
<-ctx.Done()
43-
h.Delete(id)
44-
}()
45-
return wrpc.Own[resources.Foo](id.String()), nil
46-
}
47-
48-
func (h *ResourcesHandler) Foo_Foo(ctx context.Context, v wrpc.Own[resources.Foo]) (string, error) {
49-
stored, ok := h.Load(string(v))
50-
if !ok {
51-
return "", fmt.Errorf("unknown resource ID `%s`", string(v))
52-
}
53-
foo := stored.(Foo)
54-
foo.cancel()
55-
return "foo", nil
56-
}
57-
58-
func (h *ResourcesHandler) Foo_Bar(ctx context.Context, v wrpc.Borrow[resources.Foo]) (string, error) {
59-
stored, ok := h.Load(string(v))
60-
if !ok {
61-
return "", fmt.Errorf("unknown resource ID `%s`", string(v))
62-
}
63-
return stored.(Foo).Bar(ctx)
64-
}
65-
66-
func (h *ResourcesHandler) Bar(ctx context.Context, v wrpc.Borrow[resources.Foo]) (string, error) {
67-
stored, ok := h.Load(string(v))
68-
if !ok {
69-
return "", fmt.Errorf("unknown resource ID `%s`", string(v))
70-
}
71-
return stored.(Foo).Bar(ctx)
72-
}
73-
7417
func run() (err error) {
7518
nc, err := nats.Connect(nats.DefaultURL)
7619
if err != nil {
@@ -88,7 +31,7 @@ func run() (err error) {
8831
}()
8932

9033
client := wrpcnats.NewClient(nc, wrpcnats.WithPrefix("go"))
91-
stop, err := server.Serve(client, &ResourcesHandler{})
34+
stop, err := server.Serve(client, &app.Handler{})
9235
if err != nil {
9336
return fmt.Errorf("failed to serve `server` world: %w", err)
9437
}
@@ -103,17 +46,6 @@ func run() (err error) {
10346
return nil
10447
}
10548

106-
func init() {
107-
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
108-
Level: slog.LevelInfo, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
109-
if a.Key == slog.TimeKey {
110-
return slog.Attr{}
111-
}
112-
return a
113-
},
114-
})))
115-
}
116-
11749
func main() {
11850
if err := run(); err != nil {
11951
log.Fatal(err)

examples/go/resources-server/resources_server.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)