aboutsummaryrefslogtreecommitdiff
path: root/src/web
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-10-25 00:33:41 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-10-25 01:47:52 +0200
commite8ec4001ce1297d5a3db6d3fc8af8de47daa6a61 (patch)
tree839d9c1da0c2b1db539c104d9aacc8c8e62a3dcd /src/web
parent0266bdd60cb9cccf20a5ded3eba72ea833bee72d (diff)
downloaddiller-server-e8ec4001ce1297d5a3db6d3fc8af8de47daa6a61.tar.gz
diller-server-e8ec4001ce1297d5a3db6d3fc8af8de47daa6a61.tar.bz2
diller-server-e8ec4001ce1297d5a3db6d3fc8af8de47daa6a61.tar.xz
diller-server-e8ec4001ce1297d5a3db6d3fc8af8de47daa6a61.zip
wip
Diffstat (limited to 'src/web')
-rw-r--r--src/web/DillerWeb.js83
1 files changed, 51 insertions, 32 deletions
diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js
index 40fbc3d..a5bdf6b 100644
--- a/src/web/DillerWeb.js
+++ b/src/web/DillerWeb.js
@@ -59,17 +59,22 @@ function DillerWeb(diller, db, config) {
});
}
+ function getIndex(req, res) {
+ var baseUrl = (req.headers['trygvis-prefix'] || '').replace(/\/$/, '');
+ res.render('index', {baseUrl: baseUrl + '/'});
+ }
+
function init() {
app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
- var router = express.Router();
+ var api = express.Router();
- function addRoute(name, method, path, callback) {
- router[method](path, callback);
- var layer = _.last(router.stack);
+ function addApi(name, method, path, callback) {
+ api[method](path, callback);
+ var layer = _.last(api.stack);
calls.push({
name: name,
@@ -82,12 +87,21 @@ function DillerWeb(diller, db, config) {
});
}
- addRoute('getDevices', 'get', '/device', getDevices);
- addRoute('getDevice', 'get', '/device/:deviceId', getDevice);
- addRoute('getValues', 'get', '/property/:propertyId/values', getValues);
+ addApi('getDevices', 'get', '/device', getDevices);
+ addApi('getDevice', 'get', '/device/:deviceId', getDevice);
+ addApi('getValues', 'get', '/property/:propertyId/values', getValues);
+
+ var templates = express.Router();
- app.use('/api', router);
- app.use(express.static('web'));
+ templates.get('/', getIndex);
+
+ app.set('views', 'web/templates');
+ app.set('view engine', 'jade');
+ app.set('view cache', false);
+
+ app.use('/api', api);
+ app.use('/', templates);
+ app.use(express.static('web/static'));
}
function listen() {
@@ -96,54 +110,59 @@ function DillerWeb(diller, db, config) {
}
function generateRpc() {
- console.log('function DillerRpc($http) {');
+ var s =
+ 'function DillerRpc($http, DillerConfig) {\n' +
+ ' var baseUrl = DillerConfig.baseUrl;\n' +
+ '\n';
- var s = _.map(calls, function (call) {
+ s += _.map(calls, function (call) {
//console.error(call);
- console.error('call.layer', call.layer);
+ //console.error('call.layer', call.layer);
var s = ' function ' + call.name + '(' + call.keys.join(', ') + ') {\n' +
' var req = {};\n' +
' req.method = \'' + call.method + '\';\n' +
- ' req.url = \'/api' + call.path + '\';\n';
+ ' req.url = baseUrl + \'/api' + call.path + '\';\n';
s += _.map(call.layer.keys, function (key) {
return ' req.url = req.url.replace(/:' + key.name + '/, ' + key.name + ');\n'
- }).join('');
+ }).join('\n');
s +=
' return $http(req);\n' +
' }\n';
return s;
- });
- _.each(s, function (x) {
- console.log(x);
- });
-
- console.log(' return {');
- console.log(_.map(calls, function (call) {
+ }).join('\n');
+ s += '\n';
+ s += ' return {\n';
+ s += _.map(calls, function (call) {
return ' ' + call.name + ': ' + call.name
- }).join(',\n'));
- console.log(' };');
- console.log('}');
- console.log('');
-
- console.log('DillerRpcResolve = {};');
- _.each(calls, function (call) {
+ }).join(',\n');
+ s += '\n';
+ s += ' };\n';
+ s += '}\n';
+ s += '\n';
+
+ s += 'DillerRpcResolve = {};\n';
+ s += _.map(calls, function (call) {
+ var s = '';
var args = ['DillerRpc'];
if (call.keys.length > 0) {
args.push('$route');
}
- console.log('DillerRpcResolve.' + call.name + ' = function(' + args.join(', ') + ') {');
+ s += 'DillerRpcResolve.' + call.name + ' = function(' + args.join(', ') + ') {\n';
args = _.map(call.keys, function (key) {
return '$route.current.params.' + key;
});
- console.log(' return DillerRpc.' + call.name + '(' + args.join(', ') + ');');
- console.log('};');
- });
+ s += ' return DillerRpc.' + call.name + '(' + args.join(', ') + ');\n';
+ s += '};\n';
+
+ return s;
+ }).join('');
+ return s;
}
return {