2018-01-29 18:57:00 +00:00
|
|
|
let loopback = require('loopback');
|
|
|
|
let boot = require('loopback-boot');
|
2018-02-13 22:01:28 +00:00
|
|
|
let fs = require('fs-extra');
|
2018-01-29 18:57:00 +00:00
|
|
|
let i18n = require('i18n');
|
2018-02-13 22:01:28 +00:00
|
|
|
let path = require('path');
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2017-10-13 14:54:07 +00:00
|
|
|
module.exports = {
|
|
|
|
loopback: loopback,
|
|
|
|
boot: vnBoot
|
|
|
|
};
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2017-11-16 11:35:07 +00:00
|
|
|
function vnBoot(app, rootDir, rootModule) {
|
2017-10-13 14:22:45 +00:00
|
|
|
// Internationalization
|
|
|
|
|
2018-01-29 18:57:00 +00:00
|
|
|
let localeDir = `${__dirname}/../common/locale`;
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2018-01-29 18:57:00 +00:00
|
|
|
if (fs.existsSync(localeDir)) {
|
2017-10-13 14:22:45 +00:00
|
|
|
i18n.configure({
|
2018-01-29 18:57:00 +00:00
|
|
|
directory: localeDir,
|
2017-10-13 14:22:45 +00:00
|
|
|
defaultLocale: 'es'
|
|
|
|
});
|
|
|
|
|
2018-01-29 18:57:00 +00:00
|
|
|
app.use(i18n.init);
|
2017-10-13 14:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 14:54:07 +00:00
|
|
|
// View
|
|
|
|
|
2018-02-14 11:52:24 +00:00
|
|
|
let wpAssets;
|
2018-01-31 12:40:26 +00:00
|
|
|
let viewDir = `${rootDir}/../client`;
|
2017-10-13 14:54:07 +00:00
|
|
|
|
|
|
|
if (fs.existsSync(viewDir)) {
|
2018-02-14 11:52:24 +00:00
|
|
|
try {
|
|
|
|
wpAssets = require('../client/webpack-assets.json');
|
|
|
|
} catch (e) {}
|
|
|
|
|
2017-10-13 14:54:07 +00:00
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
app.set('views', viewDir);
|
|
|
|
app.use(loopback.static(path.resolve(rootDir, '../client')));
|
|
|
|
}
|
|
|
|
|
2018-02-14 11:52:24 +00:00
|
|
|
app.renderIndex = async res => {
|
|
|
|
res.render(`${viewDir}/index.ejs`, {
|
|
|
|
assets: assets
|
|
|
|
});
|
|
|
|
|
|
|
|
function assets(main, deps) {
|
|
|
|
let jsFiles;
|
|
|
|
|
|
|
|
if (wpAssets) {
|
|
|
|
jsFiles = [wpAssets.manifest.js];
|
|
|
|
|
|
|
|
for (let dep of deps)
|
|
|
|
jsFiles.push(wpAssets[dep].js);
|
|
|
|
|
|
|
|
jsFiles.push(wpAssets[main].js);
|
|
|
|
} else {
|
|
|
|
let publicPath = '/static';
|
|
|
|
jsFiles = [`${publicPath}/manifest.js`];
|
|
|
|
|
|
|
|
for (let dep of deps)
|
|
|
|
jsFiles.push(`${publicPath}/${dep}.js`);
|
|
|
|
|
|
|
|
jsFiles.push(`${publicPath}/${main}.js`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsFiles;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-13 14:22:45 +00:00
|
|
|
// Initialization
|
|
|
|
|
2018-02-14 11:52:24 +00:00
|
|
|
app.start = port => {
|
2018-02-03 19:14:44 +00:00
|
|
|
function onListen() {
|
2017-10-13 14:22:45 +00:00
|
|
|
app.emit('started');
|
2018-02-14 11:52:24 +00:00
|
|
|
let packageJson = require(`${rootDir}/../package.json`);
|
|
|
|
let appName = packageJson.name;
|
|
|
|
let baseUrl = app.get('url').replace(/\/$/, '');
|
|
|
|
console.log(`Web server ${appName} listening at: %s`, baseUrl);
|
2018-02-03 19:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let args = port ? [port, onListen] : [onListen];
|
|
|
|
return app.listen.apply(app, args);
|
2017-10-13 14:22:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let config = require('./config.json');
|
|
|
|
|
2018-01-29 18:57:00 +00:00
|
|
|
for (let key in config) {
|
2017-10-13 14:22:45 +00:00
|
|
|
app.set(key, config[key]);
|
|
|
|
}
|
|
|
|
|
2018-01-29 18:57:00 +00:00
|
|
|
let modelConfigFiles = [
|
|
|
|
`${__dirname}/model-config.json`,
|
|
|
|
`${rootDir}/model-config.json`
|
|
|
|
];
|
|
|
|
|
|
|
|
let modelConfig = {};
|
|
|
|
|
|
|
|
for (file of modelConfigFiles)
|
|
|
|
if (fs.existsSync(file))
|
|
|
|
Object.assign(modelConfig, require(file));
|
|
|
|
|
|
|
|
let bootOptions = {
|
2017-10-13 14:22:45 +00:00
|
|
|
appRootDir: __dirname,
|
|
|
|
appConfigRootDir: rootDir,
|
|
|
|
modelsRootDir: rootDir,
|
2018-01-29 18:57:00 +00:00
|
|
|
models: modelConfig,
|
2017-10-13 14:22:45 +00:00
|
|
|
modelSources: [
|
2018-01-29 18:57:00 +00:00
|
|
|
`loopback/common/models`,
|
|
|
|
`loopback/server/models`,
|
|
|
|
`${__dirname}/../common/models`,
|
2018-02-03 19:14:44 +00:00
|
|
|
`${rootDir}/../common/models`
|
2017-10-13 14:22:45 +00:00
|
|
|
],
|
|
|
|
mixinDirs: [
|
2018-01-29 18:57:00 +00:00
|
|
|
`loopback/common/mixins`,
|
|
|
|
`loopback/server/mixins`,
|
|
|
|
`${__dirname}/../common/mixins`,
|
2018-02-03 19:14:44 +00:00
|
|
|
`${rootDir}/../common/mixins`
|
2017-10-13 14:22:45 +00:00
|
|
|
],
|
|
|
|
bootDirs: [
|
2018-01-29 18:57:00 +00:00
|
|
|
`${__dirname}/boot`,
|
|
|
|
`${rootDir}/boot`
|
2017-10-13 14:22:45 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
boot(app, bootOptions, function(err) {
|
|
|
|
if (err) throw err;
|
2017-11-16 11:35:07 +00:00
|
|
|
if (require.main === rootModule)
|
2017-10-13 14:22:45 +00:00
|
|
|
app.start();
|
2017-10-13 14:54:07 +00:00
|
|
|
});
|
|
|
|
}
|