Skip to content

javanigus/contentful-webhook-tunnel

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

contentful-webhook-tunnel

Build Status NPM Dependency Status NPM Verion

A simple HTTP server for listening to Contentful API Webhooks with secure tunnels to localhost by ngrok.

This module extends contentful-webhook-listener.js to automatically start up ngrok with a tunnel to the same port and registers a webhook for the ngrok URL with Contentful.

This is very useful for local development and/or servers behind firewalls to create a script with custom callbacks for any or all actions that occur in Contentful.

Install

npm install contentful-webhook-tunnel

Usage

Require this module and then create a new server. The createServer function expects the first argument to be an object with the spaces key and an array of space IDs to register the webhook with.

Then listen for one or more of Contentful webhook events, create, save, autoSave, archive, unarchive, delete, publish or unpublish on the server object and setup a custom callback. See contentful-webhook-listener.js for details on the payload for each event.

Lastly, instruct the server to start listening.

var tunnel = require("contentful-webhook-tunnel");

var server = tunnel.createServer({
    "spaces": [ "cfexampleapi" ]
});

server.on("publish", function (payload) {

    console.log("Received webhook for Publish event in Contentful");
    
    // see payload for details
    console.log(payload);

});

server.listen();

To register the webhooks via the Contentful Content Management API requires an access token. Vist the Contentful Developer Center to acquire an access token for a local script. Then, save that token to an environment variable named CONTENTFUL_MANAGEMENT_ACCESS_TOKEN.

Ngrok defaults to using tunnel servers in the US. To use a tunnel server outside the US then set the NGORK_REGION environment variable to another region. See the ngrok documentation for the list of supported regions.

To have your tunnel protected with HTTP Basic Auth, you must create an account the ngrok, get your access token, and set the NGROK_AUTH_TOKEN environment variable. If that environment variable is present, then random a username and password will be created and registered with Contentful each and every time the script is run.

How it Works

Node.js is used to create a HTTP server that listens for requests and processes them as requests from Contentful Webhooks. When this server starts listening, then ngrok is started up and connected to the same port as the server and requires the same authentication, if any. Once ngork is connected and provides an ngrok URL, then the ngrok URL is registered with Contentful via the Contentful Content Management API. If the server is interrupted or terminated, then the registered webhook will be removed from Contentful.

Deploying and running this server on a publicly accessible system does not require ngrok and, therefore, should use the contentful-webhook-listener.js server, that this is based on, instead.

Advanced Usage

In addition to the Contentful webhook events, this server also emits the following events.

Event: 'error'

  • The error that occurred.

Emitted when an error occurs. The server will be closed directly following this event.

Event: 'ngrokConnect'

  • the public URL of your tunnel
  • the URL to your local ngork web interface
  • the local port number that is forwarded

Emitted after ngork connects.

Event: 'ngrokDisconnet'

Emitted after ngrok disconnects.

Event: 'webhookCreated'

  • <Webhook> the webhook definition from Contentful

Emitted after a webhook record is created via the Contentful Content Management API.

Event: 'webhookDeleted'

Emitted after a webhook record is deleted via the Contentful Content Management API.

Usage with a Proxy

To proxy HTTP(S) requests, then set the appropriate npm config variables.

npm config set https-proxy http://proxy.example.com:3128/
npm config set proxy http://proxy.example.com:3128/

If the npm config variables are not found, then these environment variables will be used.

HTTPS_PROXY=http://proxy.example.com:3128/
HTTP_PROXY=http://proxy.example.com:3128/

Todo

  • accept a single space or an array of spaces or an options object as the first argument to createServer()

Change Log

1.5.0 — March 17, 2017

  • port number no longer defaults to 5678
  • if port number is not specified, then the OS will chose a random port number

1.4.0 — March 16, 2017

  • added HTTP proxy support with https-proxy-agent
  • proxy settings are read from the npm config or from the environment variables

1.3.0 — Feburary 7, 2017

  • made HTTP Basic Auth optional since it requires an ngork account
  • options that require an ngork account are now only used if the NGROK_ACCESS_TOKEN is found in the process environment

1.2.0 — January 31, 2017

  • automatically removes orphaned tunnels from the same host upon reconnection
  • returns the URL to the ngrok UI and the local port number that is forwarded as arguments to the ngrokConnect event in addition to the public ngork URL
  • requires ngrok 2.2.6 or greater to get the ngrok UI URL, otherwise that arugment will be undefined

1.1.0 — November 4, 2016

  • random username and password are used if none are provided
  • port number defaults to 5678 if none is provided

1.0.0 — November 3, 2016

  • initial version

License

contentful-webhook-tunnel is available under the MIT License.

About

An HTTP server for listening to Contentful API Webhooks with secure tunnels to localhost by ngrok.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%