Skip to content

Commit d5bb855

Browse files
authored
New quickstart (#214)
* new greeter template to show durable execution * new greeter template for Java templates * new greeter template for Kotlin templates * new greeter template for Go templates * new greeter template for Python templates * new greeter template for Rust templates * Fix comments * Add ghost to error messages * Fix wrong imports in templates * Remove licenses of templates because they are shown as code snippets in the docs * Make code snippets more concise * Use pydantic for Python template * throw runtime exception in java templates * Fix import in TS deno template * Kotlin template: Rename to utils.kt * Upgrade go version * Fix rust template * Fix Java templates issues * Fix cloudflare workers template * Fix package name quarkus template * Fix Rust shuttle template * Clean imports
1 parent 28ded7c commit d5bb855

File tree

42 files changed

+551
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+551
-172
lines changed

templates/bun/src/index.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import { Context, endpoint, service } from "@restatedev/restate-sdk/fetch";
1+
import * as restate from "@restatedev/restate-sdk/fetch";
2+
import {sendNotification, sendReminder} from "./utils";
23

3-
// Template of a Restate service and handler
4-
//
5-
// Have a look at the TS QuickStart: https://docs.restate.dev/get_started/quickstart?sdk=ts
6-
//
4+
const handler = restate
5+
.endpoint()
6+
.bind(
7+
restate.service({
8+
name: "Greeter",
9+
handlers: {
10+
greet: async (ctx: restate.Context, name: string) => {
711

8-
const greeter = service({
9-
name: "Greeter",
10-
handlers: {
11-
greet: async (ctx: Context, greeting: string) => {
12-
return `${greeting}!`;
13-
},
14-
},
15-
});
12+
// Durably execute a set of steps; resilient against failures
13+
const greetingId = ctx.rand.uuidv4();
14+
await ctx.run(() => sendNotification(greetingId, name));
15+
await ctx.sleep(1000);
16+
await ctx.run(() => sendReminder(greetingId));
1617

17-
const handler = endpoint().bind(greeter).handler();
18+
// Respond to caller
19+
return `You said hi to ${name}!`;
20+
},
21+
},
22+
}),
23+
)
24+
.handler();
1825

1926
const server = Bun.serve({
2027
port: 9080,

templates/bun/src/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
// You can remove this file.
3+
// It's only purpose is providing stubs for the template.
4+
5+
export function sendNotification(greetingId: string, name: string) {
6+
if (Math.random() < 0.5) { // 50% chance of failure
7+
console.error(`👻 Failed to send notification: ${greetingId} - ${name}`);
8+
throw new Error(`Failed to send notification ${greetingId} - ${name}`);
9+
}
10+
console.log(`Notification sent: ${greetingId} - ${name}`);
11+
}
12+
13+
export function sendReminder(greetingId: string) {
14+
if (Math.random() < 0.5) { // 50% chance of failure
15+
console.error(`👻 Failed to send reminder: ${greetingId}`);
16+
throw new Error(`Failed to send reminder: ${greetingId}`);
17+
}
18+
console.log(`Reminder sent: ${greetingId}`);
19+
}
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
import { Context, endpoint, service } from "@restatedev/restate-sdk-cloudflare-workers/fetch";
1+
import * as restate from "@restatedev/restate-sdk-cloudflare-workers/fetch";
2+
import { sendNotification, sendReminder } from "./utils.js";
23

3-
// Template of a Restate service and handler
4-
//
5-
// Have a look at the TS QuickStart: https://docs.restate.dev/get_started/quickstart?sdk=ts
6-
//
4+
export default restate
5+
.endpoint()
6+
.bind(
7+
restate.service({
8+
name: "greeter",
9+
handlers: {
10+
greet: async (ctx: restate.Context, name: string) => {
11+
// Durably execute a set of steps; resilient against failures
12+
const greetingId = ctx.rand.uuidv4();
13+
await ctx.run(() => sendNotification(greetingId, name));
14+
await ctx.sleep(1000);
15+
await ctx.run(() => sendReminder(greetingId));
716

8-
const greeter = service({
9-
name: "greeter",
10-
handlers: {
11-
greet: async (ctx: Context, greeting: string) => {
12-
return `${greeting}!`;
13-
},
14-
},
15-
});
16-
17-
export default endpoint().bind(greeter).handler();
17+
// Respond to caller
18+
return `You said hi to ${name}!`;
19+
},
20+
},
21+
}),
22+
)
23+
.handler();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// You can remove this file.
2+
// It's only purpose is providing stubs for the template.
3+
4+
export function sendNotification(greetingId: string, name: string) {
5+
if (Math.random() < 0.5) {
6+
// 50% chance of failure
7+
console.error(`👻 Failed to send notification: ${greetingId} - ${name}`);
8+
throw new Error(`Failed to send notification ${greetingId} - ${name}`);
9+
}
10+
console.log(`Notification sent: ${greetingId} - ${name}`);
11+
}
12+
13+
export function sendReminder(greetingId: string) {
14+
if (Math.random() < 0.5) {
15+
// 50% chance of failure
16+
console.error(`👻 Failed to send reminder: ${greetingId}`);
17+
throw new Error(`Failed to send reminder: ${greetingId}`);
18+
}
19+
console.log(`Reminder sent: ${greetingId}`);
20+
}

templates/deno/main.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
import {
2-
Context,
3-
endpoint,
4-
service,
5-
} from "npm:@restatedev/restate-sdk@^1.4.0/fetch";
1+
import * as restate from "npm:@restatedev/restate-sdk/fetch";
2+
import { sendNotification, sendReminder } from "./utils.ts";
63

7-
// Template of a Restate service and handler
8-
//
9-
// Have a look at the TS QuickStart: https://docs.restate.dev/get_started/quickstart?sdk=ts
10-
//
4+
const handler = restate
5+
.endpoint()
6+
.bind(
7+
restate.service({
8+
name: "Greeter",
9+
handlers: {
10+
greet: async (ctx: restate.Context, name: string) => {
11+
// Durably execute a set of steps; resilient against failures
12+
const greetingId = ctx.rand.uuidv4();
13+
await ctx.run(() => sendNotification(greetingId, name));
14+
await ctx.sleep(1000);
15+
await ctx.run(() => sendReminder(greetingId));
1116

12-
const greeter = service({
13-
name: "Greeter",
14-
handlers: {
15-
greet: async (ctx: Context, greeting: string) => {
16-
return `${greeting}!`;
17-
},
18-
},
19-
});
20-
21-
const handler = endpoint().bind(greeter).bidirectional().handler();
17+
// Respond to caller
18+
return `You said hi to ${name}!`;
19+
},
20+
},
21+
}),
22+
)
23+
.bidirectional()
24+
.handler();
2225

2326
Deno.serve({ port: 9080 }, handler.fetch);

templates/deno/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// You can remove this file.
2+
// It's only purpose is providing stubs for the template.
3+
4+
export function sendNotification(greetingId: string, name: string) {
5+
if (Math.random() < 0.5) {
6+
// 50% chance of failure
7+
console.error(`👻 Failed to send notification: ${greetingId} - ${name}`);
8+
throw new Error(`Failed to send notification ${greetingId} - ${name}`);
9+
}
10+
console.log(`Notification sent: ${greetingId} - ${name}`);
11+
}
12+
13+
export function sendReminder(greetingId: string) {
14+
if (Math.random() < 0.5) {
15+
// 50% chance of failure
16+
console.error(`👻 Failed to send reminder: ${greetingId}`);
17+
throw new Error(`Failed to send reminder: ${greetingId}`);
18+
}
19+
console.log(`Reminder sent: ${greetingId}`);
20+
}

templates/go/go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ module github.com/restatedev/examples/templates/go
22

33
go 1.22.5
44

5-
require github.com/restatedev/sdk-go v0.11.0
5+
require github.com/restatedev/sdk-go v0.12.0
66

77
require (
8+
github.com/go-logr/logr v1.4.2 // indirect
9+
github.com/go-logr/stdr v1.2.2 // indirect
810
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
911
github.com/google/uuid v1.6.0 // indirect
1012
github.com/mr-tron/base58 v1.2.0 // indirect
13+
go.opentelemetry.io/otel v1.28.0 // indirect
14+
go.opentelemetry.io/otel/metric v1.28.0 // indirect
15+
go.opentelemetry.io/otel/trace v1.28.0 // indirect
1116
golang.org/x/net v0.23.0 // indirect
1217
golang.org/x/text v0.14.0 // indirect
1318
google.golang.org/protobuf v1.34.2 // indirect

templates/go/go.sum

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
4+
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
5+
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
6+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
7+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
38
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
49
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
5-
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
6-
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
10+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
11+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
712
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
813
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
914
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
1015
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
1116
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1217
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13-
github.com/restatedev/sdk-go v0.11.0 h1:5pN5XSlTxJBWHZEuY0fZPorKU6xncz5CPGnnCChgjxg=
14-
github.com/restatedev/sdk-go v0.11.0/go.mod h1:6gHoU8pyP7YQfFWxKG2u94u/TSGen0qN7BWowWNDw4Y=
18+
github.com/restatedev/sdk-go v0.12.0 h1:M9bdcsHvJALcg27U0s6V7HD1qNVFPh60XhM5H/n2zlQ=
19+
github.com/restatedev/sdk-go v0.12.0/go.mod h1:Xalv67a5uOgGcbz7U1BgZQydCrsmENq2RAeTwTGXHng=
1520
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1621
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
22+
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
23+
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
24+
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
25+
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
26+
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
27+
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
1728
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
1829
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
1930
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
2031
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
2132
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
2233
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
23-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
24-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2534
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
2635
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
2736
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

templates/go/greeter.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
package main
22

33
import (
4-
"fmt"
5-
64
restate "github.com/restatedev/sdk-go"
5+
"time"
76
)
87

98
// Greeter is a struct which represents a Restate service; reflection will turn exported methods into service handlers
109
type Greeter struct{}
1110

12-
func (Greeter) Greet(ctx restate.Context, greeting string) (string, error) {
13-
return fmt.Sprintf("%s!", greeting), nil
11+
func (Greeter) Greet(ctx restate.Context, name string) (string, error) {
12+
// Durably execute a set of steps; resilient against failures
13+
greetingId := restate.Rand(ctx).UUID().String()
14+
15+
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) {
16+
return restate.Void{}, SendNotification(greetingId, name)
17+
}); err != nil {
18+
return "", err
19+
}
20+
21+
if err := restate.Sleep(ctx, 1*time.Second); err != nil {
22+
return "", err
23+
}
24+
25+
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) {
26+
return restate.Void{}, SendReminder(greetingId)
27+
}); err != nil {
28+
return "", err
29+
}
30+
31+
// Respond to caller
32+
return "You said hi to " + name + "!", nil
1433
}

templates/go/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
func SendNotification(greetingId string, name string) error {
9+
if rand.Float32() < 0.5 { // 50% chance of failure
10+
fmt.Printf("👻 Failed to send notification: %s - %s\n", greetingId, name)
11+
return fmt.Errorf("failed to send notification: %s - %s", greetingId, name)
12+
}
13+
fmt.Printf("Notification sent: %s - %s\n", greetingId, name)
14+
return nil
15+
}
16+
17+
func SendReminder(greetingId string) error {
18+
if rand.Float32() < 0.5 { // 50% chance of failure
19+
fmt.Printf("👻 Failed to send reminder: %s\n", greetingId)
20+
return fmt.Errorf("failed to send reminder: %s", greetingId)
21+
}
22+
fmt.Printf("Reminder sent: %s\n", greetingId)
23+
return nil
24+
}

0 commit comments

Comments
 (0)