-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsiteConfig.js
110 lines (91 loc) · 3.93 KB
/
siteConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// website specific configuration
// (c)copyright 2015 by Gerald Wodni <[email protected]>
"use strict";
var _ = require( "underscore" );
var os = require( "os" );
var fs = require( "fs" );
var path = require( "path" );
var async = require( "async" );
module.exports = function _siteConfig( k, opts ) {
const autoLoadEnabled = process.env.KERN_AUTO_LOAD !== "false"
var websiteConfigs = {};
function getField( item, field, defaultValue ) {
if( typeof item === "undefined" )
return defaultValue;
var index = field.indexOf( "." );
if( index > 0 )
return getField( item[ field.substring( 0, index ) ], field.substring( index + 1 ), defaultValue );
else if( _.has( item, field ) )
return item[ field ];
else
return defaultValue;
};
function get( website, key, defaultValue ) {
if( !_.has( websiteConfigs, website ) )
return defaultValue;
return getField( websiteConfigs[ website ], key, defaultValue );
}
function loadAll(d) {
const loadOnlyHosts = process.env.KERN_LOAD_ONLY_HOSTS || "" != "" ?
process.env.KERN_LOAD_ONLY_HOSTS.split(",") : false;
console.log("LoadOnlyHosts:".yellow.bold, loadOnlyHosts );
/* configure websites (async) */
fs.readdir( k.kernOpts.websitesRoot, function _loadAll_readdir( err, dirs ) {
if( err )
throw err;
/* all loaded, perform callback */
var remaining = dirs.length;
function done() {
if( --remaining == 0 )
d();
}
_.map( dirs, function _configure_dir( website ) {
if( loadOnlyHosts && loadOnlyHosts.indexOf( website ) == -1 )
return done();
fs.readFile( path.join( k.kernOpts.websitesRoot, website, "config.json" ), function( err, data ) {
/* skip if error / non-existant */
if( err )
return done();
var finalConfig = {};
try {
var config = JSON.parse( data );
} catch( err ) {
console.log( "WebSite Config ERROR".bold.red, website );
throw err;
}
_.each( config, function _it( websiteConfig, host ) {
if( new RegExp( host, "i" ).test( os.hostname() ) ) {
finalConfig = _.extend( finalConfig, websiteConfig );
console.log( "Config".green.bold + " " + website.grey + " " + host + " activated".bold.green );
}
else
console.log( "Config".green.bold + " " + website.grey + " " + host + " skipped".yellow );
});
websiteConfigs[ website ] = finalConfig;
/* add routes */
if( finalConfig.hierarchyUp ) {
console.log( "Hierarchy-Route: ", website, " -> ", finalConfig.hierarchyUp );
k.hierarchy.addRoute( website, finalConfig.hierarchyUp );
}
/* add mysql DB */
if( finalConfig.mysql ) {
k.db.add( website, finalConfig.mysql );
}
/* autoload */
if( finalConfig.autoLoad && autoLoadEnabled )
k.site.load( website, function _autoLoad_callback(err) {
if( err )
console.log("Autoload-Error:".bold.red, err );
done();
});
else
done();
});
});
});
}
return {
loadAll: loadAll,
get: get
}
}