-
Notifications
You must be signed in to change notification settings - Fork 11
/
sst.config.ts
85 lines (78 loc) · 2.52 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
import { SSTConfig } from "sst"
import { Api, NextjsSite, use, WebSocketApi } from "sst/constructs"
const DATABENTO_API_KEY = process.env.DATABENTO_API_KEY ?? "db-key-error"
import {
ViewerProtocolPolicy,
OriginRequestPolicy,
AllowedMethods,
OriginProtocolPolicy,
} from "aws-cdk-lib/aws-cloudfront"
import * as cloudfront from "aws-cdk-lib/aws-cloudfront"
import { HttpOrigin } from "aws-cdk-lib/aws-cloudfront-origins"
import { WebsocketStack } from "./websocketStack"
export default {
config(_input) {
return {
name: "trading-demo",
region: "us-east-1",
}
},
stacks(app) {
app.stack(WebsocketStack)
app.stack(function Site({ stack }) {
const { websocket } = use(WebsocketStack)
const cfFunction = new cloudfront.Function(stack, "Function", {
code: cloudfront.FunctionCode.fromInline(
`function handler(event) {
var request = event.request;
var uri = request.uri;
uri = uri.replace('/plausible/event', '/api/event');
request.uri = uri;
return request;
}`
),
})
const plausibleOrigin = new HttpOrigin("plausible.io", {
protocolPolicy: OriginProtocolPolicy.HTTPS_ONLY,
})
const nextjs = new NextjsSite(stack, "nextjs", {
path: "nextjs",
environment: {
NEXT_PUBLIC_WS_URL: websocket.url,
},
customDomain: app.stage === "prod" ? {
domainName: "orderflowreplay.com",
domainAlias: "www.orderflowreplay.com",
}: undefined,
cdk: {
distribution: {
additionalBehaviors: {
"/js/script.tagged-events.js": {
origin: plausibleOrigin,
viewerProtocolPolicy: ViewerProtocolPolicy.HTTPS_ONLY,
},
"/plausible/event": {
functionAssociations: [
{
function: cfFunction,
eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
},
],
origin: plausibleOrigin,
originRequestPolicy: new OriginRequestPolicy(
stack,
"Managed-UserAgentRefererHeaders"
),
allowedMethods: AllowedMethods.ALLOW_ALL,
viewerProtocolPolicy: ViewerProtocolPolicy.HTTPS_ONLY,
},
},
},
},
})
stack.addOutputs({
Nextjs: nextjs.url,
})
})
},
} satisfies SSTConfig