diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8a190ba..2a46790 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -39,7 +39,7 @@ jobs: run: | aws ecr get-login-password --region ap-southeast-2 | docker login --username AWS --password-stdin 045615149555.dkr.ecr.ap-southeast-2.amazonaws.com npm ci - npm run build + npm run build:image env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} diff --git a/.gitignore b/.gitignore index 4e01ea5..f4815f1 100644 --- a/.gitignore +++ b/.gitignore @@ -125,5 +125,4 @@ dist ### Node Patch ### # Serverless Webpack directories .webpack/ -app/**/*.js -app/**/*.d.ts \ No newline at end of file +build \ No newline at end of file diff --git a/.releaserc b/.releaserc index 3ca0f47..1f9d777 100644 --- a/.releaserc +++ b/.releaserc @@ -10,6 +10,7 @@ [ "@semantic-release/exec", { + "prepareCmd": "./prepare.sh ${nextRelease.version} ${options.branch} ${commits.length} ${Date.now()}", "publishCmd": "./publish.sh ${nextRelease.version} ${options.branch} ${commits.length} ${Date.now()}" } ] diff --git a/CHANGELOG.md b/CHANGELOG.md index ddff7fb..5d48c30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,51 @@ +# [1.3.0](https://github.com/phenixcoder/lambda-container-service/compare/v1.2.1...v1.3.0) (2021-11-22) + + +### Bug Fixes + +* **newman:** fixed dev server express mapping ([9be5ee3](https://github.com/phenixcoder/lambda-container-service/commit/9be5ee3804eeec84316f36011d3c39e611d9e07c)) + + +### Features + +* **logger:** added improved logger ([2530e38](https://github.com/phenixcoder/lambda-container-service/commit/2530e3832e5ad53bf0b7e76bb26d6c574720f8a1)) + +# [1.3.0](https://github.com/phenixcoder/lambda-container-service/compare/v1.2.1...v1.3.0) (2021-11-22) + + +### Bug Fixes + +* **newman:** fixed dev server express mapping ([9be5ee3](https://github.com/phenixcoder/lambda-container-service/commit/9be5ee3804eeec84316f36011d3c39e611d9e07c)) + + +### Features + +* **logger:** added improved logger ([2530e38](https://github.com/phenixcoder/lambda-container-service/commit/2530e3832e5ad53bf0b7e76bb26d6c574720f8a1)) + +# [1.3.0](https://github.com/phenixcoder/lambda-container-service/compare/v1.2.1...v1.3.0) (2021-11-22) + + +### Bug Fixes + +* **newman:** fixed dev server express mapping ([9be5ee3](https://github.com/phenixcoder/lambda-container-service/commit/9be5ee3804eeec84316f36011d3c39e611d9e07c)) + + +### Features + +* **logger:** added improved logger ([2530e38](https://github.com/phenixcoder/lambda-container-service/commit/2530e3832e5ad53bf0b7e76bb26d6c574720f8a1)) + +# [1.3.0](https://github.com/phenixcoder/lambda-container-service/compare/v1.2.1...v1.3.0) (2021-11-22) + + +### Bug Fixes + +* **newman:** fixed dev server express mapping ([9be5ee3](https://github.com/phenixcoder/lambda-container-service/commit/9be5ee3804eeec84316f36011d3c39e611d9e07c)) + + +### Features + +* **logger:** added improved logger ([2530e38](https://github.com/phenixcoder/lambda-container-service/commit/2530e3832e5ad53bf0b7e76bb26d6c574720f8a1)) + ## [1.2.1](https://github.com/phenixcoder/lambda-container-service/compare/v1.2.0...v1.2.1) (2021-11-14) diff --git a/Dockerfile b/Dockerfile index 67b2936..b7bfad0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ FROM public.ecr.aws/lambda/nodejs:14 -COPY . ${LAMBDA_TASK_ROOT} - +COPY ./build ${LAMBDA_TASK_ROOT} +WORKDIR ${LAMBDA_TASK_ROOT} +RUN pwd && ls -la RUN npm ci --only=production # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) diff --git a/dev-server.js b/dev-server.js index 81b62a8..da350c9 100644 --- a/dev-server.js +++ b/dev-server.js @@ -1,6 +1,6 @@ const express = require('express') const { existsSync } = require('fs') -const { handler } = require('./app/index') +const { handler } = require('./build/src/index') const newman = require('newman') const app = express(); @@ -10,7 +10,7 @@ let server = null; app.post('/2015-03-31/functions/function/invocations', async (req, res) => { try { - const body = req.body ? JSON.parse(req.body) : req; + const body = req.body ? JSON.parse(req.body) : ExpressToAWSAPIGatewayProxyEventV2(req); handler(body, {}).then(result => { res.send(result); }).catch(error => console.error(error)); @@ -20,7 +20,12 @@ app.post('/2015-03-31/functions/function/invocations', async (req, res) => { }) app.all('/api', async (req, res) => { - handler(req, {}).then(result => { + handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => { + res.send(result); + }).catch(error => console.error(error)); +}); +app.all('/__health', async (req, res) => { + handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => { res.send(result); }).catch(error => console.error(error)); }); @@ -34,8 +39,8 @@ server = app.listen(port, () => { if (process.argv[2]) { const testFilename = process.argv[3] || 'service-collection.postman_collection.json' - console.log('Running Newman tests'); if (!existsSync(`./${testFilename}`)) { + console.log('Running Newman tests'); console.error(`Error: test collection not found ./${testFilename} @@ -58,4 +63,86 @@ server = app.listen(port, () => { process.exit(); }) } -}) \ No newline at end of file +}) + +function ExpressToAWSAPIGatewayProxyEventV2(request) { + return { + "resource": request.path, + "path": request.path, + "httpMethod": request.method, + "headers": { + "header1": "value1", + "header2": "value2" + }, + "multiValueHeaders": { + "header1": [ + "value1" + ], + "header2": [ + "value1", + "value2" + ] + }, + "queryStringParameters": { + "parameter1": "value1", + "parameter2": "value" + }, + "multiValueQueryStringParameters": { + "parameter1": [ + "value1", + "value2" + ], + "parameter2": [ + "value" + ] + }, + "requestContext": { + "accountId": "123456789012", + "apiId": "id", + "authorizer": { + "claims": null, + "scopes": null + }, + "domainName": "id.execute-api.us-east-1.amazonaws.com", + "domainPrefix": "id", + "extendedRequestId": "request-id", + "httpMethod": "GET", + "identity": { + "accessKey": null, + "accountId": null, + "caller": null, + "cognitoAuthenticationProvider": null, + "cognitoAuthenticationType": null, + "cognitoIdentityId": null, + "cognitoIdentityPoolId": null, + "principalOrgId": null, + "sourceIp": "IP", + "user": null, + "userAgent": "user-agent", + "userArn": null, + "clientCert": { + "clientCertPem": "CERT_CONTENT", + "subjectDN": "www.example.com", + "issuerDN": "Example issuer", + "serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1", + "validity": { + "notBefore": "May 28 12:30:02 2019 GMT", + "notAfter": "Aug 5 09:36:04 2021 GMT" + } + } + }, + "path": "/my/path", + "protocol": "HTTP/1.1", + "requestId": "id=", + "requestTime": "04/Mar/2020:19:15:17 +0000", + "requestTimeEpoch": 1583349317135, + "resourceId": null, + "resourcePath": "/my/path", + "stage": "$default" + }, + "pathParameters": null, + "stageVariables": null, + "body": "Hello from Lambda!", + "isBase64Encoded": false + } +} \ No newline at end of file diff --git a/package.json b/package.json index 240b4e2..4c4f17a 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "scripts": { "start": "tsc --watch --preserveWatchOutput & nodemon dev-server.js -e js -w ./app", "build": "tsc", - "test": "find app -wholename '*.d.ts' -delete && find app -wholename '*.js' -delete && jest", + "build:image": "./scripts/gen-build", + "test": "jest", "test:watch": "npm t -- --watch", "test:newman": "npm run build && node dev-server.js --test", "lint": "tslint --fix -p ./tsconfig.json", @@ -33,7 +34,7 @@ }, "husky": { "hooks": { - "pre-commit": "tsdx lint" + "pre-commit": "npm run lint && npm run prettier" } }, "prettier": { diff --git a/prepare.sh b/prepare.sh new file mode 100755 index 0000000..94423bf --- /dev/null +++ b/prepare.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# export LAMBDA_TASK_ROOT=/var/task + +npm version $1 --no-git-tag-version +docker build --no-cache -t lambda-container-service . \ No newline at end of file diff --git a/publish.sh b/publish.sh index 792b3d5..4337c0b 100755 --- a/publish.sh +++ b/publish.sh @@ -2,8 +2,6 @@ REPO=045615149555.dkr.ecr.ap-southeast-2.amazonaws.com/lambda-container-service -npm version $1 --no-git-tag-version -docker build -t lambda-container-service . docker tag lambda-container-service:latest $REPO:$1 docker push $REPO:$1 docker tag lambda-container-service:latest $REPO:latest diff --git a/scripts/gen-build b/scripts/gen-build new file mode 100755 index 0000000..3fe1651 --- /dev/null +++ b/scripts/gen-build @@ -0,0 +1,11 @@ +#!/bin/bash + +rm -rf ./build +npm run build +cp ./package-lock.json ./build +cd ./build +mv src app +../scripts/gen-prod-package-json package.json package.json +find . -wholename '*.d.ts' -delete +find . -wholename '*.test.js' -delete +cd .. \ No newline at end of file diff --git a/scripts/gen-prod-package-json b/scripts/gen-prod-package-json new file mode 100755 index 0000000..d01f2d2 --- /dev/null +++ b/scripts/gen-prod-package-json @@ -0,0 +1,22 @@ +#!/bin/env node + +const { readFileSync, writeFileSync } = require("fs"); + +const packageString = readFileSync(process.argv[2]); + +const { name, version, dependencies, engines, keywords, repository } = JSON.parse(packageString); + +const prodJSON = { + name, + version, + dependencies, + engines, + keywords, + repository +}; + + +console.log(prodJSON); +if (process.argv[3]) { + writeFileSync(process.argv[3], JSON.stringify(prodJSON, null, ' ')); +} \ No newline at end of file diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..05a7907 --- /dev/null +++ b/setup.sh @@ -0,0 +1,2 @@ +#!/bin/bash + diff --git a/app/index.test.ts b/src/index.test.ts similarity index 96% rename from app/index.test.ts rename to src/index.test.ts index 2602300..39afcde 100644 --- a/app/index.test.ts +++ b/src/index.test.ts @@ -90,8 +90,10 @@ describe('Index Handler', () => { const response = await handler(mockRequest(), mockContext()); expect(response).toStrictEqual({ body: JSON.stringify({ - hello: 'world', - foo: 'bar', + version, + name, + event: mockRequest(), + context: mockContext(), }), headers: { 'content-type': 'text/json', diff --git a/app/index.ts b/src/index.ts similarity index 89% rename from app/index.ts rename to src/index.ts index 5635233..1e6a8c9 100644 --- a/app/index.ts +++ b/src/index.ts @@ -30,8 +30,10 @@ export const handler = async ( 'content-type': 'text/json', }, body: JSON.stringify({ - hello: 'world', - foo: 'bar', + version: pkg.version, + name: pkg.name, + event, + context: _context, }), }; }; diff --git a/app/lib/logger.test.ts b/src/lib/logger.test.ts similarity index 100% rename from app/lib/logger.test.ts rename to src/lib/logger.test.ts diff --git a/app/lib/logger.ts b/src/lib/logger.ts similarity index 93% rename from app/lib/logger.ts rename to src/lib/logger.ts index b6e72e6..f391f19 100644 --- a/app/lib/logger.ts +++ b/src/lib/logger.ts @@ -32,9 +32,7 @@ const format = winston.format.combine( ) ); -const transports = [ - new winston.transports.Console() -]; +const transports = [new winston.transports.Console()]; const Logger = winston.createLogger({ level: level(), diff --git a/tsconfig.json b/tsconfig.json index 09808f3..caddb77 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,8 +11,8 @@ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - // "outDir": "app", /* Redirect output structure to the directory. */ - // "rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + "outDir": "build", /* Redirect output structure to the directory. */ + // "rootDir": ".", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "removeComments": true, /* Do not emit comments to output. */ // "noEmit": true, /* Do not emit outputs. */