Skip to content

Commit

Permalink
feat(@whook/example): add AWS build to @whook/example
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Dec 5, 2024
1 parent a7b84a8 commit 230c0ec
Show file tree
Hide file tree
Showing 19 changed files with 2,683 additions and 4,721 deletions.
6,710 changes: 1,997 additions & 4,713 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ DEV_MODE=1
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@whook/authorization": "<current_version>",
"@whook/aws-lambda": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
Expand Down Expand Up @@ -435,6 +436,7 @@ DEV_MODE=1
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@whook/authorization": "<current_version>",
"@whook/aws-lambda": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
Expand Down Expand Up @@ -780,6 +782,7 @@ DEV_MODE=1
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@whook/authorization": "<current_version>",
"@whook/aws-lambda": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
Expand Down Expand Up @@ -1108,6 +1111,7 @@ DEV_MODE=1
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@whook/authorization": "<current_version>",
"@whook/aws-lambda": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
Expand Down
4 changes: 4 additions & 0 deletions packages/whook-create/src/services/createWhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('initCreateWhook', () => {
dependencies: {
..._packageJSON.dependencies,
'@whook/authorization': '<current_version>',
'@whook/aws-lambda': '<current_version>',
'@whook/cors': '<current_version>',
'@whook/http-router': '<current_version>',
'@whook/http-server': '<current_version>',
Expand Down Expand Up @@ -160,6 +161,7 @@ describe('initCreateWhook', () => {
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@whook/authorization": "<current_version>",
"@whook/aws-lambda": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
Expand Down Expand Up @@ -355,6 +357,7 @@ describe('initCreateWhook', () => {
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@whook/authorization": "<current_version>",
"@whook/aws-lambda": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
Expand Down Expand Up @@ -535,6 +538,7 @@ describe('initCreateWhook', () => {
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@whook/authorization": "<current_version>",
"@whook/aws-lambda": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
Expand Down
25 changes: 25 additions & 0 deletions packages/whook-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ npm run dev -- ls
npx whook ls
```

## Deploying with AWS Lambda

### Pushing to S3

First of all, push the lambdas to S3. If you prefer to use it with your own architecture later, you can stop at that step:

```sh
NODE_ENV=production APP_ENV=staging npm run build
APP_ENV=staging bin/lambdas_zip.sh
APP_ENV=staging bin/lambdas_push.sh
```

Generate API types:

```sh
Expand All @@ -102,6 +114,19 @@ Debug `knifecycle` internals (dependency injection issues):
DEBUG=knifecycle npm run dev
```

Debug built lambdas:

```sh
## HTTP
APP_ENV=staging npx whook testHTTPLambda --name putEcho \
--parameters '{ "body": { "echo": "Hey!" } }'
## Cron
APP_ENV=staging npx whook testCronLambda --name handleMinutes
## Consumer
APP_ENV=staging npx whook testConsumerLambda --name handleMessages \
--event '{ "Records": [{ "test": "test" }] }'
```

[//]: # (::contents:end)

# Authors
Expand Down
13 changes: 13 additions & 0 deletions packages/whook-example/bin/lambda_push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e -o pipefail -u

echo "Sending Signatures..."
aws s3 cp \
--content-type text/plain --recursive \
--exclude "*.zip" --include "*.base64sha256" \
lambdas/$APP_ENV s3://diagrams-lambdas-release-$APP_ENV/;

echo "Sending ZIP files..."
aws s3 cp --recursive --exclude "*.base64sha256" \
--include "*.zip" lambdas/$APP_ENV \
s3://diagrams-lambdas-release-$APP_ENV/
15 changes: 15 additions & 0 deletions packages/whook-example/bin/lambdas_zip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e -o pipefail -u

APP_ENV=${APP_ENV:-local}
FILES=$(ls "builds/$APP_ENV")

rm -rf "lambdas/$APP_ENV"
mkdir -p "lambdas/$APP_ENV"

for f in $FILES
do
echo "Processing $f file..."
zip -jr "lambdas/$APP_ENV/$f.zip" builds/$APP_ENV/$f/*
openssl dgst -sha256 -binary "lambdas/$APP_ENV/$f.zip" | openssl enc -base64 | tr -d "\n" > "lambdas/$APP_ENV/$f.zip.base64sha256"
done
4 changes: 3 additions & 1 deletion packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"testsFiles": "'src/**/*.test.ts'",
"distFiles": "'dist/**/*.js'",
"ignore": [
"dist"
"dist",
"builds"
],
"bundleFiles": [
"bin",
Expand Down Expand Up @@ -80,6 +81,7 @@
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@whook/authorization": "^18.0.0",
"@whook/aws-lambda": "^18.0.1",
"@whook/cors": "^18.0.0",
"@whook/http-router": "^18.0.0",
"@whook/http-server": "^18.0.0",
Expand Down
61 changes: 61 additions & 0 deletions packages/whook-example/src/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`commands should work with config 1`] = `
{
"stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js".
",
"stdout": ""localhost"
",
}
`;

exports[`commands should work with env 1`] = `
{
"stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js".
🔂 - Running with "test" node environment.
🔂 - Running with "local" application environment.
",
"stdout": "test
",
}
`;

exports[`commands should work with ls 1`] = `
{
"stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js".
",
"stdout": "
# Provided by "@whook/example": 2 commands
- printEnv: A command printing every env values
- terraformValues: A command printing lambdas informations for Terraform
# Provided by "@whook/whook": 8 commands
- config: A simple program that returns the queryed config value
- create: A command helping to create new Whook files easily
- env: A command printing env values
- generateOpenAPISchema: Write openAPI schema to stdout
- generateOpenAPITypes: Write openAPI types to stdout
- handler: Runs the given server handler for testing purpose
- inspect: A simple program that returns the result of the injected service
- ls: Print available commands
# Provided by "@whook/cors": none
# Provided by "@whook/authorization": none
# Provided by "@whook/aws-lambda": 7 commands
- testConsumerLambda: A command for testing AWS consumer lambda
- testCronLambda: A command for testing AWS cron lambda
- testHTTPLambda: A command for testing AWS HTTP lambda
- testKafkaConsumerLambda: A command for testing AWS lambda Kafka consumers
- testS3Lambda: A command for testing AWS consumer lambda
- testS3Lambda: A command for testing AWS consumer lambda
- testTransformerLambda: A command for testing AWS lambda transformers
",
}
`;
57 changes: 57 additions & 0 deletions packages/whook-example/src/build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, beforeAll, test } from '@jest/globals';
import { exec } from 'child_process';
import { YError } from 'yerror';

describe('build should work', () => {
let env = 'JWT_SECRET=test ';

beforeAll(async () => {
const { stdout } = await execCommand(
`sed -e 's/^//' .env.${process.env.NODE_ENV} || echo ""`,
);

env += stdout.trim();
});

[
['getPing', '{}'],
['getOpenAPI', '{}'],
[
'getParameters',
'{ "aHeader": "true", "pathParam1":"4", "pathParam2":"a,b,c,d" }',
],
['getTime', '{}'],
['getDelay', '{ "duration": 1 }'],
['putEcho', '{"body": { "echo": "YOLO!" }}'],
].forEach(([operationId, parameters]) => {
test(`with ${operationId} http lambdas`, async () => {
await execCommand(
`${env} npx whook testHTTPLambda --name ${operationId} --parameters '${parameters}'`,
);
});
});

test(`with cron lambdas`, async () => {
await execCommand(`${env} npx whook testCronLambda --name handleMinutes`);
});

test(`with consumer lambdas`, async () => {
await execCommand(
`${env} npx whook testConsumerLambda --name handleMessages --event '{ "Records": [{ "test": "test" }] }'`,
);
});
});

async function execCommand(
command,
): Promise<{ stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (err) {
reject(YError.wrap(err, 'E_COMMAND_FAILURE', stdout, stderr));
return;
}
resolve({ stdout, stderr });
});
});
}
6 changes: 3 additions & 3 deletions packages/whook-example/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Per convention a Whook server build file must export
*/
import { Knifecycle, constant, alsoInject } from 'knifecycle';
import { prepareEnvironment } from './index.js';
import { initBuildConstants } from '@whook/whook';
import {
DEFAULT_BUILD_INITIALIZER_PATH_MAP,
initBuildConstants,
runBuild as runBaseBuild,
prepareBuildEnvironment as prepareBaseBuildEnvironment,
} from '@whook/whook';
} from '@whook/aws-lambda';

/* Architecture Note #1.2.1: The `runBuild` function
Expand All @@ -35,7 +35,7 @@ export async function prepareBuildEnvironment<T extends Knifecycle>(
$ = await prepareEnvironment($);

// Usually, here you call the installed build env
$ = await prepareBaseBuildEnvironment($);
$ = await prepareBaseBuildEnvironment<T>($);

// The build often need to know were initializers
// can be found to create a static build and
Expand Down
10 changes: 10 additions & 0 deletions packages/whook-example/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ On air 🚀🌕
# Provided by "@whook/authorization": none
# Provided by "@whook/aws-lambda": 7 commands
- testConsumerLambda: A command for testing AWS consumer lambda
- testCronLambda: A command for testing AWS cron lambda
- testHTTPLambda: A command for testing AWS HTTP lambda
- testKafkaConsumerLambda: A command for testing AWS lambda Kafka consumers
- testS3Lambda: A command for testing AWS consumer lambda
- testS3Lambda: A command for testing AWS consumer lambda
- testTransformerLambda: A command for testing AWS lambda transformers
",
}
`);
Expand Down
42 changes: 42 additions & 0 deletions packages/whook-example/src/config/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readFileSync } from 'fs';
import { NodeEnv } from 'application-services';
import { DEFAULT_SWAGGER_UI_CONFIG } from '@whook/swagger-ui';
import type { AppConfig } from 'application-services';
import type { JsonObject } from 'type-fest';

/* Architecture Note #2: Configuration
Expand All @@ -16,6 +17,39 @@ The `src/config/common/config.ts` one allows to add common
const _packageJSON = JSON.parse(readFileSync('package.json').toString());
const DEBUG_NODE_ENVS = ['test', 'development', 'staging'];

// Export custom handlers definitions
export type WhookAWSLambdaBaseHTTPConfiguration = {
type: 'http';
};
export type WhookAWSLambdaBaseCronConfiguration<T = JsonObject> = {
type: 'cron';
schedules: {
rule: string;
body?: T;
enabled: boolean;
}[];
};
export type WhookAWSLambdaBaseConsumerConfiguration = {
type: 'consumer';
enabled: boolean;
};
export type WhookAWSLambdaBaseTransformerConfiguration = {
type: 'transformer';
enabled: boolean;
};
export type WhookAWSLambdaBaseKafkaConsumerConfiguration = {
type: 'kafka';
enabled: boolean;
};
export type WhookAWSLambdaBaseLogSubscriberConfiguration = {
type: 'log';
enabled: boolean;
};
export type WhookAWSLambdaBaseS3Configuration = {
type: 's3';
enabled: boolean;
};

/* Architecture Note #2.2: Exporting
Each configuration file then create a configuration object
Expand All @@ -30,8 +64,16 @@ const CONFIG: Omit<AppConfig, 'HOST'> = {
CONFIG: {
name: _packageJSON.name,
description: _packageJSON.description || '',
baseURL: 'https://api.example.com',
},
COMPILER_OPTIONS: {
externalModules: [],
ignoredModules: [],
excludeNodeModules: true,
},
DEBUG_NODE_ENVS: process.env.DEBUG ? Object.keys(NodeEnv) : DEBUG_NODE_ENVS,
BUILD_PARALLELISM: 10,
PROXYED_ENV_VARS: ['APP_ENV', 'NODE_ENV', 'JWT_SECRET'],
ERRORS_DESCRIPTORS: {
...DEFAULT_ERRORS_DESCRIPTORS,
E_INVALID_FAKE_TOKEN: {
Expand Down
Loading

0 comments on commit 230c0ec

Please sign in to comment.