Skip to content

Commit

Permalink
feat: add pubsub go example (#2960)
Browse files Browse the repository at this point in the history
Fixes #2956
  • Loading branch information
safeer authored Oct 2, 2024
1 parent e534f24 commit 1e4223a
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/go/cron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# FTL cron example

Cron jobs will be scheduled on deploy. Expect:
```
info:cron: Frequent cron job triggered.
info:cron: Hourly cron job triggered.
```
12 changes: 12 additions & 0 deletions examples/go/pubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# FTL pubsub example

Run using:
```sh
ftl call pubsub.orderPizza '{"type":"veggie","customer":"bob"}'
```

Expect:
```
info:pubsub: Cooking pizza: {99 veggie bob}
info:pubsub: Delivering pizza: {99 veggie bob}
```
38 changes: 38 additions & 0 deletions examples/go/pubsub/dispatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pubsub

import (
"context"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
"golang.org/x/exp/rand"
)

type OrderPizzaRequest struct {
Type ftl.Option[string] `json:"type"`
Customer string `json:"customer"`
}

type OrderPizzaResponse struct {
ID int `json:"id"`
}

//ftl:verb export
func OrderPizza(ctx context.Context, req OrderPizzaRequest) (OrderPizzaResponse, error) {
randomID := rand.Intn(1000)
p := Pizza{
ID: randomID,
Type: req.Type.Default("cheese"),
Customer: req.Customer,
}
ftl.LoggerFromContext(ctx).Infof("Ordering pizza with ID: %d", randomID)
NewOrderTopic.Publish(ctx, p)
return OrderPizzaResponse{ID: randomID}, nil
}

var _ = ftl.Subscription(PizzaReadyTopic, "deliverPizzaSub")

//ftl:subscribe deliverPizzaSub
func DeliverPizza(ctx context.Context, pizza Pizza) error {
ftl.LoggerFromContext(ctx).Infof("Delivering pizza: %v", pizza)
return nil
}
2 changes: 2 additions & 0 deletions examples/go/pubsub/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "pubsub"
language = "go"
51 changes: 51 additions & 0 deletions examples/go/pubsub/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module ftl/pubsub

go 1.23.0

replace github.com/TBD54566975/ftl => ../../..

require (
github.com/TBD54566975/ftl v0.0.0-00010101000000-000000000000
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
)

require (
connectrpc.com/connect v1.16.2 // indirect
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.1 // indirect
github.com/XSAM/otelsql v0.34.0 // indirect
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/types v0.16.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/hashicorp/cronexpr v1.1.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
github.com/swaggest/jsonschema-go v0.3.72 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.5 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
224 changes: 224 additions & 0 deletions examples/go/pubsub/go.sum

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions examples/go/pubsub/kitchen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pubsub

import (
"context"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
)

//ftl:export
var NewOrderTopic = ftl.Topic[Pizza]("newOrderTopic")

//ftl:export
var PizzaReadyTopic = ftl.Topic[Pizza]("pizzaReadyTopic")

var _ = ftl.Subscription(NewOrderTopic, "cookPizzaSub")

type Pizza struct {
ID int
Type string
Customer string
}

//ftl:subscribe cookPizzaSub
func CookPizza(ctx context.Context, pizza Pizza) error {
ftl.LoggerFromContext(ctx).Infof("Cooking pizza: %v", pizza)
return PizzaReadyTopic.Publish(ctx, pizza)
}
27 changes: 27 additions & 0 deletions examples/go/pubsub/types.ftl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1e4223a

Please sign in to comment.