Skip to content

Commit

Permalink
Root app support (#269)
Browse files Browse the repository at this point in the history
* Root app support

* Attempt to deploy Root App

* Fix root app and routes

* Try to fix routes

* More tests

* Fix curls

* Root apps do not depend on prefix

* Fix demo-app version replace
  • Loading branch information
huntharo authored Nov 7, 2022
1 parent fd39017 commit afab62e
Show file tree
Hide file tree
Showing 9 changed files with 2,170 additions and 72 deletions.
56 changes: 44 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ jobs:

- name: Stitch Version and Prefix into Simple Demo App
run: |
npx replace-in-file "/\/${DEMO_APP_NAME}\/0\.0\.0/g" ${PREFIX}/${DEMO_APP_NAME}/${PACKAGE_VERSION} --configFile=.demo-app-replace.config.js --isRegex
npx replace-in-file "/\/0\.0\.0/g" ${PREFIX}/${PACKAGE_VERSION} --configFile=.demo-app-replace.config.js --isRegex
- name: Stitch Prefix into Nextjs Demo App
run: |
Expand Down Expand Up @@ -225,13 +225,45 @@ jobs:
- name: Test Demo App
run: |
echo Testing App Frame Loading
curl --fail https://${EDGE_DOMAIN}${PREFIX}/${DEMO_APP_NAME}/
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/${DEMO_APP_NAME}/
echo Testing App HTML Loading
curl --fail https://${EDGE_DOMAIN}${PREFIX}/${DEMO_APP_NAME}/${PACKAGE_VERSION}/index.html
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/${DEMO_APP_NAME}/${PACKAGE_VERSION}/index.html
echo Testing App Method Invocation
curl --fail "https://${EDGE_DOMAIN}${PREFIX}/${DEMO_APP_NAME}/${PACKAGE_VERSION}/serverIncrement?currValue=1"
curl -H"accept: application/json" --fail "https://${EDGE_DOMAIN}${PREFIX}/${DEMO_APP_NAME}/${PACKAGE_VERSION}/serverIncrement?currValue=1"
echo Testing App Method Invocation on _next/data Route
curl --fail "https://${EDGE_DOMAIN}${PREFIX}/${DEMO_APP_NAME}/_next/data/${PACKAGE_VERSION}/serverIncrement?currValue=1"
curl -H"accept: application/json" --fail "https://${EDGE_DOMAIN}${PREFIX}/${DEMO_APP_NAME}/_next/data/${PACKAGE_VERSION}/serverIncrement?currValue=1"
- name: Publish Demo App to MicroApps - Root App
run: |
npx microapps-publish publish -a '[root]' \
-t lambda-url \
--startup-type direct \
-n ${PACKAGE_VERSION} \
-d ${DEPLOYER_LAMBDA_NAME} \
-l ${DEMO_APP_LAMBDA_NAME} \
-s packages/demo-app/static_files \
-i index.html --overwrite --noCache
- name: Demo App URL - Root App
uses: Sibz/github-status-action@v1
with:
authToken: ${{secrets.GITHUB_TOKEN}}
context: 'Demo App (Root App) URL on ${{ matrix.deployName }}'
description: 'Passed'
state: 'success'
sha: ${{github.event.pull_request.head.sha || github.sha}}
target_url: https://${{ steps.getCDKExports.outputs.edgeDomain }}${{ steps.getCDKExports.outputs.prefix }}/?appver=${{ env.PACKAGE_VERSION }}

- name: Test Demo App - Root App
run: |
echo Testing App Frame Loading
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/
echo Testing App HTML Loading
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/${PACKAGE_VERSION}/index.html
echo Testing App Method Invocation
curl -H"accept: application/json" --fail "https://${EDGE_DOMAIN}${PREFIX}/${PACKAGE_VERSION}/serverIncrement?currValue=1"
echo Testing App Method Invocation on _next/data Route
curl -H"accept: application/json" --fail "https://${EDGE_DOMAIN}${PREFIX}/_next/data/${PACKAGE_VERSION}/serverIncrement?currValue=1"
- name: Publish Nextjs Demo App to MicroApps
run: |
Expand All @@ -257,13 +289,13 @@ jobs:
- name: Test Nextjs Demo App
run: |
echo Testing App Frame Loading
curl --fail https://${EDGE_DOMAIN}${PREFIX}/${NEXTJS_DEMO_APP_NAME}/
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/${NEXTJS_DEMO_APP_NAME}/
echo Testing App HTML Loading
curl --fail https://${EDGE_DOMAIN}${PREFIX}/${NEXTJS_DEMO_APP_NAME}/${{ needs.build.outputs.nextjsDemoAppPackageVersion }}
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/${NEXTJS_DEMO_APP_NAME}/${{ needs.build.outputs.nextjsDemoAppPackageVersion }}
echo Testing Post Page Loading
curl --fail https://${EDGE_DOMAIN}${PREFIX}/${NEXTJS_DEMO_APP_NAME}/${{ needs.build.outputs.nextjsDemoAppPackageVersion }}/posts/pre-rendering
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/${NEXTJS_DEMO_APP_NAME}/${{ needs.build.outputs.nextjsDemoAppPackageVersion }}/posts/pre-rendering
echo Testing Image Rendering
curl --fail -o /dev/null https://${EDGE_DOMAIN}${PREFIX}/${NEXTJS_DEMO_APP_NAME}/${{ needs.build.outputs.nextjsDemoAppPackageVersion }}/_next/image?url=%2Fimages%2Fprofile.jpg&w=384&q=75
curl -H"accept: image/jpg" --fail -o /dev/null https://${EDGE_DOMAIN}${PREFIX}/${NEXTJS_DEMO_APP_NAME}/${{ needs.build.outputs.nextjsDemoAppPackageVersion }}/_next/image?url=%2Fimages%2Fprofile.jpg&w=384&q=75
- name: Publish Release App to MicroApps
run: |
Expand All @@ -290,11 +322,11 @@ jobs:
- name: Test Release App
run: |
echo Testing App Frame Loading
curl --fail https://${EDGE_DOMAIN}${PREFIX}/${RELEASE_APP_NAME}/
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/${RELEASE_APP_NAME}/
echo Testing App HTML Loading
curl --fail https://${EDGE_DOMAIN}${PREFIX}/${RELEASE_APP_NAME}/${{ needs.build.outputs.releaseAppPackageVersion }}
curl -H"accept: text/html" --fail https://${EDGE_DOMAIN}${PREFIX}/${RELEASE_APP_NAME}/${{ needs.build.outputs.releaseAppPackageVersion }}
echo Testing App Method Invocation
curl --fail https://${EDGE_DOMAIN}${PREFIX}/${RELEASE_APP_NAME}/${{ needs.build.outputs.releaseAppPackageVersion }}/api/refresh/${DEMO_APP_NAME}
curl -H"accept: application/json" --fail https://${EDGE_DOMAIN}${PREFIX}/${RELEASE_APP_NAME}/${{ needs.build.outputs.releaseAppPackageVersion }}/api/refresh/${DEMO_APP_NAME}
build-jsii:
if: github.event_name != 'pull_request' || (github.event_name == 'pull_request' && contains( github.event.pull_request.labels.*.name, 'BUILD-JSII'))
Expand Down
10 changes: 8 additions & 2 deletions packages/demo-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ export async function handler(
statusCode: 200,
headers: {
'Content-Type': 'text/html',
'Powered-By': 'demo-app',
},
isBase64Encoded: false,
body: html,
};
}

return {
source: 'demo-app',
timestamp: `${new Date().toUTCString()}`,
statusCode: 404,
headers: {
'Content-Type': 'text/plain',
'Powered-By': 'demo-app',
},
isBase64Encoded: false,
body: `${new Date().toUTCString()} - default failure`,
};
}
17 changes: 14 additions & 3 deletions packages/demo-app/static_files/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@
<div>This is a simple demo app</div>
<div>
<button
onclick="fetch(`/demo-app/0.0.0/serverIncrement?currValue=${document.getElementById('output').innerText}`).then((response) => response.json()).then((data) => {document.getElementById('output').innerText = data.newValue })"
onclick="fetch(`serverIncrement?currValue=${document.getElementById('output').innerText}`).then((response) => response.json()).then((data) => {document.getElementById('output').innerText = data.newValue })"
>
Run Server Increment
Run Server Increment - Relative
</button>

<div id="output">1</div>
</div>

<div>
<button
onclick="fetch(`0.0.0/serverIncrement?currValue=${document.getElementById('output2').innerText}`).then((response) => response.json()).then((data) => {document.getElementById('output2').innerText = data.newValue })"
>
Run Server Increment - Version Relative
</button>

<div id="output2">1</div>
</div>
<div id="output">1</div>
</body>
</html>
28 changes: 26 additions & 2 deletions packages/microapps-cdk/src/MicroAppsCF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,13 @@ export class MicroAppsCF extends Construct implements IMicroAppsCF {
//
if (createAPIPathRoute) {
distro.addBehavior(
posixPath.join(rootPathPrefix, '/*/*/api/*'),
posixPath.join(rootPathPrefix, '*/api/*'),
apiGwyOrigin,
apiGwyBehaviorOptions,
);

distro.addBehavior(
posixPath.join(rootPathPrefix, 'api/*'),
apiGwyOrigin,
apiGwyBehaviorOptions,
);
Expand All @@ -342,7 +348,16 @@ export class MicroAppsCF extends Construct implements IMicroAppsCF {
// Note: send anything with _next/data after the appName (and optional version)
// to the app origin as iframe-less will have no version before _next/data
// in the path
posixPath.join(rootPathPrefix, '/*/_next/data/*'),
posixPath.join(rootPathPrefix, '*/_next/data/*'),
apiGwyOrigin,
apiGwyBehaviorOptions,
);

distro.addBehavior(
// Note: send anything with _next/data after the appName (and optional version)
// to the app origin as iframe-less will have no version before _next/data
// in the path
posixPath.join(rootPathPrefix, '_next/data/*'),
apiGwyOrigin,
apiGwyBehaviorOptions,
);
Expand All @@ -357,6 +372,15 @@ export class MicroAppsCF extends Construct implements IMicroAppsCF {
s3BehaviorOptions,
);

//
// Root app static resources
//
distro.addBehavior(
posixPath.join(rootPathPrefix, '/*.*.*/*.*'),
bucketAppsOrigin,
s3BehaviorOptions,
);

//
// Everything that isn't a static asset is going to API Gateway
// There is no trailing slash because Serverless Next.js wants
Expand Down
19 changes: 18 additions & 1 deletion packages/microapps-deployer/src/controllers/VersionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default class VersionController {
effect: iamCDK.Effect.ALLOW,
actions: ['s3:PutObject', 's3:GetObject', 's3:AbortMultipartUpload'],
resources: [`arn:aws:s3:::${config.filestore.stagingBucket}/*`],
// TODO: Add condition to limit to app prefix
}),
new iamCDK.PolicyStatement({
effect: iamCDK.Effect.ALLOW,
Expand Down Expand Up @@ -791,11 +792,27 @@ export default class VersionController {
} while (list.IsTruncated);
}

/**
* Get the prefix that the app version resources will be stored under
*
* @param request
* @param config
* @returns
*/
private static GetBucketPrefix(
request: Pick<IDeployVersionRequestBase, 'appName' | 'semVer'>,
config: IConfig,
): string {
const pathPrefix = config.rootPathPrefix === '' ? '' : `${config.rootPathPrefix}/`;
return `${pathPrefix}${request.appName}/${request.semVer}`.toLowerCase();
return `${pathPrefix}${VersionController.GetAppNameOrRootTrailingSlash(request)}${
request.semVer
}`.toLowerCase();
}

private static GetAppNameOrRootTrailingSlash(
request: Pick<IDeployVersionRequestBase, 'appName'>,
): string {
const isRootApp = request.appName === '[root]';
return isRootApp ? '' : `${request.appName}/`;
}
}
1 change: 1 addition & 0 deletions packages/microapps-deployer/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Object.defineProperty(Config, 'instance', {
},
uploadRoleName: 'microapps-upload-test-role',
rootPathPrefix: 'qa',
requireIAMAuthorization: false,
};
}),
});
Expand Down
Loading

0 comments on commit afab62e

Please sign in to comment.