diff --git a/rebuild.sh b/rebuild.sh new file mode 100644 index 0000000..ec42243 --- /dev/null +++ b/rebuild.sh @@ -0,0 +1,20 @@ +#!/bin/bash +NVM="$HOME/.nvm" +NVM_VERSION="5.7.0" +DEV="$HOME/dev" +LIVE="$HOME/live" +SWAP="/swapfile" +DBSETUP="obrand-admin-server/dbrebuild.sh" +PKG_MANAGER=$( command -v yum || command -v apt-get ) || echo "Neither yum nor apt-get found" +PKG_DEVICE=$(basename $PKG_MANAGER) + +echo Copying... + +cd $DEV/O-Brand/dist +cp -ra . $DEV/obrand-admin-server/wwwroot + +cd $DEV/obrand-admin-server + + +rsync -uav --exclude .git $DEV/obrand-admin-server/ $LIVE + diff --git a/server/app/beacon.js b/server/app/beacon.js new file mode 100644 index 0000000..513cc46 --- /dev/null +++ b/server/app/beacon.js @@ -0,0 +1,47 @@ +/** + * + * User: Martin Donnelly + * Date: 2016-04-05 + * Time: 11:48 + * + */ +/*jshint node:true*/ +'use strict'; + +var db = require('../units/db-connector').dbConnection; +var dbPages = require('../units/db-pages')(db); +var logger = require('log4js').getLogger(); + +module.exports = function(app) { + var express = require('express'); + var beaconRouter = express.Router(); + + beaconRouter.get('/', function(req, res) { + var beacon = {}; + + if (req.headers.hasOwnProperty('beacon')) { + beacon = JSON.parse(req.headers.beacon); + } + + /* res.send({ + beacon: beacon + });*/ + logger.info('gettingLatestAddedPage'); + dbPages.getLatestAddedPage() + .then((d) => { + logger.debug(d); + //res.status(407).end(); + res.redirect(301, d.fullpath); + }).catch((err)=> { + console.error(err); + res.status(401).end(); + }); + + }); + + app.use('/beacon', require('body-parser').json()); + app.use('/beacon', require('body-parser').text()); + app.use('/beacon', require('body-parser').urlencoded()); + + app.use('/beacon', beaconRouter); +}; diff --git a/server/units/db-pages.js b/server/units/db-pages.js index 1dfed6a..93c812b 100644 --- a/server/units/db-pages.js +++ b/server/units/db-pages.js @@ -16,70 +16,92 @@ module.exports = function(db) { var module = {}; + module.getLatestAddedPage = function() { + console.log('+ getLatestAddedPage'); + //select '/export/' || pages.pid::text || '.html' as fullpath from pages order by id desc limit 1; + return new Promise(function(resolve, reject) { + + db.oneOrNone( + "select 'export/' || pages.pid::text || '.html' as fullpath from pages order by id desc limit 1;") + .then((d) => { + console.log(d); + console.log('- getlastaddedpage'); + return resolve(d); + }) + .catch((err)=> { + console.log('+getLatestAddedPage failed'); + console.log(err); + return reject(err); + }); + }); + + }; + module.getPageList = function() { return new Promise(function(resolve, reject) { - db.any("select pages.cid, pages.pid, pages.title, company.company_name, '/export/' || pages.pid::text || '.html' as fullpath from pages join company on company.cid = pages.cid;") + db.any( + "select pages.cid, pages.pid, pages.title, company.company_name, '/export/' || pages.pid::text || '.html' as fullpath from pages join company on company.cid = pages.cid;") .then((d) => { return resolve(d); }) .catch((err)=> { - console.log('+getPageList failed'); - console.log(err); - return reject(err); - }); + console.log('+getPageList failed'); + console.log(err); + return reject(err); + }); }); }; module.getFullPageList = function() { - return new Promise(function(resolve, reject) { + return new Promise(function(resolve, reject) { - db.any("select * from pages;") - .then((d) => { - let rArray = []; - for (var l = 0;l < d.length;l++) { - let attributeObj = $U.newObjectFrom(d[l],['id','cid','vid','pid','content','title']); - attributeObj = $U.populateObject(d[l].data,attributeObj); + db.any("select * from pages;") + .then((d) => { + let rArray = []; + for (var l = 0; l < d.length; l++) { + let attributeObj = $U.newObjectFrom(d[l], + ['id', 'cid', 'vid', 'pid', 'content', 'title']); + attributeObj = $U.populateObject(d[l].data, attributeObj); - let newObj = {"id":attributeObj.id, "type":"page","attributes":attributeObj}; - - // { data: [ { "id": "56b7ecbc1ef21172377d6159", "type": "guest", "attributes": { "name":"Ray","message":"First ever guest post entry" } }, ... ] } + let newObj = { + "id": attributeObj.id, + "type": "page", + "attributes": attributeObj + }; + // { data: [ { "id": "56b7ecbc1ef21172377d6159", "type": "guest", "attributes": { "name":"Ray","message":"First ever guest post entry" } }, ... ] } - rArray.push(newObj); - } - return resolve(rArray); + rArray.push(newObj); + } + return resolve(rArray); - - }) - .catch((err)=> { - console.log('+getPageList failed'); - console.log(err); - return reject(err); - }); - }); - }; + }) + .catch((err)=> { + console.log('+getPageList failed'); + console.log(err); + return reject(err); + }); + }); + }; module.sqlInsertPage = function(data) { return new Promise(function(resolve, reject) { - db.func('upsert_page',data) - .then((d)=> { - console.log(d); - console.log('+sqlInsertPage OK'); - return resolve('ok'); - }) - .catch((err)=> { - console.log('+sqlInsertPage failed'); - console.log(err); - return reject(err); - }); - }); - + db.func('upsert_page', data) + .then((d)=> { + console.log(d); + console.log('+sqlInsertPage OK'); + return resolve('ok'); + }) + .catch((err)=> { + console.log('+sqlInsertPage failed'); + console.log(err); + return reject(err); + }); + }); }; - - module.addNewPage = function(data) { return new Promise((resolve, reject) => { let _data, _jsonData; @@ -91,22 +113,41 @@ module.exports = function(db) { _data.pid = newId(); } - _jsonData = $U.newObjectFrom(_data, ['title','image-url','link1-text','link1-url','link2-text','link2-url','link3-text','link3-url','link4-text','link4-url']); + _jsonData = $U.newObjectFrom(_data, + [ + 'title', + 'image-url', + 'link1-text', + 'link1-url', + 'link2-text', + 'link2-url', + 'link3-text', + 'link3-url', + 'link4-text', + 'link4-url' + ]); - const sqlData = [_data.cid, _data.vid, _data.pid, _data.content, _data.title, JSON.stringify(_jsonData)]; + const sqlData = [ + _data.cid, + _data.vid, + _data.pid, + _data.content, + _data.title, + JSON.stringify(_jsonData) + ]; console.log(sqlData); this.sqlInsertPage(sqlData) - .then(function(d) { - console.log('Inserted'); - console.log(d); - data.attributes = _data; - return resolve(data); - }) - .catch(function(err) { - console.log('Failed to insert'); - return reject(err); - }); + .then(function(d) { + console.log('Inserted'); + console.log(d); + data.attributes = _data; + return resolve(data); + }) + .catch(function(err) { + console.log('Failed to insert'); + return reject(err); + }); }); };