From 0d1573d775d71735286ee76b9e25cc32b77c602d Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Thu, 29 Dec 2016 21:05:01 +0100 Subject: [PATCH] Enable cors on deploy and apps --- package.json | 1 + server/cors-settings.js | 5 +++++ server/index.js | 9 +++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 server/cors-settings.js diff --git a/package.json b/package.json index e46d227..6a1928c 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "brfs": "^1.4.3", "browser-request": "^0.3.3", "browserify": "^13.0.0", + "cors": "^2.8.1", "date-and-time": "^0.3.0", "dom-css": "^2.0.0", "envify": "^3.4.1", diff --git a/server/cors-settings.js b/server/cors-settings.js new file mode 100644 index 0000000..9dc86a7 --- /dev/null +++ b/server/cors-settings.js @@ -0,0 +1,5 @@ +var path = require('path') +var fs = require('fs') + +var contents = fs.readFileSync(path.join(process.env['HOME'], '.binder/cors.conf'), 'utf8') +module.exports = JSON.parse(contents) diff --git a/server/index.js b/server/index.js index 0ce3693..85a43e1 100644 --- a/server/index.js +++ b/server/index.js @@ -5,6 +5,7 @@ var expressWs = require('express-ws') var bodyParser = require('body-parser') var app = express() var http = require('http').Server(app) +var cors = require('cors'); var io = require('socket.io')(http) var assign = require('object-assign') var merge = require('lodash.merge') @@ -20,6 +21,10 @@ app.use('/js', express.static(path.join(__dirname, '../public/js'))) app.use('/css', express.static(path.join(__dirname, '../public/css'))) app.use('/assets', express.static(path.join(__dirname, '../public/assets'))) +// CORS options + +var launchCORSOptions = require('./cors-settings') + // set once the server is started var binder = null @@ -63,7 +68,7 @@ app.get('/validate/:name', function (req, res) { // API endpoints -app.get('/api/deploy/:templateName', function (req, res) { +app.get('/api/deploy/:templateName', cors(launchCORSOptions), function (req, res) { var name = req.params.templateName binder.deployBinder(name, function (err, status) { if (err) return res.status(500).end() @@ -71,7 +76,7 @@ app.get('/api/deploy/:templateName', function (req, res) { }) }) -app.get('/api/apps/:templateName/:id', function (req, res) { +app.get('/api/apps/:templateName/:id', cors(launchCORSOptions), function (req, res) { var name = req.params.templateName var id = req.params.id binder.getDeployStatus(name, id, function (err, status) {