2018-12-21 10:36:44 +00:00
|
|
|
require('require-yaml');
|
|
|
|
const cookieParser = require('cookie-parser');
|
|
|
|
const loopback = require('loopback');
|
|
|
|
const boot = require('loopback-boot');
|
|
|
|
const DataSource = require('loopback-datasource-juggler').DataSource;
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const i18n = require('i18n');
|
|
|
|
const path = require('path');
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2018-07-09 10:47:22 +00:00
|
|
|
let _resolveConnector = DataSource._resolveConnector;
|
2018-12-21 10:36:44 +00:00
|
|
|
DataSource._resolveConnector = function(...args) {
|
|
|
|
let name = args[0];
|
2018-07-09 10:47:22 +00:00
|
|
|
let testPath = `${__dirname}/connectors/${name}.js`;
|
|
|
|
|
2018-12-17 10:28:39 +00:00
|
|
|
if (fs.existsSync(testPath)) {
|
2018-07-09 10:47:22 +00:00
|
|
|
return {
|
|
|
|
connector: require(testPath),
|
|
|
|
error: null
|
|
|
|
};
|
2018-12-17 10:28:39 +00:00
|
|
|
}
|
2018-07-09 10:47:22 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
return _resolveConnector.apply(this, args);
|
2017-10-13 14:54:07 +00:00
|
|
|
};
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
let app = loopback();
|
|
|
|
module.exports = app;
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
// Configuration
|
|
|
|
|
|
|
|
let rootDir = __dirname;
|
|
|
|
let lbDir = path.resolve(`${rootDir}/..`);
|
|
|
|
let appDir = path.resolve(`${__dirname}/../..`);
|
|
|
|
let localeDir = `${lbDir}/locale`;
|
|
|
|
let viewDir = `${lbDir}/client`;
|
|
|
|
let buildDir = `${appDir}/dist`;
|
|
|
|
let servicesDir = `${appDir}/modules`;
|
|
|
|
let modulesPath = `${appDir}/modules.yml`;
|
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
app.use(cookieParser());
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
// Internationalization
|
2017-10-13 14:54:07 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
if (fs.existsSync(localeDir)) {
|
|
|
|
i18n.configure({
|
|
|
|
directory: localeDir,
|
|
|
|
defaultLocale: 'es'
|
|
|
|
});
|
2017-10-13 14:54:07 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
app.use(i18n.init);
|
|
|
|
}
|
2018-09-25 09:44:21 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
// View
|
2018-12-20 13:37:29 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
if (fs.existsSync(viewDir)) {
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
app.set('views', viewDir);
|
2018-12-27 11:54:16 +00:00
|
|
|
app.use(loopback.static(viewDir));
|
2018-12-21 10:36:44 +00:00
|
|
|
}
|
2018-02-14 11:52:24 +00:00
|
|
|
|
2019-01-02 07:15:55 +00:00
|
|
|
app.getWpAssets = entryPoint => {
|
2019-01-07 07:41:54 +00:00
|
|
|
const wpAssets = require(`${buildDir}/webpack-assets.json`);
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
let jsFiles = [];
|
|
|
|
let regex = new RegExp(`(^|~)${entryPoint}($|~)`);
|
2018-02-03 19:14:44 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
for (let asset in wpAssets) {
|
|
|
|
if (regex.test(asset))
|
|
|
|
jsFiles.push(wpAssets[asset].js);
|
|
|
|
}
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
return jsFiles;
|
|
|
|
};
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
// Initialization
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2019-01-08 19:15:47 +00:00
|
|
|
app.disconnect = async function() {
|
|
|
|
let promises = [];
|
|
|
|
for (let ds in app.dataSources)
|
|
|
|
promises.push(app.dataSources[ds].disconnect());
|
|
|
|
return await Promise.all(promises);
|
|
|
|
};
|
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
app.start = function(port) {
|
2018-12-21 10:36:44 +00:00
|
|
|
function onListen() {
|
|
|
|
let baseUrl = app.get('url').replace(/\/$/, '');
|
|
|
|
let explorerPath = app.get('loopback-component-explorer').mountPath;
|
|
|
|
console.log(`Browse your REST API at: %s`, `${baseUrl}${explorerPath}`);
|
|
|
|
app.emit('started');
|
|
|
|
}
|
2018-01-29 18:57:00 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
let args = port ? [port, onListen] : [onListen];
|
|
|
|
return app.listen(...args);
|
|
|
|
};
|
2018-01-29 18:57:00 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
let config = require('./config.json');
|
|
|
|
|
|
|
|
for (let key in config)
|
|
|
|
app.set(key, config[key]);
|
|
|
|
|
|
|
|
let modelConfigFiles = [
|
|
|
|
`${__dirname}/model-config.json`
|
|
|
|
];
|
|
|
|
let modelSources = [
|
|
|
|
`loopback/common/models`,
|
|
|
|
`loopback/server/models`,
|
|
|
|
`${__dirname}/../common/models`
|
|
|
|
];
|
|
|
|
let mixinDirs = [
|
|
|
|
`loopback/common/mixins`,
|
|
|
|
`loopback/server/mixins`,
|
|
|
|
`${__dirname}/../common/mixins`
|
|
|
|
];
|
|
|
|
let bootDirs = [
|
|
|
|
`${__dirname}/boot`
|
|
|
|
];
|
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
addPath(`${appDir}/back`);
|
|
|
|
|
|
|
|
let services = require(modulesPath);
|
|
|
|
for (let service of services)
|
|
|
|
addPath(`${servicesDir}/${service}/back`);
|
|
|
|
|
|
|
|
function addPath(path) {
|
|
|
|
modelConfigFiles.push(`${path}/model-config.json`);
|
|
|
|
modelSources.push(`${path}/models`);
|
|
|
|
mixinDirs.push(`${path}/mixins`);
|
|
|
|
bootDirs.push(`${path}/boot`);
|
2018-12-21 10:36:44 +00:00
|
|
|
}
|
2018-01-29 18:57:00 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
let models = {};
|
|
|
|
for (file of modelConfigFiles) {
|
2018-12-21 19:22:13 +00:00
|
|
|
if (fs.existsSync(file)) {
|
|
|
|
let fileModels = require(file);
|
|
|
|
for (let key in fileModels) {
|
|
|
|
if (models[key])
|
|
|
|
console.warn(`Redeclaration of '${key}' at ${file}`);
|
|
|
|
}
|
|
|
|
Object.assign(models, fileModels);
|
|
|
|
}
|
2017-10-13 14:54:07 +00:00
|
|
|
}
|
2018-12-21 10:36:44 +00:00
|
|
|
|
|
|
|
let bootOptions = {
|
|
|
|
appRootDir: __dirname,
|
|
|
|
appConfigRootDir: rootDir,
|
|
|
|
modelsRootDir: rootDir,
|
|
|
|
models: models,
|
|
|
|
modelSources: modelSources,
|
|
|
|
mixinDirs: mixinDirs,
|
|
|
|
bootDirs: bootDirs
|
|
|
|
};
|
|
|
|
|
|
|
|
if (fs.existsSync(`/etc/salix`))
|
|
|
|
bootOptions.dsRootDir = `/etc/salix`;
|
|
|
|
|
|
|
|
boot(app, bootOptions, function(err) {
|
|
|
|
if (err) throw err;
|
|
|
|
if (require.main === module)
|
|
|
|
app.start();
|
|
|
|
});
|