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 Sep 26, 2021
1 parent da55737 commit 4c537fe
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": "^8.4.2",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.1",
"http-auth-utils": "^3.0.1",
"js-yaml": "^3.13.1",
"jwt-service": "^7.1.3",
"knifecycle": "^11.1.1",
"strict-qs": "^6.1.4",
Expand Down Expand Up @@ -299,7 +301,7 @@ Mr Bean
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"architecture": "jsarch 'src/**/*.ts' > ARCHITECTURE.md && git add ARCHITECTURE.md",
"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 @@ -310,7 +312,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 @@ -444,12 +446,14 @@ Mr Bean
"@whook/authorization": "<current_version>",
"@whook/cli": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^8.4.2",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.1",
"http-auth-utils": "^3.0.1",
"js-yaml": "^3.13.1",
"jwt-service": "^7.1.3",
"knifecycle": "^11.1.1",
"strict-qs": "^6.1.4",
Expand Down Expand Up @@ -557,7 +561,7 @@ Mr Bean
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"architecture": "jsarch 'src/**/*.ts' > ARCHITECTURE.md && git add ARCHITECTURE.md",
"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 @@ -568,7 +572,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 @@ -687,12 +691,14 @@ Mr Bean
"@whook/authorization": "<current_version>",
"@whook/cli": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/gcp-functions": "^8.4.2",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.1",
"http-auth-utils": "^3.0.1",
"js-yaml": "^3.13.1",
"jwt-service": "^7.1.3",
"knifecycle": "^11.1.1",
"strict-qs": "^6.1.4",
Expand Down Expand Up @@ -800,7 +806,7 @@ Mr Bean
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"architecture": "jsarch 'src/**/*.ts' > ARCHITECTURE.md && git add ARCHITECTURE.md",
"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 @@ -811,7 +817,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 @@ -93,6 +93,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 @@ -45,7 +45,7 @@
"scripts": {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"architecture": "jsarch 'src/**/*.ts' > ARCHITECTURE.md && git add ARCHITECTURE.md",
"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 @@ -58,7 +58,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 @@ -83,6 +83,7 @@
"@whook/authorization": "^8.4.2",
"@whook/cli": "^8.4.2",
"@whook/cors": "^8.4.2",
"@whook/gcp-functions": "^8.4.2",
"@whook/http-router": "^8.4.2",
"@whook/http-transaction": "^8.3.0",
"@whook/swagger-ui": "^8.4.2",
Expand All @@ -91,6 +92,7 @@
"http-auth-utils": "^3.0.1",
"jwt-service": "^7.1.3",
"knifecycle": "^11.1.1",
"js-yaml": "^3.13.1",
"strict-qs": "^6.1.4",
"type-fest": "^2.3.3",
"yerror": "^6.0.1",
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 4c537fe

Please sign in to comment.