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

Commit 5e50d39

Browse files
committed
fix: update code for new go SDK
fix: go imports
1 parent 2bb8392 commit 5e50d39

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

src/pages/guides/terraform/terratest.mdx

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,54 +47,42 @@ 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/keyvalue"
52+
"github.com/nitrictech/go-sdk/nitric/websockets"
5253
)
5354

5455
func main() {
5556
// Create a WebSocket endpoint named "public".
56-
ws, err := nitric.NewWebsocket("public")
57-
if err != nil {
58-
fmt.Println("Error creating WebSocket:", err)
59-
return
60-
}
57+
ws := nitric.NewWebsocket("public")
6158

6259
// Initialize a KV store named "connections" with Get, Set, and Delete permissions.
63-
connections, err := nitric.NewKv("connections").Allow(nitric.KvStoreGet, nitric.KvStoreSet, nitric.KvStoreDelete)
64-
if err != nil {
65-
fmt.Println("Error creating KV store:", err)
66-
return
67-
}
60+
connections := nitric.NewKv("connections").Allow(keyvalue.KvStoreGet, keyvalue.KvStoreSet, keyvalue.KvStoreDelete)
6861

6962
// 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) {
63+
ws.On(websockets.EventType_Connect, func(ctx *websockets.Ctx) {
7164
err := connections.Set(context.TODO(), ctx.Request.ConnectionID(), map[string]interface{}{
7265
"connectionId": ctx.Request.ConnectionID(),
7366
})
7467
if err != nil {
75-
return ctx, err
68+
return
7669
}
77-
78-
return next(ctx)
7970
})
8071

8172
// 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) {
73+
ws.On(websockets.EventType_Disconnect, func(ctx *websockets.Ctx) {
8374
err := connections.Delete(context.TODO(), ctx.Request.ConnectionID())
8475
if err != nil {
85-
return ctx, err
76+
return
8677
}
87-
88-
return next(ctx)
8978
})
9079

9180
// 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) {
81+
ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) {
9382
connectionStream, err := connections.Keys(context.TODO())
9483
if err != nil {
95-
return ctx, err
84+
return
9685
}
97-
9886
senderId := ctx.Request.ConnectionID()
9987

10088
for {
@@ -110,17 +98,12 @@ func main() {
11098
message := fmt.Sprintf("%s: %s", senderId, ctx.Request.Message())
11199
err = ws.Send(context.TODO(), connectionId, []byte(message))
112100
if err != nil {
113-
return ctx, err
101+
return
114102
}
115103
}
116-
117-
return next(ctx)
118104
})
119105

120-
// Start the Nitric service to handle WebSocket events.
121-
if err := nitric.Run(); err != nil {
122-
fmt.Println("Error running Nitric service:", err)
123-
}
106+
nitric.Run()
124107
}
125108
```
126109

@@ -173,14 +156,14 @@ go get github.com/aws/aws-sdk-go/service/iam
173156

174157
### Create the Test File
175158

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

178161
```bash
179162
mkdir -p test
180-
touch test/test_terraform_resources.go
163+
touch test/terraform_resources_test.go
181164
```
182165

183-
Add the following code to `test_terraform_resources.go`:
166+
Add the following code to `terraform_resources_test.go`:
184167

185168
```go
186169
package test
@@ -199,7 +182,7 @@ import (
199182
func TestTerraformResources(t *testing.T) {
200183
// Set Terraform options, specifying the directory with your Terraform configuration
201184
terraformOptions := &terraform.Options{
202-
TerraformDir: "../cdktf.out/stacks/go-realtime-dev",
185+
TerraformDir: "../cdktf.out/stacks/my-websocket-app-dev",
203186
}
204187

205188
// Ensure resources are destroyed after test completion

0 commit comments

Comments
 (0)