'use strict'; var express = require('express'); var async = require('async'); var path = require('path'); var assets = require('node-ff-assets'); var fs = require('fs'); var minify = require('minify'); function buildAssets(server,callbackDone) { var singleResult = 'false' !== process.env.DEV_ASSETS_SINGLE_RESULT; var assetsConfig = require('./example-assets.json'); assets.build({ assets: { js: { configCreate: assets.factory.builder.configCreate.fromJSON(assetsConfig,'js'), configFill: function (config, callback) { async.series([ assets.factory.lib.async.pushLinkSources(config, '/static/js/', 'www_static/js/'), assets.factory.lib.async.pushLinkSources(config, '/static/js/controller/', 'www_static/js/controller/'), ],callback); }, }, css: { configCreate: assets.factory.builder.configCreate.fromJSON(assetsConfig,'css'), configFill: function (config, callback) { async.series([ assets.factory.lib.async.pushLinkSources(config, '/static/css/', 'www_static/css/'), ],callback); }, }, }, assemblerCreate: assets.factory.builder.assemblerCreate.readFileRegex(), assemblerFill: function (assembler, callback) { var serverResultKey = 'ff_assets_'+assembler.config.assetType; assembler.on ('log', assets.factory.assembler.event.log.console(assembler.config.assetType)); assembler.on ('result', assets.factory.assembler.event.result.objectSet(server,serverResultKey)); assembler.config.linkTargetSingleResult = singleResult; callback(); }, },callbackDone); } function renderTemplatePath(viewPath) { return function (req, res) { res.locals.query = req.query; var qi = req.url.indexOf('?'); if (qi === -1) { qi = req.url.length; } res.render(viewPath + req.url.substring(req.route.path.length-1, qi)); }; } function renderIndex(server) { var inline = ''; minify(__dirname+'/../es5-ff-spa-loader.js', {}, function(err, data) { inline = '\n\t\t'; }); var inlineNot = '\n\t\t'; var useInline = true; // or false return function (req, res) { res.render('index', { inlineNot: useInline?'':inlineNot, inline: useInline?inline:'' }); }; } function sendRedirect() { return function (req, res) { res.redirect('/example-ui'); }; } var stringHash = function (str) { var hash = 31; // prime for (var i = 0; i < str.length; i++) { hash = ((hash<<5)-hash)+str.charCodeAt(i); hash = hash & hash; // keep 32b } return hash; }; var server = express(); buildAssets(server,function(err) { if (err) { throw err; } console.info('Server assets done.'); server.set('view engine', 'ejs'); server.set('views', path.join(__dirname,'www_views')); server.use('/static', express.static(path.join(__dirname,'www_static'))); server.use('/static/module/bootstrap', express.static(path.join(__dirname,'node_modules/bootstrap/dist'))); server.use('/static/module/flot', express.static(path.join(__dirname,'node_modules/flot'))); server.get('/static/es5-ff-spa-loader.js', function (req,res) { res.write(fs.readFileSync(__dirname+'/../es5-ff-spa-loader.js', 'utf8')); res.end(); }); server.get('/static/resources-spa-mobile', function (req,res) { var hashJs = stringHash(fs.readFileSync(__dirname+'/www_static/js/lib/assets.js', 'utf8')); var hashCss = stringHash(fs.readFileSync(__dirname+'/www_static/css/lib/assets.css', 'utf8')); res.json({ data: { resources: [ { url: '/static/js/lib/assets.js----TODO', type: 'js', hash: hashJs }, ] } }); }); server.get('/static/resources-spa-web', function (req,res) { var hashJs = stringHash(fs.readFileSync(__dirname+'/www_static/js/lib/assets.js', 'utf8')); var hashCss = stringHash(fs.readFileSync(__dirname+'/www_static/css/lib/assets.css', 'utf8')); res.json({ data: { resources: [ { url: '/static/js/lib/assets.js', type: 'js', hash: hashJs }, { url: '/static/css/lib/assets.css', type: 'css', hash: hashCss } ] } }); }); server.get('/', sendRedirect()); server.get('/example-ui/thtml/*', renderTemplatePath('thtml/')); server.get('/example-ui', renderIndex(server)); server.get('/example-ui/*', renderIndex(server)); // must be last; for HTML5 history console.info('Server config done.'); server.listen(8080); console.info('Server started on port 8080'); });