-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add docs/develop/go * Update Cloud docs * Use main() in service and virtual object * Go quickstart * Update Go SDK * Add tour * Update for latest go sdk * Update docs/develop/go/journaling-results.mdx Co-authored-by: Francesco Guardiani <[email protected]> * Review comments * *string for default check * Add go to github workflows * Add logo to overview * Go in concepts pages * Add go microservice use case * Event processing go docs * Reduce tab spacing * Use 0.11.0 --------- Co-authored-by: Francesco Guardiani <[email protected]>
- Loading branch information
1 parent
4fe82f7
commit 0b3392f
Showing
56 changed files
with
4,188 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package concepts | ||
|
||
import restate "github.com/restatedev/sdk-go" | ||
|
||
type OrderProcessor struct{} | ||
|
||
// <start_here> | ||
// <mark_1> | ||
func (OrderProcessor) Process(ctx restate.ObjectContext, order Order) error { | ||
// </mark_1> | ||
// 1. Set status | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_CREATED) | ||
// </mark_4> | ||
|
||
// 2. Handle payment | ||
// <mark_5> | ||
token := restate.Rand(ctx).UUID().String() | ||
paid, err := restate.Run(ctx, func(ctx restate.RunContext) (bool, error) { | ||
return paymentClnt.Charge(ctx, order.Id, token, order.TotalCost) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// </mark_5> | ||
|
||
if !paid { | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_REJECTED) | ||
// </mark_4> | ||
return nil | ||
} | ||
|
||
// 3. Wait until the requested preparation time | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_SCHEDULED) | ||
// </mark_4> | ||
if err := restate.Sleep(ctx, order.DeliveryDelay); err != nil { | ||
return err | ||
} | ||
|
||
// 4. Trigger preparation | ||
// <mark_3> | ||
preparationAwakeable := restate.Awakeable[restate.Void](ctx) | ||
// <mark_5> | ||
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) { | ||
return restate.Void{}, restaurant.Prepare(order.Id, preparationAwakeable.Id()) | ||
}); err != nil { | ||
return err | ||
} | ||
// </mark_5> | ||
// </mark_3> | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_IN_PREPARATION) | ||
// </mark_4> | ||
|
||
// <mark_3> | ||
if _, err := preparationAwakeable.Result(); err != nil { | ||
return err | ||
} | ||
// </mark_3> | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_SCHEDULING_DELIVERY) | ||
// </mark_4> | ||
|
||
// 5. Find a driver and start delivery | ||
// <mark_2> | ||
if _, err := restate.Object[restate.Void](ctx, "DeliveryManager", order.Id, "StartDelivery"). | ||
Request(order); err != nil { | ||
return err | ||
} | ||
// </mark_2> | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_DELIVERED) | ||
// </mark_4> | ||
|
||
return nil | ||
} | ||
|
||
// <end_here> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package concepts | ||
|
||
import ( | ||
"time" | ||
|
||
restate "github.com/restatedev/sdk-go" | ||
) | ||
|
||
type MyService struct{} | ||
|
||
/* | ||
// <start_rpc_call> | ||
func (MyService) MyRestateHandler(ctx restate.Context) error { | ||
// focus | ||
greet, err := restate. | ||
// focus | ||
Service[string](ctx, "Greeter", "Greet"). | ||
// focus | ||
Request("Hi") | ||
return err | ||
} | ||
// <end_rpc_call> | ||
// <start_one_way_call> | ||
func (MyService) MyRestateHandler(ctx restate.Context) error { | ||
// focus | ||
restate. | ||
// focus | ||
ServiceSend(ctx, "Greeter", "Greet"). | ||
// focus | ||
Send("Hi") | ||
return nil | ||
} | ||
// <end_one_way_call> | ||
*/ | ||
|
||
// <start_delayed_call> | ||
func (MyService) MyRestateHandler(ctx restate.Context) error { | ||
// focus | ||
restate. | ||
// focus | ||
ServiceSend(ctx, "Greeter", "Greet"). | ||
// focus | ||
Send("Hi", restate.WithDelay(1*time.Second)) | ||
return nil | ||
} | ||
|
||
// <end_delayed_call> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
restate "github.com/restatedev/sdk-go" | ||
"github.com/restatedev/sdk-go/server" | ||
) | ||
|
||
type RoleUpdate struct{} | ||
|
||
// <start_here> | ||
// <mark_2> | ||
func (RoleUpdate) ApplyRoleUpdate(ctx restate.Context, update UpdateRequest) error { | ||
// </mark_2> | ||
|
||
// <mark_1> | ||
success, err := restate.Run(ctx, func(ctx restate.RunContext) (bool, error) { | ||
return applyUserRole(update.UserId, update.Role) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// </mark_1> | ||
// <mark_3> | ||
if !success { | ||
return nil | ||
} | ||
// </mark_3> | ||
|
||
// <mark_3> | ||
for _, permission := range update.Permissions { | ||
// </mark_3> | ||
// <mark_1> | ||
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) { | ||
return restate.Void{}, applyPermission(update.UserId, permission) | ||
}); err != nil { | ||
return err | ||
} | ||
// </mark_1> | ||
// <mark_3> | ||
} | ||
// </mark_3> | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
if err := server.NewRestate(). | ||
Bind(restate.Reflect(RoleUpdate{})). | ||
Start(context.Background(), ":9080"); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// <end_here> | ||
|
||
type UserRole struct { | ||
RoleKey string | ||
RoleDescription string | ||
} | ||
|
||
type Permission struct { | ||
PermissionKey string | ||
Setting string | ||
} | ||
|
||
type UpdateRequest struct { | ||
UserId string | ||
Role UserRole | ||
Permissions []Permission | ||
} | ||
|
||
func applyUserRole( | ||
userId string, | ||
userRole UserRole, | ||
) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
func applyPermission( | ||
userId string, | ||
permission Permission, | ||
) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package concepts | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type Status int | ||
|
||
const ( | ||
Status_NEW Status = iota | ||
Status_CREATED | ||
Status_SCHEDULED | ||
Status_IN_PREPARATION | ||
Status_SCHEDULING_DELIVERY | ||
Status_WAITING_FOR_DRIVER | ||
Status_IN_DELIVERY | ||
Status_DELIVERED | ||
Status_REJECTED | ||
Status_CANCELLED | ||
Status_UNKNOWN | ||
) | ||
|
||
type Order struct { | ||
Id string | ||
TotalCost int | ||
DeliveryDelay time.Duration | ||
} | ||
|
||
type PaymentClient struct{} | ||
|
||
func (PaymentClient) Charge(ctx context.Context, id string, token string, amount int) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
var paymentClnt PaymentClient | ||
|
||
type RestaurantClient struct{} | ||
|
||
func (RestaurantClient) Prepare(id string, cb string) error { | ||
return nil | ||
} | ||
|
||
var restaurant = RestaurantClient{} |
Oops, something went wrong.