@@ -47,54 +47,42 @@ import (
47
47
" context"
48
48
" fmt"
49
49
50
- " github.com/nitrictech/go-sdk/handler"
51
50
" github.com/nitrictech/go-sdk/nitric"
51
+ " github.com/nitrictech/go-sdk/nitric/keyvalue"
52
+ " github.com/nitrictech/go-sdk/nitric/websockets"
52
53
)
53
54
54
55
func main () {
55
56
// 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" )
61
58
62
59
// 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 )
68
61
69
62
// 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 ) {
71
64
err := connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
72
65
" connectionId" : ctx.Request .ConnectionID (),
73
66
})
74
67
if err != nil {
75
- return ctx, err
68
+ return
76
69
}
77
-
78
- return next (ctx)
79
70
})
80
71
81
72
// 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 ) {
83
74
err := connections.Delete (context.TODO (), ctx.Request .ConnectionID ())
84
75
if err != nil {
85
- return ctx, err
76
+ return
86
77
}
87
-
88
- return next (ctx)
89
78
})
90
79
91
80
// 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 ) {
93
82
connectionStream , err := connections.Keys (context.TODO ())
94
83
if err != nil {
95
- return ctx, err
84
+ return
96
85
}
97
-
98
86
senderId := ctx.Request .ConnectionID ()
99
87
100
88
for {
@@ -110,17 +98,12 @@ func main() {
110
98
message := fmt.Sprintf (" %s : %s " , senderId, ctx.Request .Message ())
111
99
err = ws.Send (context.TODO (), connectionId, []byte (message))
112
100
if err != nil {
113
- return ctx, err
101
+ return
114
102
}
115
103
}
116
-
117
- return next (ctx)
118
104
})
119
105
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 ()
124
107
}
125
108
```
126
109
@@ -173,14 +156,14 @@ go get github.com/aws/aws-sdk-go/service/iam
173
156
174
157
### Create the Test File
175
158
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:
177
160
178
161
``` bash
179
162
mkdir -p test
180
- touch test/test_terraform_resources .go
163
+ touch test/terraform_resources_test .go
181
164
```
182
165
183
- Add the following code to ` test_terraform_resources .go` :
166
+ Add the following code to ` terraform_resources_test .go` :
184
167
185
168
``` go
186
169
package test
@@ -199,7 +182,7 @@ import (
199
182
func TestTerraformResources (t *testing .T ) {
200
183
// Set Terraform options, specifying the directory with your Terraform configuration
201
184
terraformOptions := &terraform.Options {
202
- TerraformDir: " ../cdktf.out/stacks/go-realtime -dev" ,
185
+ TerraformDir: " ../cdktf.out/stacks/my-websocket-app -dev" ,
203
186
}
204
187
205
188
// Ensure resources are destroyed after test completion
0 commit comments