-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsst.config.ts
171 lines (155 loc) · 4.81 KB
/
sst.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { SSTConfig } from "sst";
import { Api, WebSocketApi, Table, Queue } from "sst/constructs";
import * as iam from "aws-cdk-lib/aws-iam";
import * as sqs from "aws-cdk-lib/aws-sqs";
export default {
config(_input) {
return {
name: "notification",
region: "eu-west-1",
};
},
stacks(app) {
app.setDefaultFunctionProps({
runtime: "go1.x",
});
app.stack(function Stack({ stack }) {
// queues
const notifyConnection = new Queue(stack, "notifyConnection");
const notifyUser = new Queue(stack, "notifyUser");
const deleteConnectionDeadLetter = new Queue(stack, "deleteConnectionDeadLetter");
const deleteConnection = new Queue(stack, "deleteConnection", {
cdk: {
queue: {
deadLetterQueue: {
queue: deleteConnectionDeadLetter.cdk.queue,
maxReceiveCount: 4,
},
}
}
});
// REST api
const api = new Api(stack, "api", {
routes: {
"POST /token": "cmd/token/issuer/main.go",
},
});
// persistence for websockets
const userIdIndexName = 'UserIdIndex';
const connections = new Table(stack, "connections", {
fields: {
ConnectionId: "string",
UserId: "string",
},
primaryIndex: { partitionKey: "ConnectionId" },
globalIndexes: {
[userIdIndexName]: {
partitionKey: "UserId",
projection: "keys_only",
},
},
});
// websocket api
const wsApi = new WebSocketApi(stack, "wsapi", {
routes: {
// execute when connection is opened
$connect: {
function: {
timeout: 10,
handler: "cmd/connect/main.go",
permissions: [connections, deleteConnection],
environment: {
CONFIG_CONNECTIONS_TABLE_ID: connections.tableName,
CONFIG_SQS_DELETE_CONNECTION_URL: deleteConnection.queueUrl,
},
}
},
// execure when connection is closed
$disconnect: {
function: {
timeout: 10,
handler: "cmd/disconnect/main.go",
permissions: [connections],
environment: {
CONFIG_CONNECTIONS_TABLE_ID: connections.tableName,
},
}
},
// handle messages that don't match the pattern
$default: {
function: {
timeout: 10,
handler: "cmd/default/main.go",
permissions: [notifyConnection],
environment: {
CONFIG_SQS_NOTIFY_CONNECTION_URL: notifyConnection.queueUrl,
},
}
},
// handle specific messages
ping: {
function: {
timeout: 10,
handler: "cmd/ping/main.go",
permissions: [notifyConnection],
environment: {
CONFIG_SQS_NOTIFY_CONNECTION_URL: notifyConnection.queueUrl,
},
}
},
},
});
// notify connection consumer
notifyConnection.addConsumer(stack, {
function: {
timeout: 10,
handler: "cmd/notify_connection/main.go",
permissions: [wsApi],
environment: {
CONFIG_API_GATEWAY_ENDPOINT: wsApi.url.replace("wss://", "https://"),
},
}
});
// notify user queue consumer
notifyUser.addConsumer(stack, {
function: {
timeout: 10,
handler: "cmd/notify_user/main.go",
permissions: [notifyConnection, connections],
environment: {
CONFIG_CONNECTIONS_TABLE_ID: connections.tableName,
CONFIG_SQS_NOTIFY_CONNECTION_URL: notifyConnection.queueUrl,
CONFIG_USER_ID_INDEX_NAME: userIdIndexName,
},
}
});
// delete connection consumer
deleteConnection.addConsumer(stack, {
function: {
timeout: 10,
handler: "cmd/delete_connection/main.go",
permissions: [
// attach custom policy since the default
// _connectionsArn provided by wsApi
// is not enough
new iam.PolicyStatement({
actions: ["execute-api:ManageConnections"],
effect: iam.Effect.ALLOW,
resources: [
wsApi._connectionsArn.replace("/POST/*", "/*"),
],
}),
],
environment: {
CONFIG_API_GATEWAY_ENDPOINT: wsApi.url.replace("wss://", "https://"),
},
}
});
// set console outputs
stack.addOutputs({
ApiEndpoint: api.url,
WsApiEndpoint: wsApi.url,
});
});
},
} satisfies SSTConfig;