summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2016-04-14 20:32:08 -0400
committerJules Laplace <jules@okfoc.us>2016-04-14 20:32:08 -0400
commitc98560cca095ef27b909ac47baf843362bffb28d (patch)
tree21b5d4f88d1c7416e41b60ce448bf6a5ef8073da
parent21b7769c2a57d7e9c62318e911a45a6edebc0912 (diff)
OKWebhook module for receiving pushes from git
-rw-r--r--app/index.js7
-rw-r--r--app/node_modules/okservices/okwebhook/index.js83
-rw-r--r--app/node_modules/okservices/okwebhook/package.json11
-rw-r--r--examples/index.js11
-rw-r--r--examples/lib/okexample/index.js5
5 files changed, 114 insertions, 3 deletions
diff --git a/app/index.js b/app/index.js
index 8c3abb9..48e713a 100644
--- a/app/index.js
+++ b/app/index.js
@@ -15,6 +15,7 @@ var OKServer = require('okserver');
var OKSchema = require('okschema');
var OKS3Service = require('okservices/oks3');
var OKTwitterService = require('okservices/oktwitter')
+var OKWebhookService = require('okservices/okwebhook')
require('dotenv').load();
@@ -110,6 +111,12 @@ function OKCMS(options) {
credentials: config,
});
break
+ case 'webhook':
+ services.webhook = OKWebhookService({
+ express: express,
+ config: config,
+ });
+ break
default:
services[key] = config.lib({
db: resourceCache,
diff --git a/app/node_modules/okservices/okwebhook/index.js b/app/node_modules/okservices/okwebhook/index.js
new file mode 100644
index 0000000..d04e662
--- /dev/null
+++ b/app/node_modules/okservices/okwebhook/index.js
@@ -0,0 +1,83 @@
+
+/**
+ * Service which will listen for a Github webhook, fired on push.
+ * This service can be used to rebuild / restart the app automatically
+ * when new code is pushed.
+ */
+
+var crypto = require('crypto')
+var exec = require('child_process').exec
+var path = require('path')
+
+function OKWebhook (options) {
+ if (!(this instanceof OKWebhook)) return new OKWebhook(options)
+ options = options || {}
+ if (!options.express)
+ throw new Error('Express not provided to OKWebhook');
+ if (!options.config)
+ throw new Error('Configuration not provided to OKWebhook');
+ if (options.config.active && !options.config.secret)
+ throw new Error('Github secret not provided to OKWebhook');
+ if (options.config.active && !options.config.command)
+ throw new Error('Build command not provided to OKWebhook');
+
+ var express = options.express
+ var router = express.Router()
+ var config = options.config
+
+ var secret = config.secret
+ var command = config.command
+
+ router.get('/', function (req, res) {
+ res.send('GET not supported')
+ })
+
+ router.post('/', getBody, function (req, res) {
+ if (!config.active)
+ return
+ console.log("OKWebhook received push")
+ var event = req.headers['x-github-event']
+ if (event !== "push") {
+ return res.sendStatus(500)
+ }
+ var sig = req.headers['x-hub-signature'].split('=')[1]
+ var text = req.rawBody
+ var hash = crypto.createHmac('sha1', secret).update(text).digest('hex')
+ if (hash !== sig) {
+ return res.sendStatus(500)
+ }
+ res.sendStatus(200)
+ var cwd = path.dirname(command)
+ exec(command, { cwd: cwd }, function(err, stdout, stderr){
+ // may not fire if process was restarted..
+ console.log(process.env)
+ console.log(stdout)
+ })
+ })
+
+ function getBody (req, res, next) {
+ req.rawBody = ''
+ // req.setEncoding('utf8')
+
+ req.on('data', function(chunk) {
+ req.rawBody += chunk
+ })
+
+ req.on('end', function() {
+ try {
+ req.body = JSON.parse(req.rawBody)
+ } catch (e) {
+ return res.sendStatus(500)
+ }
+ next()
+ })
+ }
+
+ this._router = router
+}
+
+OKWebhook.prototype.middleware = function () {
+ return this._router
+}
+
+module.exports = OKWebhook
diff --git a/app/node_modules/okservices/okwebhook/package.json b/app/node_modules/okservices/okwebhook/package.json
new file mode 100644
index 0000000..0436f01
--- /dev/null
+++ b/app/node_modules/okservices/okwebhook/package.json
@@ -0,0 +1,11 @@
+{
+ "name": "okwebhook",
+ "version": "1.0.0",
+ "description": "webhook to receive pushes from github",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "author": "okfocus <frontdesk@okfoc.us>",
+ "license": "LNT"
+}
diff --git a/examples/index.js b/examples/index.js
index 3bbed28..efbfc38 100644
--- a/examples/index.js
+++ b/examples/index.js
@@ -1,10 +1,13 @@
var okcms = require('..');
+var isProduction = process.env.OK_PRODUCTION === 'true'
+
var app = okcms.createApp({
root: 'public',
- debug: true,
+ debug: !isProduction,
+ production: isProduction,
schemas: {
page: {
@@ -47,6 +50,12 @@ var app = okcms.createApp({
audio: { allowed: true, preserveFilename: true, maxbytes: 100*1024*1024 },
},
+ webhook: {
+ active: false,
+ secret: 'test',
+ command: '/path/to/build.sh',
+ },
+
example: {
lib: require("./lib/okexample"),
stuff: "things",
diff --git a/examples/lib/okexample/index.js b/examples/lib/okexample/index.js
index b614697..04c5984 100644
--- a/examples/lib/okexample/index.js
+++ b/examples/lib/okexample/index.js
@@ -22,13 +22,14 @@ function OKExample (options) {
if (!(this instanceof OKExample)) return new OKExample(options)
options = options || {}
if (!options.express)
- throw new Error('Express not provided to OKDumpfm');
+ throw new Error('Express not provided to OKExample');
if (!options.config)
- throw new Error('Configuration not provided to OKDumpfm');
+ throw new Error('Configuration not provided to OKExample');
var express = options.express
var router = express.Router()
var config = options.config
+ var db = options.db
router.get('*', function (req, res) {
res.send(config.stuff)