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 Mar 3, 2021
1 parent bdfcd2f commit 95a039a
Show file tree
Hide file tree
Showing 14 changed files with 629 additions and 27 deletions.

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions packages/whook-create/src/services/createWhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,14 @@ Mr Bean
"@whook/authorization": "<current_version>",
"@whook/cli": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^7.1.5",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.0",
"http-auth-utils": "^3.0.0",
"js-yaml": "^3.13.1",
"jwt-service": "^7.1.0",
"knifecycle": "^11.1.0",
"strict-qs": "^6.1.3",
Expand Down Expand Up @@ -297,7 +299,7 @@ Mr Bean
"private": true,
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"build": "npm run compile && npm run types && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
"compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
"compile:mjs": "babel --env-name=mjs --out-file-extension=.mjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
Expand All @@ -308,7 +310,7 @@ Mr Bean
"lint": "eslint 'src/**/*.ts'",
"prettier": "prettier --write 'src/**/*.ts'",
"start": "NODE_ENV=\${NODE_ENV:-development} node bin/start",
"test": "npm run jest",
"test": "NODE_ENV=test npm run build && npm run jest",
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist",
"watch": "NODE_ENV=\${NODE_ENV:-development} DEV_MODE=1 DESTROY_SOCKETS=1 babel-node --extensions '.ts,.js' bin/watch",
"whook": "NODE_ENV=\${NODE_ENV:-development} whook",
Expand Down Expand Up @@ -441,12 +443,14 @@ Mr Bean
"@whook/authorization": "<current_version>",
"@whook/cli": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^7.1.5",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.0",
"http-auth-utils": "^3.0.0",
"js-yaml": "^3.13.1",
"jwt-service": "^7.1.0",
"knifecycle": "^11.1.0",
"strict-qs": "^6.1.3",
Expand Down Expand Up @@ -552,7 +556,7 @@ Mr Bean
"private": true,
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"build": "npm run compile && npm run types && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
"compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
"compile:mjs": "babel --env-name=mjs --out-file-extension=.mjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
Expand All @@ -563,7 +567,7 @@ Mr Bean
"lint": "eslint 'src/**/*.ts'",
"prettier": "prettier --write 'src/**/*.ts'",
"start": "NODE_ENV=\${NODE_ENV:-development} node bin/start",
"test": "npm run jest",
"test": "NODE_ENV=test npm run build && npm run jest",
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist",
"watch": "NODE_ENV=\${NODE_ENV:-development} DEV_MODE=1 DESTROY_SOCKETS=1 babel-node --extensions '.ts,.js' bin/watch",
"whook": "NODE_ENV=\${NODE_ENV:-development} whook",
Expand Down Expand Up @@ -681,12 +685,14 @@ Mr Bean
"@whook/authorization": "<current_version>",
"@whook/cli": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^7.1.5",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.0",
"http-auth-utils": "^3.0.0",
"js-yaml": "^3.13.1",
"jwt-service": "^7.1.0",
"knifecycle": "^11.1.0",
"strict-qs": "^6.1.3",
Expand Down Expand Up @@ -792,7 +798,7 @@ Mr Bean
"private": true,
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"build": "npm run compile && npm run types && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
"compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
"compile:mjs": "babel --env-name=mjs --out-file-extension=.mjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
Expand All @@ -803,7 +809,7 @@ Mr Bean
"lint": "eslint 'src/**/*.ts'",
"prettier": "prettier --write 'src/**/*.ts'",
"start": "NODE_ENV=\${NODE_ENV:-development} node bin/start",
"test": "npm run jest",
"test": "NODE_ENV=test npm run build && npm run jest",
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist",
"watch": "NODE_ENV=\${NODE_ENV:-development} DEV_MODE=1 DESTROY_SOCKETS=1 babel-node --extensions '.ts,.js' bin/watch",
"whook": "NODE_ENV=\${NODE_ENV:-development} whook",
Expand Down
51 changes: 51 additions & 0 deletions packages/whook-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,57 @@ 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
```

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

# Authors
Expand Down
6 changes: 4 additions & 2 deletions packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"scripts": {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=${NODE_ENV:-development} node bin/build",
"build": "npm run compile && npm run types && NODE_ENV=${NODE_ENV:-development} node bin/build",
"cli": "env NODE_ENV=${NODE_ENV:-cli}",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
"compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
Expand All @@ -56,7 +56,7 @@
"metapak": "metapak",
"prettier": "prettier --write 'src/**/*.ts'",
"start": "NODE_ENV=${NODE_ENV:-development} node bin/start",
"test": "npm run jest",
"test": "NODE_ENV=test npm run build && npm run jest",
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist",
"watch": "NODE_ENV=${NODE_ENV:-development} DEV_MODE=1 DESTROY_SOCKETS=1 babel-node --extensions '.ts,.js' bin/watch",
"whook": "NODE_ENV=${NODE_ENV:-development} whook",
Expand All @@ -80,6 +80,7 @@
"@whook/authorization": "^7.1.5",
"@whook/cli": "^7.1.5",
"@whook/cors": "^7.1.5",
"@whook/gcp-functions": "^7.1.5",
"@whook/http-router": "^7.1.5",
"@whook/http-transaction": "^7.1.3",
"@whook/swagger-ui": "^7.1.5",
Expand All @@ -88,6 +89,7 @@
"http-auth-utils": "^3.0.0",
"jwt-service": "^7.1.0",
"knifecycle": "^11.1.0",
"js-yaml": "^3.13.1",
"strict-qs": "^6.1.3",
"type-fest": "^0.21.2",
"yerror": "^5.0.0",
Expand Down
13 changes: 9 additions & 4 deletions packages/whook-example/src/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ Object {
",
"stdout": "
# Provided by \\"@whook/example\\": 3 commands
# Provided by \\"@whook/example\\": 4 commands
- generateOpenAPISchema: Write openAPI schema to stdout
- generateOpenAPITypes: Write openAPI types to stdout
- printEnv: A command printing every env values
- terraformValues: A command printing functions informations for Terraform
# Provided by \\"@whook/gcp-functions\\": 1 commands
- testHTTPLambda: A command for testing AWS HTTP lambda
# Provided by \\"@whook/cors\\": none
# Provided by \\"@whook/cli\\": 6 commands
Expand All @@ -44,9 +52,6 @@ Object {
# Provided by \\"@whook/whook\\": none
# Provided by \\"@whook/cors\\": none
",
}
`;
Loading

0 comments on commit 95a039a

Please sign in to comment.