https://github.com/supercharge/hapi-google-cloud-functions
Serverless functions on Google Cloud Functions with hapi.js
https://github.com/supercharge/hapi-google-cloud-functions
google-cloud hapi hapijs nodejs serverless serverless-functions supercharge
Last synced: 5 months ago
JSON representation
Serverless functions on Google Cloud Functions with hapi.js
- Host: GitHub
- URL: https://github.com/supercharge/hapi-google-cloud-functions
- Owner: supercharge
- License: mit
- Created: 2019-12-11T14:20:36.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-07-09T07:58:17.000Z (about 4 years ago)
- Last Synced: 2024-04-26T05:43:58.766Z (over 1 year ago)
- Topics: google-cloud, hapi, hapijs, nodejs, serverless, serverless-functions, supercharge
- Language: JavaScript
- Size: 50.8 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
hapi in Google Cloud Functions
Run your hapi server in a Google Cloud function.
Follow @marcuspoehls and @superchargejs for updates!
---
## Introduction
Serverless is becoming popular and widely accepted in the developer community. Going serverless requires a mindset shift. Going serverless requires you to think stateless.This `@supercharge/hapi-google-cloud-functions` package let’s you use your [hapi.js](https://hapi.dev) HTTP server in a Google Cloud function.
This package wraps your hapi server and transforms an incoming request to the cloud function into a hapi-compatible request. This request will be injected into your hapi server and the resulting response transformed *back* into a cloud-function-compatible format.
It’s basically a “done for you” package to run your hapi server in a serverless function on Google Cloud.
## Requirements
> **hapi v19 (or later)** and **Node.js v12 (or newer)**This plugin requires **hapi v19** (or later) and **Node.js v12 or newer**.
### Compatibility
| Major Release | [hapi.js](https://github.com/hapijs/hapi) version | Node.js version |
| --- | --- | --- |
| `v2` | `>=19 @hapi/hapi` | `>=12` |
| `v1` | `>=18 hapi` | `>=8` |## Installation
```
npm i @supercharge/hapi-google-cloud-functions
```## Usage
Using `@supercharge/hapi-google-cloud-functions` is pretty straightforward:```js
'use strict'const Hapi = require('@hapi/hapi')
const CloudFunctionHandler = require('@supercharge/hapi-google-cloud-functions')// this `handler` will be used as a cached instance
// a warm function will reuse the handler for incoming requests
let handlermodule.exports.http = async (request, response) => {
if (!handler) {
// First, compose your hapi server with all the plugins and dependencies
server = new Hapi.Server()await server.register({
plugin: require('@hapi/vision')
})// Second, create a handler instance for your server which will
// transform the Cloud function request into a hapi-compatible
// request. Then, send the request through your hapi server
// and transform the response from hapi into a
// function-compatible response
handler = CloudFunctionHandler.for(server)
}return handler.proxy(request, response)
}
```## Deployment Example
There’s a deployment example in the [supercharge/playground-google-cloud-functions](https://github.com/supercharge/playground-google-cloud-functions) repository.We used the [Serverless](https://serverless.com/cli/) framework to deploy a Supercharge app in the `playground-google-cloud-functions` repository. The Serverless CLI is sweet because it allows you to deploy your app from a single configuration file.
### 1. Install the `serverless-google-cloudfunctions` Package
When deploying with the Serverless CLI, you need to add the [`serverless-google-cloudfunctions`](https://github.com/serverless/serverless-google-cloudfunctions) package as a dependency to your project. Install it from NPM:```bash
npm i serverless-google-cloudfunctions
```Then, you must add it as a plugin to your `serverless.yml` file. The next step describes this file in more detail.
### 2. Deploy to a Google Cloud Function
Deploying to Google Cloud from the Serverless CLI needs a keyfile. Follow these steps in the Serverless docs to [set up your Google Cloud credentials and generate a keyfile](https://serverless.com/framework/docs/providers/google/guide/credentials#get-credentials--assign-roles).Here’s the sample `serverless.yml` used to deploy the app:
```yaml
service: supercharge-gcp-function # do not use the "google" in the nameprovider:
name: google
runtime: nodejs8
region: europe-west1
project: your-gcp-project-name
credentials: ./path/to/your/gcp-keyfile.jsonfunctions:
app:
handler: http
memorySize: 256 # default is 1024 MB
events:
- http: pathplugins:
- serverless-google-cloudfunctions
```### 3. Deploy
Deploy your project to Google Cloud using the Serverless CLI. Run the following command from your project directory:```bash
sls deploy
```The deployment process may take some minutes. When finished, you’ll see a URL to access the deployed function. Enjoy!
## Contributing
Do you miss a string function? We very much appreciate your contribution! Please send in a pull request 😊1. Create a fork
2. Create your feature branch: `git checkout -b my-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request 🚀## License
MIT © [Supercharge](https://superchargejs.com)---
> [superchargejs.com](https://superchargejs.com) ·
> GitHub [@supercharge](https://github.com/supercharge) ·
> Twitter [@superchargejs](https://twitter.com/superchargejs)