From a2723c6b12d119245e7e25e62c005b0cbb1ce488 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 6 Jun 2014 21:22:57 +0200 Subject: [PATCH] Only one config for client and server. --- Gruntfile.js | 4 ++++ app/index.html | 3 ++- app/scripts/config.js | 21 --------------------- server/app.js | 28 +++++++++++++++++++++++++++- server/config.js | 22 ++++++++++++++++++++-- server/main.js | 2 +- server/services/nodeService.js | 4 ++-- server/templates/config.js | 6 ++++++ 8 files changed, 62 insertions(+), 28 deletions(-) delete mode 100644 app/scripts/config.js create mode 100644 server/templates/config.js diff --git a/Gruntfile.js b/Gruntfile.js index 7df714c..39c2707 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -82,6 +82,10 @@ module.exports = function (grunt) { context: '/api/', host: '127.0.0.1', port: 8080 + }, { + context: '/config.js', + host: '127.0.0.1', + port: 8080 }], livereload: { options: { diff --git a/app/index.html b/app/index.html index d0f843f..1c49b63 100644 --- a/app/index.html +++ b/app/index.html @@ -44,7 +44,6 @@ - @@ -60,5 +59,7 @@ + + diff --git a/app/scripts/config.js b/app/scripts/config.js deleted file mode 100644 index a3f43b5..0000000 --- a/app/scripts/config.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -angular.module('ffffng') -.factory('config', function () { - return { - community: { - name: 'Freifunk Hamburg', - domain: 'hamburg.freifunk.net', - contactEmail: 'kontakt@hamburg.freifunk.net' - }, - map: { - graphUrl: 'http://graph.hamburg.freifunk.net/graph.html', - mapUrl: 'http://graph.hamburg.freifunk.net/geomap.html' - }, - coordsSelector: { - lat: 53.565278, - lng: 10.001389, - defaultZoom: 10 - } - }; -}); diff --git a/server/app.js b/server/app.js index e9467a6..2b0e9e3 100644 --- a/server/app.js +++ b/server/app.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('ffffng').factory('app', function (fs) { +angular.module('ffffng').factory('app', function (fs, config, _) { var express = require('express'); var bodyParser = require('body-parser'); var compress = require('compression'); @@ -10,8 +10,34 @@ angular.module('ffffng').factory('app', function (fs) { app.use(bodyParser()); var clientDir = __dirname + '/../client'; + var templateDir = __dirname + '/templates'; + + var jsTemplateFiles = [ + '/config.js' + ]; app.use(compress()); + + function serveTemplate(mimeType, req, res, next) { + return fs.readFile(templateDir + '/' + req.path, 'utf8', function (err, body) { + if (err) { + return next(err); + } + + res.writeHead(200, {'Content-Type': mimeType}); + res.end(_.template(body, { config: config.client })); + + return null; // to suppress warning + }); + } + + app.use( function (req, res, next) { + if (jsTemplateFiles.indexOf(req.path) >= 0) { + return serveTemplate('application/javascript', req, res, next); + } + return next(); + }); + app.use('/', express.static(clientDir + '/')); return app; diff --git a/server/config.js b/server/config.js index fcd0b08..9ce81c6 100644 --- a/server/config.js +++ b/server/config.js @@ -2,7 +2,25 @@ angular.module('ffffng').factory('config', function () { return { - port: 8080, - peersPath: '/tmp/peers' + server: { + port: 8080, + peersPath: '/tmp/peers' + }, + client: { + community: { + name: 'Freifunk Hamburg', + domain: 'hamburg.freifunk.net', + contactEmail: 'kontakt@hamburg.freifunk.net' + }, + map: { + graphUrl: 'http://graph.hamburg.freifunk.net/graph.html', + mapUrl: 'http://graph.hamburg.freifunk.net/geomap.html' + }, + coordsSelector: { + lat: 53.565278, + lng: 10.001389, + defaultZoom: 10 + } + } }; }); diff --git a/server/main.js b/server/main.js index 1e10ce9..6928544 100644 --- a/server/main.js +++ b/server/main.js @@ -25,6 +25,6 @@ require('./validation/validator'); angular.injector(['ffffng']).invoke(function (config, app, Router) { Router.init(); - app.listen(config.port, '::'); + app.listen(config.server.port, '::'); module.exports = app; }); diff --git a/server/services/nodeService.js b/server/services/nodeService.js index cbb4754..07a946b 100644 --- a/server/services/nodeService.js +++ b/server/services/nodeService.js @@ -16,7 +16,7 @@ angular.module('ffffng') } function findNodeFiles(pattern) { - return glob.sync(config.peersPath + '/' + pattern.toLowerCase()); + return glob.sync(config.server.peersPath + '/' + pattern.toLowerCase()); } function isDuplicate(pattern, token) { @@ -52,7 +52,7 @@ angular.module('ffffng') function writeNodeFile(isUpdate, token, node, callback) { var filename = - config.peersPath + '/' + (node.hostname + '@' + node.mac + '@' + node.key + '@' + token).toLowerCase(); + config.server.peersPath + '/' + (node.hostname + '@' + node.mac + '@' + node.key + '@' + token).toLowerCase(); var data = ''; _.each(linePrefixes, function (prefix, key) { diff --git a/server/templates/config.js b/server/templates/config.js new file mode 100644 index 0000000..0076e5e --- /dev/null +++ b/server/templates/config.js @@ -0,0 +1,6 @@ +'use strict'; + +angular.module('ffffng') +.factory('config', function () { + return <%= JSON.stringify(config) %>; +});