Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 3b5579e

Browse files
committed
fix: update code for new go SDK
1 parent 2bb8392 commit 3b5579e

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/pages/guides/terraform/terratest.mdx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import (
4747
"context"
4848
"fmt"
4949

50-
"github.com/nitrictech/go-sdk/handler"
5150
"github.com/nitrictech/go-sdk/nitric"
51+
"github.com/nitrictech/go-sdk/nitric/websockets"
5252
)
5353

5454
func main() {
@@ -67,34 +67,29 @@ func main() {
6767
}
6868

6969
// Handle new WebSocket connections by storing the connection ID in the KV store.
70-
ws.On(handler.WebsocketConnect, func(ctx *handler.WebsocketContext, next handler.WebsocketHandler) (*handler.WebsocketContext, error) {
70+
ws.On(websockets.EventType_Connect, func(ctx *websockets.Ctx) {
7171
err := connections.Set(context.TODO(), ctx.Request.ConnectionID(), map[string]interface{}{
7272
"connectionId": ctx.Request.ConnectionID(),
7373
})
7474
if err != nil {
75-
return ctx, err
75+
return
7676
}
77-
78-
return next(ctx)
7977
})
8078

8179
// Handle WebSocket disconnections by removing the connection ID from the KV store.
82-
ws.On(handler.WebsocketDisconnect, func(ctx *handler.WebsocketContext, next handler.WebsocketHandler) (*handler.WebsocketContext, error) {
80+
ws.On(websockets.EventType_Disconnect, func(ctx *websockets.Ctx) {
8381
err := connections.Delete(context.TODO(), ctx.Request.ConnectionID())
8482
if err != nil {
85-
return ctx, err
83+
return
8684
}
87-
88-
return next(ctx)
8985
})
9086

9187
// Handle incoming messages by broadcasting them to all other connections.
92-
ws.On(handler.WebsocketMessage, func(ctx *handler.WebsocketContext, next handler.WebsocketHandler) (*handler.WebsocketContext, error) {
88+
ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) {
9389
connectionStream, err := connections.Keys(context.TODO())
9490
if err != nil {
95-
return ctx, err
91+
return
9692
}
97-
9893
senderId := ctx.Request.ConnectionID()
9994

10095
for {
@@ -110,11 +105,9 @@ func main() {
110105
message := fmt.Sprintf("%s: %s", senderId, ctx.Request.Message())
111106
err = ws.Send(context.TODO(), connectionId, []byte(message))
112107
if err != nil {
113-
return ctx, err
108+
return
114109
}
115110
}
116-
117-
return next(ctx)
118111
})
119112

120113
// Start the Nitric service to handle WebSocket events.
@@ -173,14 +166,14 @@ go get github.com/aws/aws-sdk-go/service/iam
173166

174167
### Create the Test File
175168

176-
Create a new file named `test_terraform_resources.go` in your project’s test directory:
169+
Create a new file named `terraform_resources_test.go` in your project’s test directory:
177170

178171
```bash
179172
mkdir -p test
180-
touch test/test_terraform_resources.go
173+
touch test/terraform_resources_test.go
181174
```
182175

183-
Add the following code to `test_terraform_resources.go`:
176+
Add the following code to `terraform_resources_test.go`:
184177

185178
```go
186179
package test
@@ -199,7 +192,7 @@ import (
199192
func TestTerraformResources(t *testing.T) {
200193
// Set Terraform options, specifying the directory with your Terraform configuration
201194
terraformOptions := &terraform.Options{
202-
TerraformDir: "../cdktf.out/stacks/go-realtime-dev",
195+
TerraformDir: "../cdktf.out/stacks/my-websocket-app-dev",
203196
}
204197

205198
// Ensure resources are destroyed after test completion

0 commit comments

Comments
 (0)