Skip to content

Commit 787910a

Browse files
committed
feat(@whook/example): add Google Cloud Functions build
1 parent b8be8e7 commit 787910a

File tree

18 files changed

+779
-14
lines changed

18 files changed

+779
-14
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/whook-create/src/services/__snapshots__/createWhook.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ DEV_MODE=1
129129
"@types/node-cron": "^3.0.11",
130130
"@whook/authorization": "<current_version>",
131131
"@whook/cors": "<current_version>",
132+
"@whook/gcp-functions": "^18.1.0",
132133
"@whook/swagger-ui": "<current_version>",
133134
"@whook/whook": "<current_version>",
134135
"application-services": "^7.0.2",
@@ -140,6 +141,7 @@ DEV_MODE=1
140141
"ya-json-schema-types": "^1.0.1",
141142
"ya-open-api-types": "^1.2.0",
142143
"pkg-dir": "^8.0.0",
144+
"js-yaml": "^4.1.0",
143145
"strict-qs": "^8.0.3",
144146
"type-fest": "^4.33.0",
145147
"yerror": "^8.0.0",
@@ -437,6 +439,7 @@ DEV_MODE=1
437439
"@types/node-cron": "^3.0.11",
438440
"@whook/authorization": "<current_version>",
439441
"@whook/cors": "<current_version>",
442+
"@whook/gcp-functions": "^18.1.0",
440443
"@whook/swagger-ui": "<current_version>",
441444
"@whook/whook": "<current_version>",
442445
"application-services": "^7.0.2",
@@ -448,6 +451,7 @@ DEV_MODE=1
448451
"ya-json-schema-types": "^1.0.1",
449452
"ya-open-api-types": "^1.2.0",
450453
"pkg-dir": "^8.0.0",
454+
"js-yaml": "^4.1.0",
451455
"strict-qs": "^8.0.3",
452456
"type-fest": "^4.33.0",
453457
"yerror": "^8.0.0",
@@ -783,6 +787,7 @@ DEV_MODE=1
783787
"@types/node-cron": "^3.0.11",
784788
"@whook/authorization": "<current_version>",
785789
"@whook/cors": "<current_version>",
790+
"@whook/gcp-functions": "^18.1.0",
786791
"@whook/swagger-ui": "<current_version>",
787792
"@whook/whook": "<current_version>",
788793
"application-services": "^7.0.2",
@@ -794,6 +799,7 @@ DEV_MODE=1
794799
"ya-json-schema-types": "^1.0.1",
795800
"ya-open-api-types": "^1.2.0",
796801
"pkg-dir": "^8.0.0",
802+
"js-yaml": "^4.1.0",
797803
"strict-qs": "^8.0.3",
798804
"type-fest": "^4.33.0",
799805
"yerror": "^8.0.0",
@@ -1112,6 +1118,7 @@ DEV_MODE=1
11121118
"@types/node-cron": "^3.0.11",
11131119
"@whook/authorization": "<current_version>",
11141120
"@whook/cors": "<current_version>",
1121+
"@whook/gcp-functions": "^18.1.0",
11151122
"@whook/swagger-ui": "<current_version>",
11161123
"@whook/whook": "<current_version>",
11171124
"application-services": "^7.0.2",
@@ -1123,6 +1130,7 @@ DEV_MODE=1
11231130
"ya-json-schema-types": "^1.0.1",
11241131
"ya-open-api-types": "^1.2.0",
11251132
"pkg-dir": "^8.0.0",
1133+
"js-yaml": "^4.1.0",
11261134
"strict-qs": "^8.0.3",
11271135
"type-fest": "^4.33.0",
11281136
"yerror": "^8.0.0",

packages/whook-create/src/services/createWhook.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ describe('initCreateWhook', () => {
161161
"@types/node-cron": "^3.0.11",
162162
"@whook/authorization": "<current_version>",
163163
"@whook/cors": "<current_version>",
164+
"@whook/gcp-functions": "^18.1.0",
164165
"@whook/http-router": "<current_version>",
165166
"@whook/http-server": "<current_version>",
166167
"@whook/http-transaction": "<current_version>",
@@ -169,6 +170,7 @@ describe('initCreateWhook', () => {
169170
"application-services": "^7.0.2",
170171
"common-services": "^17.0.1",
171172
"http-auth-utils": "^6.0.1",
173+
"js-yaml": "^4.1.0",
172174
"jwt-service": "^11.0.2",
173175
"knifecycle": "^19.0.0",
174176
"node-cron": "^3.0.3",
@@ -357,6 +359,7 @@ describe('initCreateWhook', () => {
357359
"@types/node-cron": "^3.0.11",
358360
"@whook/authorization": "<current_version>",
359361
"@whook/cors": "<current_version>",
362+
"@whook/gcp-functions": "^18.1.0",
360363
"@whook/http-router": "<current_version>",
361364
"@whook/http-server": "<current_version>",
362365
"@whook/http-transaction": "<current_version>",
@@ -365,6 +368,7 @@ describe('initCreateWhook', () => {
365368
"application-services": "^7.0.2",
366369
"common-services": "^17.0.1",
367370
"http-auth-utils": "^6.0.1",
371+
"js-yaml": "^4.1.0",
368372
"jwt-service": "^11.0.2",
369373
"knifecycle": "^19.0.0",
370374
"node-cron": "^3.0.3",
@@ -538,6 +542,7 @@ describe('initCreateWhook', () => {
538542
"@types/node-cron": "^3.0.11",
539543
"@whook/authorization": "<current_version>",
540544
"@whook/cors": "<current_version>",
545+
"@whook/gcp-functions": "^18.1.0",
541546
"@whook/http-router": "<current_version>",
542547
"@whook/http-server": "<current_version>",
543548
"@whook/http-transaction": "<current_version>",
@@ -546,6 +551,7 @@ describe('initCreateWhook', () => {
546551
"application-services": "^7.0.2",
547552
"common-services": "^17.0.1",
548553
"http-auth-utils": "^6.0.1",
554+
"js-yaml": "^4.1.0",
549555
"jwt-service": "^11.0.2",
550556
"knifecycle": "^19.0.0",
551557
"node-cron": "^3.0.3",

packages/whook-example/ARCHITECTURE.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Per convention a Whook server build file must export
259259
The `runBuild` function is intended to build the
260260
project.
261261

262-
[See in context](./src/build.ts#L15-L19)
262+
[See in context](./src/build.ts#L17-L21)
263263

264264

265265

@@ -268,7 +268,7 @@ The `runBuild` function is intended to build the
268268
The `prepareBuildEnvironment` create the build
269269
environment
270270

271-
[See in context](./src/build.ts#L27-L31)
271+
[See in context](./src/build.ts#L29-L33)
272272

273273

274274

@@ -288,15 +288,15 @@ The `src/config/common/config.ts` one allows to add common
288288

289289
Whook provides several types you may extend here.
290290

291-
[See in context](./src/whook.d.ts#L26-L29)
291+
[See in context](./src/whook.d.ts#L31-L34)
292292

293293

294294

295295
#### 2.1.1. AppEnvVars
296296

297297
The process environment can be typed by extending this type.
298298

299-
[See in context](./src/whook.d.ts#L32-L35)
299+
[See in context](./src/whook.d.ts#L37-L40)
300300

301301

302302

@@ -305,7 +305,7 @@ The process environment can be typed by extending this type.
305305
The configuration is typed so that you are sure you cannot
306306
produce a bad configuration for your API.
307307

308-
[See in context](./src/whook.d.ts#L43-L47)
308+
[See in context](./src/whook.d.ts#L48-L52)
309309

310310

311311

@@ -315,15 +315,15 @@ Here we export a custom API handler config type in order
315315
to allow using the various plugins installed that deal
316316
with the routes.
317317

318-
[See in context](./src/whook.d.ts#L59-L64)
318+
[See in context](./src/whook.d.ts#L67-L72)
319319

320320

321321

322322
#### 2.1.3. WhookMain
323323

324324
Here we export a main config to type AppEnv.
325325

326-
[See in context](./src/whook.d.ts#L70-L73)
326+
[See in context](./src/whook.d.ts#L79-L82)
327327

328328

329329

@@ -534,7 +534,7 @@ Here we export a custom API handler config type in order
534534
to allow using the various plugins installed that deal
535535
with the routes.
536536

537-
[See in context](./src/whook.d.ts#L81-L86)
537+
[See in context](./src/whook.d.ts#L90-L95)
538538

539539

540540

packages/whook-example/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,64 @@ Debug `knifecycle` internals (dependency injection issues):
102102
DEBUG=knifecycle npm run dev
103103
```
104104

105+
## Deploying with Google Cloud Functions
106+
107+
Create a project and save its credentials to `.credentials.json`.
108+
109+
Then install Terraform:
110+
```sh
111+
wget https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip
112+
mkdir .bin
113+
unzip -d .bin terraform_0.12.24_linux_amd64.zip
114+
rm terraform_0.12.24_linux_amd64.zip
115+
```
116+
117+
Then initialize the Terraform configuration:
118+
```sh
119+
.bin/terraform init ./terraform;
120+
```
121+
122+
Create a new workspace:
123+
```sh
124+
.bin/terraform workspace new staging
125+
```
126+
127+
Build the functions:
128+
```sh
129+
NODE_ENV=staging npm run build
130+
```
131+
132+
Build the Whook commands Terraform depends on:
133+
```sh
134+
npm run compile
135+
```
136+
137+
Plan the deployment:
138+
```sh
139+
.bin/terraform plan -var="project_id=my-project-1664" \
140+
-out=terraform.plan terraform
141+
```
142+
143+
Apply changes:
144+
```sh
145+
# parallelism may be necessary to avoid hitting
146+
# timeouts with a slow connection
147+
.bin/terraform apply -parallelism=1 terraform.plan
148+
```
149+
150+
Finally retrieve the API URL and enjoy!
151+
```sh
152+
.bin/terraform -var="project_id=my-project-1664" \
153+
output api_url
154+
```
155+
156+
## Testing the GCP Functions
157+
158+
```sh
159+
JWT_SECRET=lol APP_ENV=local npm run dev -- testGCPFunctionRoute \
160+
--name putEcho --parameters '{ "body": { "echo": "Hey!" } }'
161+
```
162+
105163
[//]: # (::contents:end)
106164

107165
# Authors

packages/whook-example/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"@types/node-cron": "^3.0.11",
8282
"@whook/authorization": "^18.1.0",
8383
"@whook/cors": "^18.1.0",
84+
"@whook/gcp-functions": "^18.1.0",
8485
"@whook/swagger-ui": "^18.1.0",
8586
"@whook/whook": "^18.1.0",
8687
"application-services": "^7.0.2",
@@ -92,6 +93,7 @@
9293
"ya-json-schema-types": "^1.0.1",
9394
"ya-open-api-types": "^1.2.0",
9495
"pkg-dir": "^8.0.0",
96+
"js-yaml": "^4.1.0",
9597
"strict-qs": "^8.0.3",
9698
"type-fest": "^4.33.0",
9799
"yerror": "^8.0.0",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, beforeAll, test } from '@jest/globals';
2+
import { exec } from 'child_process';
3+
import { YError } from 'yerror';
4+
5+
describe('build should work', () => {
6+
let env = 'JWT_SECRET=test ';
7+
8+
beforeAll(async () => {
9+
const { stdout } = await execCommand(
10+
`sed -e 's/^//' .env.${process.env.NODE_ENV} || echo ""`,
11+
);
12+
13+
env += stdout.trim();
14+
});
15+
16+
[
17+
['getPing', '{}'],
18+
['getOpenAPI', '{}'],
19+
[
20+
'getParameters',
21+
'{ "aHeader": "true", "pathParam1":"4", "pathParam2":"a,b,c,d" }',
22+
],
23+
['getTime', '{}'],
24+
['getDelay', '{ "duration": 1 }'],
25+
['putEcho', '{"body": { "echo": "YOLO!" }}'],
26+
].forEach(([operationId, parameters]) => {
27+
test(`with ${operationId} route functions`, async () => {
28+
await execCommand(
29+
`${env} npx whook testGCPFunctionRoute --name ${operationId} --parameters '${parameters}'`,
30+
);
31+
});
32+
});
33+
});
34+
35+
async function execCommand(
36+
command,
37+
): Promise<{ stdout: string; stderr: string }> {
38+
return new Promise((resolve, reject) => {
39+
exec(command, (err, stdout, stderr) => {
40+
if (err) {
41+
reject(YError.wrap(err, 'E_COMMAND_FAILURE', stdout, stderr));
42+
return;
43+
}
44+
resolve({ stdout, stderr });
45+
});
46+
});
47+
}

packages/whook-example/src/build.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import { prepareEnvironment } from './index.js';
88
import {
99
DEFAULT_BUILD_INITIALIZER_PATH_MAP,
1010
initBuildConstants,
11+
} from '@whook/whook';
12+
import {
1113
runBuild as runBaseBuild,
1214
prepareBuildEnvironment as prepareBaseBuildEnvironment,
13-
} from '@whook/whook';
15+
} from '@whook/gcp-functions';
1416

1517
/* Architecture Note #1.2.1: The `runBuild` function
1618
@@ -56,7 +58,7 @@ export async function prepareBuildEnvironment<T extends Knifecycle>(
5658

5759
// Finally, some constants can be serialized instead of being
5860
// initialized in the target build saving some time at boot
59-
$.register(alsoInject([], initBuildConstants));
61+
$.register(alsoInject(['DEFINITIONS'], initBuildConstants));
6062

6163
return $;
6264
}

packages/whook-example/src/cli.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ On air 🚀🌕
2323
",
2424
"stdout": "
2525
26-
# Provided by "@whook/example": 1 commands
26+
# Provided by "@whook/example": 2 commands
2727
- printEnv: A command printing every env values
28+
- terraformValues: A command printing functions informations for Terraform
2829
2930
3031
# Provided by "@whook/whook": 7 commands
@@ -37,6 +38,10 @@ On air 🚀🌕
3738
- route: Runs the given server route for testing purpose
3839
3940
41+
# Provided by "@whook/gcp-functions": 1 commands
42+
- testGCPFunctionRoute: A command for testing a GCP function route
43+
44+
4045
# Provided by "@whook/cors": none
4146
4247

0 commit comments

Comments
 (0)