Skip to content

Commit

Permalink
feat(@whook/example): add Google Cloud Functions build
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Aug 20, 2023
1 parent 473cc54 commit 889e5f6
Show file tree
Hide file tree
Showing 17 changed files with 2,455 additions and 4,143 deletions.
6,009 changes: 1,870 additions & 4,139 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ DEV_MODE=1
"dependencies": {
"@whook/authorization": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^13.1.0",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
"@whook/http-transaction": "<current_version>",
Expand All @@ -150,6 +151,7 @@ DEV_MODE=1
"openapi-schema-validator": "^12.1.3",
"openapi-types": "^12.1.3",
"pkg-dir": "^7.0.0",
"js-yaml": "^4.1.0",
"strict-qs": "^8.0.1",
"type-fest": "^4.2.0",
"yerror": "^8.0.0",
Expand Down Expand Up @@ -477,6 +479,7 @@ DEV_MODE=1
"dependencies": {
"@whook/authorization": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^13.1.0",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
"@whook/http-transaction": "<current_version>",
Expand All @@ -490,6 +493,7 @@ DEV_MODE=1
"openapi-schema-validator": "^12.1.3",
"openapi-types": "^12.1.3",
"pkg-dir": "^7.0.0",
"js-yaml": "^4.1.0",
"strict-qs": "^8.0.1",
"type-fest": "^4.2.0",
"yerror": "^8.0.0",
Expand Down Expand Up @@ -849,6 +853,7 @@ DEV_MODE=1
"dependencies": {
"@whook/authorization": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^13.1.0",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
"@whook/http-transaction": "<current_version>",
Expand All @@ -862,6 +867,7 @@ DEV_MODE=1
"openapi-schema-validator": "^12.1.3",
"openapi-types": "^12.1.3",
"pkg-dir": "^7.0.0",
"js-yaml": "^4.1.0",
"strict-qs": "^8.0.1",
"type-fest": "^4.2.0",
"yerror": "^8.0.0",
Expand Down Expand Up @@ -1204,6 +1210,7 @@ DEV_MODE=1
"dependencies": {
"@whook/authorization": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^13.1.0",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
"@whook/http-transaction": "<current_version>",
Expand All @@ -1217,6 +1224,7 @@ DEV_MODE=1
"openapi-schema-validator": "^12.1.3",
"openapi-types": "^12.1.3",
"pkg-dir": "^7.0.0",
"js-yaml": "^4.1.0",
"strict-qs": "^8.0.1",
"type-fest": "^4.2.0",
"yerror": "^8.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/whook-create/src/services/createWhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ describe('initCreateWhook', () => {
"dependencies": {
"@whook/authorization": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^13.1.0",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
"@whook/http-transaction": "<current_version>",
Expand All @@ -168,6 +169,7 @@ describe('initCreateWhook', () => {
"application-services": "^4.0.0",
"common-services": "^15.0.0",
"http-auth-utils": "^5.0.1",
"js-yaml": "^4.1.0",
"jwt-service": "^10.0.4",
"knifecycle": "^17.0.1",
"openapi-schema-validator": "^12.1.3",
Expand Down Expand Up @@ -382,6 +384,7 @@ describe('initCreateWhook', () => {
"dependencies": {
"@whook/authorization": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^13.1.0",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
"@whook/http-transaction": "<current_version>",
Expand All @@ -390,6 +393,7 @@ describe('initCreateWhook', () => {
"application-services": "^4.0.0",
"common-services": "^15.0.0",
"http-auth-utils": "^5.0.1",
"js-yaml": "^4.1.0",
"jwt-service": "^10.0.4",
"knifecycle": "^17.0.1",
"openapi-schema-validator": "^12.1.3",
Expand Down Expand Up @@ -589,6 +593,7 @@ describe('initCreateWhook', () => {
"dependencies": {
"@whook/authorization": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^13.1.0",
"@whook/http-router": "<current_version>",
"@whook/http-server": "<current_version>",
"@whook/http-transaction": "<current_version>",
Expand All @@ -597,6 +602,7 @@ describe('initCreateWhook', () => {
"application-services": "^4.0.0",
"common-services": "^15.0.0",
"http-auth-utils": "^5.0.1",
"js-yaml": "^4.1.0",
"jwt-service": "^10.0.4",
"knifecycle": "^17.0.1",
"openapi-schema-validator": "^12.1.3",
Expand Down
58 changes: 58 additions & 0 deletions packages/whook-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,64 @@ Debug `knifecycle` internals (dependency injection issues):
DEBUG=knifecycle npm run dev
```

## Deploying with Google Cloud Functions

Create a project and save its credentials to `.credentials.json`.

Then install Terraform:
```sh
wget https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip
mkdir .bin
unzip -d .bin terraform_0.12.24_linux_amd64.zip
rm terraform_0.12.24_linux_amd64.zip
```

Then initialize the Terraform configuration:
```sh
.bin/terraform init ./terraform;
```

Create a new workspace:
```sh
.bin/terraform workspace new staging
```

Build the functions:
```sh
NODE_ENV=staging npm run build
```

Build the Whook commands Terraform depends on:
```sh
npm run compile
```

Plan the deployment:
```sh
.bin/terraform plan -var="project_id=my-project-1664" \
-out=terraform.plan terraform
```

Apply changes:
```sh
# parallelism may be necessary to avoid hitting
# timeouts with a slow connection
.bin/terraform apply -parallelism=1 terraform.plan
```

Finally retrieve the API URL and enjoy!
```sh
.bin/terraform -var="project_id=my-project-1664" \
output api_url
```

## Testing the GCP Functions

```sh
JWT_SECRET=lol APP_ENV=local npx whook testHTTPFunction --name putEcho \
--parameters '{ "body": { "echo": "Hey!" } }'
```

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

# Authors
Expand Down
2 changes: 2 additions & 0 deletions packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"dependencies": {
"@whook/authorization": "^13.1.0",
"@whook/cors": "^13.1.0",
"@whook/gcp-functions": "^13.1.0",
"@whook/http-router": "^13.1.0",
"@whook/http-server": "^13.1.0",
"@whook/http-transaction": "^13.1.0",
Expand All @@ -94,6 +95,7 @@
"openapi-schema-validator": "^12.1.3",
"openapi-types": "^12.1.3",
"pkg-dir": "^7.0.0",
"js-yaml": "^4.1.0",
"strict-qs": "^8.0.1",
"type-fest": "^4.2.0",
"yerror": "^8.0.0",
Expand Down
7 changes: 6 additions & 1 deletion packages/whook-example/src/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ exports[`commands should work with ls 1`] = `
",
"stdout": "
# Provided by "@whook/example": 1 commands
# Provided by "@whook/example": 2 commands
- printEnv: A command printing every env values
- terraformValues: A command printing functions informations for Terraform
# Provided by "@whook/gcp-functions": 1 commands
- testHTTPFunction: A command for testing GCP HTTP function
# Provided by "@whook/whook": 8 commands
Expand Down
36 changes: 36 additions & 0 deletions packages/whook-example/src/build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, test } from '@jest/globals';
import { exec } from 'child_process';

describe('build should work', () => {
[
['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(
`JWT_SECRET=test npx whook testHTTPFunction --name ${operationId} --parameters '${parameters}'`,
);
});
});
});

async function execCommand(
command,
): Promise<{ stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (err) {
reject(err);
return;
}
resolve({ stdout, stderr });
});
});
}
6 changes: 4 additions & 2 deletions packages/whook-example/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Knifecycle, constant, alsoInject } from 'knifecycle';
import {
DEFAULT_BUILD_INITIALIZER_PATH_MAP,
initBuildConstants,
runBuild as runBaseBuild,
prepareBuildEnvironment as prepareBaseBuildEnvironment,
} from '@whook/whook';
import { prepareEnvironment } from './index.js';
import { packageDirectory } from 'pkg-dir';
import {
runBuild as runBaseBuild,
prepareBuildEnvironment as prepareBaseBuildEnvironment,
} from '@whook/gcp-functions';

/* Architecture Note #1.2: The build file
Expand Down
Loading

0 comments on commit 889e5f6

Please sign in to comment.