Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapter): aws starter adapter #4390

Merged
merged 14 commits into from
Jun 11, 2023
Merged
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions starters/adapters/aws/adapters/aws-lambda/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { nodeServerAdapter } from '@builder.io/qwik-city/adapters/node-server/vite';
import { extendConfig } from '@builder.io/qwik-city/vite';
import baseConfig from '../../vite.config';

export default extendConfig(baseConfig, () => {
return {
build: {
ssr: true,
rollupOptions: {
input: ['src/entry.aws-lambda.tsx', 'src/entry.ssr.tsx', '@qwik-city-plan'],
},
},
plugins: [nodeServerAdapter({ name: 'express' })],
};
});
37 changes: 37 additions & 0 deletions starters/adapters/aws/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require("source-map-support/register");
const express = require("express");
const serverlessExpress = require("@vendia/serverless-express");
let serverlessExpressInstance;

/**
* Some async functions
*/
function asyncTask() {
// console.log(" Connection to database.....");
}

async function setup(event, context) {
//await asyncTask();
const { router, notFound, distDir, buildDir } = await import(
"./server/entry.aws-lambda.mjs"
);
const app = express();
app.use(
`/build`,
express.static(buildDir, { immutable: true, maxAge: "1y" })
);
app.use(express.static(distDir, { redirect: false }));
app.use(router);
app.use(notFound);
serverlessExpressInstance = serverlessExpress({ app });
return serverlessExpressInstance(event, context);
}

function handler(event, context) {
if (serverlessExpressInstance)
return serverlessExpressInstance(event, context);

return setup(event, context);
}

exports.qwikApp = handler;
23 changes: 23 additions & 0 deletions starters/adapters/aws/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"description": "AWS Lambda",
"scripts": {
"build.server": "vite build -c adapters/aws-lambda/vite.config.ts",
"serverless:preview": "serverless offline"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo a deploy and remove cmd would be nice as well 👼 but would require a local aws profile

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not add the serverless:deploy because of that point is necessary AWS profile. I think maybe explain on the docs

},
"devDependencies": {
"@vendia/serverless-express": "^4.10.4",
"source-map-support": "^0.5.21",
"serverless-plugin-qwik": "^1.0.5",
"@types/express": "4.17.17",
"express": "4.18.2"
},
"__qwik__": {
"priority": 30,
"displayName": "Adapter: AWS Lambda",
"docs": [
"https://qwik.builder.io/qwikcity/adaptors/aws-lambda/",
"https://aws.amazon.com/es/lambda/",
"https://www.serverless.com/cloud/docs/get-started"
]
}
}
21 changes: 21 additions & 0 deletions starters/adapters/aws/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
service: aws
frameworkVersion: '3'

provider:
name: aws
runtime: nodejs16.x
leifermendez marked this conversation as resolved.
Show resolved Hide resolved

functions:
hello:
handler: handler.qwikApp
events:
- httpApi: '*'

package:
excludeDevDependencies: false

plugins:
- serverless-offline
- serverless-plugin-qwik
- serverless-plugin-common-excludes
- serverless-plugin-include-dependencies
12 changes: 12 additions & 0 deletions starters/adapters/aws/src/entry.aws-lambda.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createQwikCity } from '@builder.io/qwik-city/middleware/node';
import qwikCityPlan from '@qwik-city-plan';
import { manifest } from '@qwik-client-manifest';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';

import render from './entry.ssr';

export const distDir = join(fileURLToPath(import.meta.url), '..', '..', 'dist');
export const buildDir = join(distDir, 'build');

export const { router, notFound } = createQwikCity({ render, qwikCityPlan, manifest });