2015-12-23 00:16:54 +00:00
'use strict' ;
2016-01-24 20:38:21 +00:00
var httpPort = 9090 ;
2015-12-23 00:16:54 +00:00
var express = require ( 'express' ) ;
var path = require ( 'path' ) ;
var fs = require ( 'fs' ) ;
2016-01-14 21:42:13 +00:00
var fetch = require ( 'fetch' ) ;
var cors = require ( 'cors' ) ;
2016-01-17 20:57:59 +00:00
var morgan = require ( 'morgan' ) ;
2016-01-24 20:38:21 +00:00
var UglifyJS = require ( "uglify-js" ) ;
var Hashes = require ( 'jshashes' ) ;
2016-04-20 16:15:23 +00:00
var minify = require ( 'minify' ) ;
2016-01-14 21:42:13 +00:00
2016-11-17 18:05:26 +00:00
var appPath = '/test' ; // dynamic context path for version or proxy/etc of server app.
2016-01-14 21:42:13 +00:00
var clientResourcesWeb = [ ] ;
var clientResources = {
js : [ ] ,
css : [ ] ,
2016-03-14 00:50:47 +00:00
dss : [ ]
2016-01-17 20:57:59 +00:00
} ;
2016-01-14 21:42:13 +00:00
var addClientResource = function ( clientResource , resourceType ) {
clientResources [ resourceType ] . push ( clientResource ) ;
2016-01-17 20:57:59 +00:00
} ;
2015-12-23 00:16:54 +00:00
2016-01-14 21:42:13 +00:00
var fetchHashResource = function ( fetchEntry , cb ) {
2016-11-17 18:05:26 +00:00
var serverUrl = 'http://localhost:' + httpPort + appPath ;
2016-01-24 20:38:21 +00:00
var hashDigest = new Hashes . SHA1 ;
2016-01-14 21:42:13 +00:00
fetch . fetchUrl ( serverUrl + fetchEntry . url , function ( err , meta , data ) {
if ( err !== null ) { return cb ( err ) ; }
2016-11-17 18:05:26 +00:00
if ( meta . status !== 200 ) { return cb ( 'wrong status: ' + meta . status ) ; }
2016-01-24 20:38:21 +00:00
var assetHash = hashDigest . hex ( '' + data ) ;
2016-01-14 21:42:13 +00:00
clientResourcesWeb . push ( {
url : fetchEntry . url ,
type : fetchEntry . type ,
hash : assetHash
} ) ;
cb ( null ) ;
} ) ;
} ;
var fetchHashResources = function ( fetchList , cb ) {
var resourceStack = fetchList ;
2016-11-17 18:05:26 +00:00
var resourceLoader = function ( err ) {
if ( err !== null ) { return cb ( err ) ; }
2016-01-14 21:42:13 +00:00
resourceStack = resourceStack . slice ( 1 ) ;
if ( resourceStack . length === 0 ) {
cb ( null ) ;
} else {
fetchHashResource ( resourceStack [ 0 ] , resourceLoader ) ;
}
} ;
fetchHashResource ( resourceStack [ 0 ] , resourceLoader ) ;
} ;
var createClientResourceFetchList = function ( ) {
var fetchList = [ ] ;
2016-01-17 20:57:59 +00:00
for ( var clientResourceIdxJs in clientResources . js ) {
var urlJs = clientResources . js [ clientResourceIdxJs ] ;
fetchList . push ( { url : urlJs , type : 'js' } ) ;
2016-01-14 21:42:13 +00:00
}
2016-01-17 20:57:59 +00:00
for ( var clientResourceIdxCss in clientResources . css ) {
var urlCss = clientResources . css [ clientResourceIdxCss ] ;
fetchList . push ( { url : urlCss , type : 'css' } ) ;
2016-01-14 21:42:13 +00:00
}
2016-03-14 00:50:47 +00:00
for ( var clientResourceIdxCssData in clientResources . dss ) {
2016-01-17 20:57:59 +00:00
var urlCssData = clientResources . cssData [ clientResourceIdxCssData ] ;
2016-03-14 00:50:47 +00:00
fetchList . push ( { url : urlCssData , type : 'dss' } ) ;
2016-01-14 21:42:13 +00:00
}
return fetchList ;
} ;
2016-01-17 20:57:59 +00:00
function renderIndex ( ) {
2015-12-23 00:16:54 +00:00
return function ( req , res ) {
2016-01-24 20:38:21 +00:00
var inlineScript = UglifyJS . minify ( _ _dirname + '/../../es5-ff-spa-loader.js' ) ;
2016-04-20 16:15:23 +00:00
minify ( _ _dirname + '/../../es5-ff-spa-loader.css' , { } , function ( err , data ) {
res . render ( 'index' , {
2016-11-17 18:47:02 +00:00
inlineScript : inlineScript . code ,
2016-04-20 16:15:23 +00:00
inlineStyle : data
} ) ;
2015-12-23 00:16:54 +00:00
} ) ;
} ;
}
2016-01-24 20:38:21 +00:00
// Add resources ORDERED per type
2016-01-14 21:42:13 +00:00
addClientResource ( '/static/module/jquery/jquery.js' , 'js' ) ;
addClientResource ( '/static/module/angular/angular.js' , 'js' ) ;
addClientResource ( '/static/module/angular-route/angular-route.js' , 'js' ) ;
addClientResource ( '/static/module/bootstrap/css/bootstrap.css' , 'css' ) ;
addClientResource ( '/static/module/bootstrap/js/bootstrap.js' , 'js' ) ;
addClientResource ( '/static/css/boot.css' , 'css' ) ;
addClientResource ( '/static/css/style.css' , 'css' ) ;
2016-01-24 20:38:21 +00:00
addClientResource ( '/static/js/example-app.js' , 'js' ) ; // deps: jquery,angular
addClientResource ( '/static/js/controller/page-bar.js' , 'js' ) ; // deps: example-app.js
2016-01-14 21:42:13 +00:00
addClientResource ( '/static/js/controller/page-foo.js' , 'js' ) ;
addClientResource ( '/static/js/controller/page-index.js' , 'js' ) ;
2016-11-17 18:05:26 +00:00
// NOTE: appPath should be done as request parameter which auto prefixes the data, as only the client knows the true context path of a http application.
2015-12-23 00:16:54 +00:00
var server = express ( ) ;
2016-01-17 20:57:59 +00:00
server . use ( morgan ( 'dev' ) ) ;
2016-04-20 16:15:23 +00:00
server . use ( cors ( { credentials : true , origin : '*' , exposedHeaders : [ 'X-My-Api' ] } ) ) ;
2016-01-14 21:42:13 +00:00
server . set ( 'view engine' , 'ejs' ) ;
2016-04-20 16:15:23 +00:00
server . set ( 'views' , path . join ( _ _dirname , 'www_views' ) ) ;
server . use ( function ( req , res , next ) { res . header ( 'X-My-Api' , 'noknok' ) ; next ( ) ; } ) ;
server . use ( appPath + '/static' , express . static ( path . join ( _ _dirname , 'www_static' ) ) ) ;
2016-11-17 18:05:26 +00:00
server . use ( appPath + '/static/module/bootstrap' , express . static ( path . join ( _ _dirname , '../../node_modules/bootstrap/dist' ) ) ) ;
server . use ( appPath + '/static/module/jquery' , express . static ( path . join ( _ _dirname , '../../node_modules/jquery/dist' ) ) ) ;
server . use ( appPath + '/static/module/angular' , express . static ( path . join ( _ _dirname , '../../node_modules/angular' ) ) ) ;
server . use ( appPath + '/static/module/angular-route' , express . static ( path . join ( _ _dirname , '../../node_modules/angular-route' ) ) ) ;
2016-04-20 16:15:23 +00:00
server . get ( appPath + '/static/spa-client-resources' , function ( req , res ) { res . json ( { data : { resources : clientResourcesWeb } } ) ; } ) ;
server . get ( appPath + '/static/spa-loader.css' , function ( req , res ) { res . sendFile ( 'es5-ff-spa-loader.css' , { root : path . join ( _ _dirname , '/../../' ) } ) ; } ) ;
server . get ( appPath + '/' , function ( req , res ) { res . redirect ( appPath + '/example-ui' ) ; } ) ;
server . get ( appPath + '/example-ui' , renderIndex ( ) ) ;
server . get ( '/' , function ( req , res ) { res . redirect ( appPath ) ; } ) ;
2016-01-24 20:38:21 +00:00
server . listen ( httpPort ) ;
console . info ( 'Server started on port ' + httpPort ) ;
2016-01-14 21:42:13 +00:00
2016-11-17 18:05:26 +00:00
fetchHashResources ( createClientResourceFetchList ( ) , function ( err ) {
if ( err !== null ) {
console . log ( 'Fatal error ' + err ) ;
process . exit ( 1 ) ;
} else {
console . log ( 'Total assets build: ' + clientResourcesWeb . length ) ;
}
2016-01-14 21:42:13 +00:00
} ) ;