Skip to content

Commit

Permalink
Merge pull request #65 from nfroidure/feat/cloud_functions
Browse files Browse the repository at this point in the history
feat(@whook/gcp-functions): add Google Cloud Functions build
  • Loading branch information
nfroidure authored Mar 30, 2020
2 parents f36f9c1 + 1525f92 commit cd36ba4
Show file tree
Hide file tree
Showing 20 changed files with 1,961 additions and 168 deletions.
54 changes: 0 additions & 54 deletions packages/whook-aws-lambda/API.md
Original file line number Diff line number Diff line change
@@ -1,55 +1 @@
# API
## Members

<dl>
<dt><a href="#default">default</a> ⇒ <code>Promise.&lt;Object&gt;</code></dt>
<dd><p>Wrap the ENV service in order to filter ENV vars for the build</p>
</dd>
</dl>

## Functions

<dl>
<dt><a href="#initBuildConstants">initBuildConstants(constants)</a> ⇒ <code>Promise.&lt;Object&gt;</code></dt>
<dd><p>Allow to proxy constants directly by serializing it in the
build, saving some computing and increasing boot time of
lambdas.</p>
</dd>
</dl>

<a name="default"></a>

## default ⇒ <code>Promise.&lt;Object&gt;</code>
Wrap the ENV service in order to filter ENV vars for the build

**Kind**: global variable
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise of an object containing the reshaped env vars.

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | <code>Object</code> | | The services ENV depends on |
| services.NODE_ENV | <code>Object</code> | | The injected NODE_ENV value to add it to the build env |
| [services.PROXYED_ENV_VARS] | <code>Object</code> | <code>{}</code> | A list of environment variable names to proxy |
| [log] | <code>Object</code> | <code>noop</code> | An optional logging service |

<a name="initBuildConstants"></a>

## initBuildConstants(constants) ⇒ <code>Promise.&lt;Object&gt;</code>
Allow to proxy constants directly by serializing it in the
build, saving some computing and increasing boot time of
lambdas.

**Kind**: global function
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise of an object containing the gathered constants.

| Param | Type | Description |
| --- | --- | --- |
| constants | <code>Object</code> | The serializable constants to gather |

**Example**
```js
import { initBuildConstants } from '@whook/aws-lambda';
import { alsoInject } from 'knifecycle';

export default alsoInject(['MY_OWN_CONSTANT'], initBuildConstants);
```
54 changes: 0 additions & 54 deletions packages/whook-aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,60 +110,6 @@ There is a complete example on how to deploy your lambdas
[//]: # (::contents:end)

# API
## Members

<dl>
<dt><a href="#default">default</a> ⇒ <code>Promise.&lt;Object&gt;</code></dt>
<dd><p>Wrap the ENV service in order to filter ENV vars for the build</p>
</dd>
</dl>

## Functions

<dl>
<dt><a href="#initBuildConstants">initBuildConstants(constants)</a> ⇒ <code>Promise.&lt;Object&gt;</code></dt>
<dd><p>Allow to proxy constants directly by serializing it in the
build, saving some computing and increasing boot time of
lambdas.</p>
</dd>
</dl>

<a name="default"></a>

## default ⇒ <code>Promise.&lt;Object&gt;</code>
Wrap the ENV service in order to filter ENV vars for the build

**Kind**: global variable
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise of an object containing the reshaped env vars.

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | <code>Object</code> | | The services ENV depends on |
| services.NODE_ENV | <code>Object</code> | | The injected NODE_ENV value to add it to the build env |
| [services.PROXYED_ENV_VARS] | <code>Object</code> | <code>{}</code> | A list of environment variable names to proxy |
| [log] | <code>Object</code> | <code>noop</code> | An optional logging service |

<a name="initBuildConstants"></a>

## initBuildConstants(constants) ⇒ <code>Promise.&lt;Object&gt;</code>
Allow to proxy constants directly by serializing it in the
build, saving some computing and increasing boot time of
lambdas.

**Kind**: global function
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise of an object containing the gathered constants.

| Param | Type | Description |
| --- | --- | --- |
| constants | <code>Object</code> | The serializable constants to gather |

**Example**
```js
import { initBuildConstants } from '@whook/aws-lambda';
import { alsoInject } from 'knifecycle';

export default alsoInject(['MY_OWN_CONSTANT'], initBuildConstants);
```

# Authors
- [Nicolas Froidure](http://insertafter.com/en/index.html)
Expand Down
1 change: 1 addition & 0 deletions packages/whook-gcp-functions/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# API
20 changes: 20 additions & 0 deletions packages/whook-gcp-functions/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright © 2017 Nicolas Froidure

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
151 changes: 151 additions & 0 deletions packages/whook-gcp-functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
[//]: # ( )
[//]: # (This file is automatically generated by a `metapak`)
[//]: # (module. Do not change it except between the)
[//]: # (`content:start/end` flags, your changes would)
[//]: # (be overridden.)
[//]: # ( )
# @whook/gcp-functions
> Build and deploy to GCP Cloud Functions with Whook.
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/whook/blob/master/packages/whook-gcp-functions/LICENSE)
[![NPM version](https://badge.fury.io/js/%40whook%2Fgcp-functions.svg)](https://npmjs.org/package/@whook/gcp-functions)


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

This module is aimed to help you to build and deploy your Whook server
to [Google Cloud Functions](https://cloud.google.com/functions).

You can find a complete setup with a Terraform deployment example in
[this pull request](https://github.com/nfroidure/whook/pull/66).

## Quick setup

Install this module and its peer dependencies :
```sh
npm i @whook/gcp-functions;
npm i --save-dev @whook/http-transaction babel-loader babel-plugin-knifecycle webpack
```

Add this module to your Whook plugins and tweak the 2 build functions
in your `index.ts` main file:
```diff
import {
runBuild as runBaseBuild,
prepareBuildEnvironment as prepareBaseBuildEnvironment,
} from '@whook/gcp-functions';

// (...)

export async function prepareEnvironment(
$: Knifecycle = new Knifecycle(),
): Promise<Knifecycle> {

// (...)

// Setup your own whook plugins or avoid whook defaults by leaving it empty
- $.register(constant('WHOOK_PLUGINS', ['@whook/cli', '@whook/whook']));
+ $.register(constant('WHOOK_PLUGINS', [
+ '@whook/gcp-functions',
+ '@whook/cli',
+ '@whook/whook',
+ ]));

// (...)

}

// (...)

// The `runBuild` function is intended to build the
// project
export async function runBuild(
innerPrepareEnvironment = prepareBuildEnvironment,
): Promise<void> {
throw new YError('E_NO_BUILD_IMPLEMENTED');

// Usually, here you call the installed build
- // return runBaseBuild(innerPrepareEnvironment);
+ return runBaseBuild(innerPrepareEnvironment);
}

// (...)

// The `prepareBuildEnvironment` create the build
// environment
export async function prepareBuildEnvironment(
$: Knifecycle = new Knifecycle(),
): Promise<Knifecycle> {
$ = await prepareEnvironment($);

// (...)

- // Usually, here you call the installed build env
- // $ = await prepareBaseBuildEnvironment($);
+ // Calling the GCP specific build
+ $ = await prepareBaseBuildEnvironment($);


// The build often need to know were initializer
// can be found to create a static build and
// remove the need to create an injector
$.register(
constant('INITIALIZER_PATH_MAP', {
ENV: require.resolve('@whook/whook/dist/services/ProxyedENV'),
apm: require.resolve('@whook/http-transaction/dist/services/apm'),
obfuscator: require.resolve(
'@whook/http-transaction/dist/services/obfuscator',
),
- log: require.resolve('common-services/dist/log'),
+ log: require.resolve('@whook/gcp-functions/dist/services/log'),
time: require.resolve('common-services/dist/time'),
delay: require.resolve('common-services/dist/delay'),
}),
);

// (...)

}
```

# Build

To build your functions :
```sh
# Build all functions
npm run compile && npm run build
# Build only one function
npm run compile && npm run build -- getPing
```

# Debug

You can easily test your function builds by adding `@whook/gcp-functions`
to your `WHOOK_PLUGINS` list. It provides you some commands like
the `testHTTPFunction` one:
```sh
npx whook testHTTPFunction --name getPing
```

To get more insights when errors happens:
```sh
npm run whook-dev -- testHTTPFunction --name getPing
```

## Deployment

We recommend using [Terraform](https://terraform.io) to deploy your
functions.

There is a complete example on how to deploy your functions
[in this pull request](https://github.com/nfroidure/whook/pull/54).

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

# API

# Authors
- [Nicolas Froidure](http://insertafter.com/en/index.html)

# License
[MIT](https://github.com/nfroidure/whook/blob/master/packages/whook-gcp-functions/LICENSE)
37 changes: 37 additions & 0 deletions packages/whook-gcp-functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cd36ba4

Please sign in to comment.