-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsst.config.ts
228 lines (211 loc) · 7.64 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/// <reference path="./.sst/platform/config.d.ts" />
export default $config({
app(input) {
return {
name: "paypals",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
providers: {
aws: {
region: "us-west-2",
profile:
input.stage === "production"
? "paypals-prod"
: "paypals-dev",
},
},
};
},
async run() {
const DB_URL = new sst.Secret("DB_URL");
const DB_TOKEN = new sst.Secret("DB_TOKEN");
const DNS_MAPPING: Record<string, string> = {
production: "bivvy.cc",
prod: "bivvy.cc",
development: "dev.bivvy.cc",
dev: "dev.bivvy.cc",
};
const stage = $app.stage;
const zone = DNS_MAPPING[stage] || "dev.bivvy.cc";
const domain = DNS_MAPPING[stage] || `${stage}.dev.bivvy.cc`;
const AUTH_DOMAIN = "auth." + domain;
let auth: sst.aws.Auth;
{
const gcid = new sst.Secret("GOOGLE_CLIENT_ID");
const gcids = new sst.Secret("GOOGLE_CLIENT_ID_SECRET");
auth = new sst.aws.Auth("auth", {
authenticator: {
handler: "packages/functions/auth/auth.handler",
link: [gcid, gcids, DB_URL, DB_TOKEN],
runtime: "nodejs18.x",
architecture: "x86_64",
nodejs: {
install: ["@libsql/client", "@libsql/linux-x64-gnu"],
},
},
});
const _authRouter = new sst.aws.Router("AuthRouter", {
domain: AUTH_DOMAIN,
routes: {
"/*": auth.url,
},
});
auth.url.apply(console.log.bind(null, "AUTH URL:"));
}
$transform(sst.aws.Function, (args, opts) => {
args.link ??= [...(args.link ?? []), auth];
return args;
});
const DBKeepAlive = new sst.aws.Cron("KeepAlive", {
schedule: "rate(1 minute)",
job: {
handler: "packages/functions/lambdas/db/keep-alive.handler",
live: false,
link: [DB_URL, DB_TOKEN],
runtime: "nodejs18.x",
architecture: "x86_64",
nodejs: {
install: ["@libsql/client", "@libsql/linux-x64-gnu"],
},
},
});
const clientTable = new sst.aws.Dynamo("clientTable", {
fields: {
ClientGroupId: "string",
ClientId: "string",
// LastMutationId: "number",
// UserId: "string",
// ExpireAt: "number",
},
// FIXME: ttl attribute
primaryIndex: {
hashKey: "ClientGroupId",
rangeKey: "ClientId",
},
ttl: "ExpireAt",
});
const api = new sst.aws.ApiGatewayV2("api", {
cors: {
// TODO: prod url
allowOrigins: ["*"],
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
},
domain: {
name: "api." + domain,
},
});
{
api.route("POST /pull", {
handler: "packages/functions/lambdas/pull.handler",
link: [clientTable, DB_URL, DB_TOKEN, auth],
environment: {
CLIENT_TABLE_NAME: clientTable.name,
REGION: $app.providers!.aws.region,
// SST_REGION: clientTable.nodes.table.restoreSourceName,
},
// live: false,
// memory: "2 GB",
runtime: "nodejs18.x",
architecture: "x86_64",
nodejs: {
install: ["@libsql/client", "@libsql/linux-x64-gnu"],
},
});
api.route("POST /push", {
handler: "packages/functions/lambdas/push.handler",
link: [clientTable, DB_URL, DB_TOKEN, auth],
environment: {
CLIENT_TABLE_NAME: clientTable.name,
// SST_REGION: stack.region,
},
runtime: "nodejs18.x",
architecture: "x86_64",
nodejs: {
install: ["@libsql/client", "@libsql/linux-x64-gnu"],
},
});
const SCAN_LIVE = undefined
api.route("POST /scan/receipt", {
handler: "packages/functions/scan/receipt.handler",
link: [auth],
live: SCAN_LIVE,
runtime: "nodejs20.x",
permissions: [
{
actions: ["textract:AnalyzeExpense"],
resources: ["*"],
},
],
environment: {
IS_LOCAL: String($dev && SCAN_LIVE)
}
}, {
});
// api.route("POST /scan/spreadsheet", {
// handler: "packages/functions/lambdas/scan/table/table.go",
// runtime: "go",
// });
api.route("GET /session", {
handler: "packages/functions/auth/validate-session.handler",
link: [auth, DB_URL, DB_TOKEN],
runtime: "nodejs18.x",
architecture: "x86_64",
nodejs: {
install: ["@libsql/client", "@libsql/linux-x64-gnu"],
},
});
api.route("GET /invite", {
handler: "packages/functions/auth/invite.createHandler",
link: [auth, DB_URL, DB_TOKEN],
runtime: "nodejs18.x",
architecture: "x86_64",
nodejs: {
install: ["@libsql/client", "@libsql/linux-x64-gnu"],
},
});
api.route("GET /invite/validate", {
handler: "packages/functions/auth/invite.validateHandler",
link: [auth, DB_URL, DB_TOKEN],
runtime: "nodejs18.x",
architecture: "x86_64",
nodejs: {
install: ["@libsql/client", "@libsql/linux-x64-gnu"],
},
});
}
// stack.addOutputs({
// ApiEndpoint: api.url,
// ApiEndpoints: "\n " + api.routes.join("\n "),
// });
//
const REPLICACHE_LICENSE_KEY = new sst.Secret(
"REPLICACHE_LICENSE_KEY",
"lf7fcf72797fa44a3a0b0469a7af59d61"
);
const site = new sst.aws.StaticSite("Site", {
path: "packages/site",
build: {
output: "dist",
command: "pnpm run build",
},
environment: {
VITE_IS_LOCAL: String($dev),
VITE_API_URL: api.url,
VITE_AUTH_URL: "https://" + AUTH_DOMAIN,
VITE_REPLICACHE_LICENSE_KEY: REPLICACHE_LICENSE_KEY.value,
},
domain: !$dev
? {
name: domain,
}
: undefined,
});
let SITE_URL = site.url.apply((url) => {
if ($dev) return "http://localhost:5173";
if (!url.startsWith("http")) {
return "https://" + url;
}
return url;
});
},
});