couchbase/sync_gateway — pull request #515 by adamcfraser (2014-12-04 23:28:00+00:00)
Title: Feature/issue 482

Webhook initial implementation

Adds event handling infrastructure, with specific support for the `document_change` event, and the `webhook` event handler.

Adds the following components:

`event_manager` (event_manager.go) : maintains the set of registered event handlers, and routes events to the appropriate handlers
`event` (event.go): an event raised during Sync Gateway processing. In this commit only the DocumentChange event is included.
`event_handler`: a component that can receive an event, and perform some operation based on the event type. In this commit the only supported handler is the webhook handler

A webhook handler registered for the document_changed event posts the body of the changed document to the url provided. A filter function can be defined to only post a subset of changed documents.

Webhook handlers are registered in the Sync Gateway config file, in a new eventHandlers section, following the format:
```
"eventHandlers": {
"document_changed": [
{"handler": "webhook",
"url": "http://localhost:8081/filtered",
"filter": `function(doc) {
if (doc.type=="interesting") {
return true;
}
return false;
}`
},
]
}
```
Config parameter information
`handler`: Handler type. 'webhook' is the only currently supported handler type
`url`: URL to post the document
`filter`: javascript function taking the document as input, and returning boolean. The webhook handler cancels the HTTP Post if the handler returns false.