Skip to content

Commit 6760bc9

Browse files
committed
chore: wip
1 parent e6f7928 commit 6760bc9

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

.stacks/core/cloud/src/cloud.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
aws_s3_deployment as s3deploy,
1616
aws_route53_targets as targets,
1717
aws_wafv2 as wafv2,
18+
Fn,
1819
} from 'aws-cdk-lib'
1920
import { hasFiles } from '@stacksjs/storage'
2021
import { path as p } from '@stacksjs/path'
@@ -24,7 +25,7 @@ export class StacksCloud extends Stack {
2425
domain: string
2526
apiPath: string
2627
docsDomain: string
27-
apiVanityUrl?: string
28+
apiVanityUrl: string
2829
vanityUrl: string
2930
docsSource: string
3031
websiteSource: string
@@ -41,6 +42,7 @@ export class StacksCloud extends Stack {
4142
firewall: wafv2.CfnWebACL
4243
originAccessIdentity: cloudfront.OriginAccessIdentity
4344
cdnCachePolicy: cloudfront.CachePolicy
45+
apiCachePolicy: cloudfront.CachePolicy | undefined
4446

4547
constructor(scope: Construct, id: string, props?: StackProps) {
4648
super(scope, id, props)
@@ -54,6 +56,7 @@ export class StacksCloud extends Stack {
5456
this.docsSource = '../../../storage/framework/docs'
5557
this.websiteSource = app.docMode ? this.docsSource : '../../../storage/public'
5658
this.privateSource = '../../../storage/private'
59+
this.apiVanityUrl = ''
5760

5861
this.zone = this.manageZone()
5962
this.certificate = this.manageCertificate()
@@ -80,8 +83,11 @@ export class StacksCloud extends Stack {
8083
return cloud.deploy?.api
8184
}
8285

83-
apiCachePolicy() {
84-
return new cloudfront.CachePolicy(this, 'StacksApiCachePolicy', {
86+
setApiCachePolicy() {
87+
if (this.apiCachePolicy)
88+
return this.apiCachePolicy
89+
90+
this.apiCachePolicy = new cloudfront.CachePolicy(this, 'StacksApiCachePolicy', {
8591
comment: 'Stacks API Cache Policy',
8692
cachePolicyName: 'StacksApiCachePolicy',
8793
// minTtl: cloud.cdn?.minTtl ? Duration.seconds(cloud.cdn.minTtl) : undefined,
@@ -90,9 +96,11 @@ export class StacksCloud extends Stack {
9096
headerBehavior: cloudfront.CacheHeaderBehavior.allowList('Accept', 'x-api-key', 'Authorization'),
9197
queryStringBehavior: cloudfront.CacheQueryStringBehavior.none(),
9298
})
99+
100+
return this.apiCachePolicy
93101
}
94102

95-
deployApi(): lambda.FunctionUrl {
103+
deployApi() {
96104
const layer = new lambda.LayerVersion(this, 'StacksLambdaLayer', {
97105
code: lambda.Code.fromAsset(p.projectStoragePath('framework/cloud/bun-lambda-layer.zip')),
98106
compatibleRuntimes: [lambda.Runtime.PROVIDED_AL2],
@@ -124,8 +132,6 @@ export class StacksCloud extends Stack {
124132
})
125133

126134
this.apiVanityUrl = api.url
127-
128-
return api
129135
}
130136

131137
allowedMethodsFromString(methods?: 'ALL' | 'GET_HEAD' | 'GET_HEAD_OPTIONS'): cloudfront.AllowedMethods {
@@ -321,15 +327,25 @@ export class StacksCloud extends Stack {
321327
this.deployApi()
322328

323329
behaviorOptions = {
330+
'/api': {
331+
origin: new origins.HttpOrigin(Fn.select(2, Fn.split('/', this.apiVanityUrl)), { // removes the https://
332+
originPath: '/api',
333+
protocolPolicy: cloudfront.OriginProtocolPolicy.HTTPS_ONLY,
334+
}),
335+
compress: true,
336+
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
337+
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
338+
cachePolicy: this.setApiCachePolicy(),
339+
},
324340
'/api/*': {
325-
origin: new origins.HttpOrigin(this.domain, {
341+
origin: new origins.HttpOrigin(Fn.select(2, Fn.split('/', this.apiVanityUrl)), { // removes the https://
326342
originPath: '/api',
327343
protocolPolicy: cloudfront.OriginProtocolPolicy.HTTPS_ONLY,
328344
}),
329345
compress: true,
330346
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
331347
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
332-
cachePolicy: this.apiCachePolicy(),
348+
cachePolicy: this.apiCachePolicy,
333349
},
334350
}
335351
}

0 commit comments

Comments
 (0)