Giter Club home page Giter Club logo

hapi-google-cloud-functions's Introduction



hapi in Google Cloud Functions

Run your hapi server in a Google Cloud function.


Installation · Usage



Latest Version Monthly downloads

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 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 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:

'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 handler

module.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 repository.

We used the Serverless 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 package as a dependency to your project. Install it from NPM:

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.

Here’s the sample serverless.yml used to deploy the app:

service: supercharge-gcp-function # do not use the "google" in the name

provider:
  name: google
  runtime: nodejs8
  region: europe-west1
  project: your-gcp-project-name
  credentials: ./path/to/your/gcp-keyfile.json

functions:
  app:
    handler: http
    memorySize: 256 # default is 1024 MB
    events:
      - http: path

plugins:
  - serverless-google-cloudfunctions

3. Deploy

Deploy your project to Google Cloud using the Serverless CLI. Run the following command from your project directory:

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


superchargejs.com  ·  GitHub @supercharge  ·  Twitter @superchargejs

hapi-google-cloud-functions's People

Contributors

dependabot-preview[bot] avatar devinrhode2 avatar marcuspoehls avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

devinrhode2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.