Skip to content

Commit

Permalink
fix: fix broken FTL examples (#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman authored Feb 3, 2024
1 parent 40be00e commit 80e2420
Show file tree
Hide file tree
Showing 22 changed files with 234 additions and 169 deletions.
10 changes: 6 additions & 4 deletions FTL.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"path": "kotlin-runtime"
},
{
"path": "examples/time"
"path": "examples/online-boutique"
},
{
"path": "examples/echo"
"path": "examples/go",
"name": "examples/go"
},
{
"path": "examples/online-boutique"
"path": "examples/kotlin",
"name": "examples/kotlin"
},
{
"path": "backend"
Expand Down Expand Up @@ -42,4 +44,4 @@
"**/*.zip": true,
}
}
}
}
1 change: 0 additions & 1 deletion examples/echo/echo.go → examples/go/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type EchoResponse struct {
// Echo returns a greeting with the current time.
//
//ftl:verb
//ftl:ingress GET /echo
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
fmt.Println("Echo received a request!")
tresp, err := sdk.Call(ctx, time.Time, time.TimeRequest{})
Expand Down
File renamed without changes.
27 changes: 17 additions & 10 deletions examples/echo/go.mod → examples/go/echo/go.mod
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
module ftl/echo

go 1.21.5
go 1.21.6

require github.com/TBD54566975/ftl v0.95.0
replace github.com/TBD54566975/ftl => ../../..

require github.com/TBD54566975/ftl v0.0.0-00010101000000-000000000000

require (
connectrpc.com/connect v1.14.0 // indirect
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.6.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/types v0.9.0 // indirect
github.com/alecthomas/types v0.10.1 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.2 // indirect
github.com/jackc/puddle/v2 v2.2.1 // 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/oklog/ulid/v2 v2.1.0 // indirect
github.com/swaggest/jsonschema-go v0.3.64 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.3 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
golang.design/x/reflect v0.0.0-20220504060917-02c43be63f3b // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
)
66 changes: 40 additions & 26 deletions examples/echo/go.sum → examples/go/echo/go.sum

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

File renamed without changes.
5 changes: 5 additions & 0 deletions examples/go/time/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ftl/time

go 1.21.6

replace github.com/TBD54566975/ftl => ../../..
File renamed without changes.
19 changes: 19 additions & 0 deletions examples/go/time/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//ftl:module time
package time

import (
"context"
"time"
)

type TimeRequest struct{}
type TimeResponse struct {
Time time.Time
}

// Time returns the current time.
//
//ftl:verb
func Time(ctx context.Context, req TimeRequest) (TimeResponse, error) {
return TimeResponse{Time: time.Now()}, nil
}
15 changes: 10 additions & 5 deletions examples/kotlin/ftl-module-ad/src/main/kotlin/ftl/ad/Ad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package ftl.ad

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import ftl.builtin.HttpRequest
import ftl.builtin.HttpResponse
import xyz.block.ftl.Context
import xyz.block.ftl.Ingress
import xyz.block.ftl.Ingress.Type.HTTP
import xyz.block.ftl.Method
import xyz.block.ftl.Verb
import java.util.*
Expand All @@ -16,12 +19,14 @@ class AdModule {
private val database: Map<String, Ad> = loadDatabase()

@Verb
@Ingress(Method.GET, "/get")
fun get(context: Context, req: AdRequest): AdResponse {
return when {
req.contextKeys != null -> AdResponse(ads = contextualAds(req.contextKeys))
else -> AdResponse(ads = randomAds())
@Ingress(Method.GET, "/get", HTTP)
fun get(context: Context, req: HttpRequest<AdRequest>): HttpResponse<AdResponse> {
val ads: List<Ad> = when {
req.body.contextKeys != null -> contextualAds(req.body.contextKeys)
else -> randomAds()
}

return HttpResponse(status = 200, headers = emptyMap(), body = AdResponse(ads = ads))
}

private fun contextualAds(contextKeys: List<String>): List<Ad> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ftl.time.TimeModuleClient
import ftl.time.TimeRequest
import xyz.block.ftl.Context
import xyz.block.ftl.Ingress
import xyz.block.ftl.Ingress.Type.HTTP
import xyz.block.ftl.Method
import xyz.block.ftl.Verb

Expand All @@ -15,7 +16,6 @@ data class EchoResponse(val message: String)
class Echo {
@Throws(InvalidInput::class)
@Verb
@Ingress(Method.GET, "/echo")
fun echo(context: Context, req: EchoRequest): EchoResponse {
val response = context.call(TimeModuleClient::time, TimeRequest)
return EchoResponse(message = "Hello, ${req.name ?: "anonymous"}! The time is ${response.time}.")
Expand Down
20 changes: 12 additions & 8 deletions examples/online-boutique/services/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
_ "embed"
"math/rand"

"ftl/builtin"

"github.com/TBD54566975/ftl/examples/online-boutique/common"
"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -34,15 +36,17 @@ type AdResponse struct {

//ftl:verb
//ftl:ingress GET /ad
func Get(ctx context.Context, req AdRequest) (AdResponse, error) {
resp := AdResponse{}
if len(req.ContextKeys) > 0 {
resp.Ads = contextualAds(req.ContextKeys)
}
if len(resp.Ads) == 0 {
resp.Ads = randomAds()
func Get(ctx context.Context, req builtin.HttpRequest[AdRequest]) (builtin.HttpResponse[AdResponse], error) {
var ads []Ad
if len(req.Body.ContextKeys) > 0 {
ads = contextualAds(req.Body.ContextKeys)
} else {
ads = randomAds()
}
return resp, nil

return builtin.HttpResponse[AdResponse]{
Body: AdResponse{Name: "ad", Ads: ads},
}, nil
}

func contextualAds(contextKeys []string) (ads []Ad) {
Expand Down
23 changes: 15 additions & 8 deletions examples/online-boutique/services/cart/cart.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cart

import (
"context"
"ftl/builtin"
)

var store = NewStore()
Expand All @@ -26,9 +27,11 @@ type Cart struct {

//ftl:verb
//ftl:ingress POST /cart/add
func AddItem(ctx context.Context, req AddItemRequest) (AddItemResponse, error) {
store.Add(req.UserID, req.Item)
return AddItemResponse{}, nil
func AddItem(ctx context.Context, req builtin.HttpRequest[AddItemRequest]) (builtin.HttpResponse[AddItemResponse], error) {
store.Add(req.Body.UserID, req.Body.Item)
return builtin.HttpResponse[AddItemResponse]{
Body: AddItemResponse{},
}, nil
}

type GetCartRequest struct {
Expand All @@ -37,8 +40,10 @@ type GetCartRequest struct {

//ftl:verb
//ftl:ingress GET /cart
func GetCart(ctx context.Context, req GetCartRequest) (Cart, error) {
return Cart{Items: store.Get(req.UserID), UserID: req.UserID}, nil
func GetCart(ctx context.Context, req builtin.HttpRequest[GetCartRequest]) (builtin.HttpResponse[Cart], error) {
return builtin.HttpResponse[Cart]{
Body: Cart{Items: store.Get(req.Body.UserID), UserID: req.Body.UserID},
}, nil
}

type EmptyCartRequest struct {
Expand All @@ -49,7 +54,9 @@ type EmptyCartResponse struct{}

//ftl:verb
//ftl:ingress POST /cart/empty
func EmptyCart(ctx context.Context, req EmptyCartRequest) (EmptyCartResponse, error) {
store.Empty(req.UserID)
return EmptyCartResponse{}, nil
func EmptyCart(ctx context.Context, req builtin.HttpRequest[EmptyCartRequest]) (builtin.HttpResponse[EmptyCartResponse], error) {
store.Empty(req.Body.UserID)
return builtin.HttpResponse[EmptyCartResponse]{
Body: EmptyCartResponse{},
}, nil
}
Loading

0 comments on commit 80e2420

Please sign in to comment.