salix/loopback/server/server.js

160 lines
3.7 KiB
JavaScript
Raw Normal View History

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
let _resolveConnector = DataSource._resolveConnector;
DataSource._resolveConnector = function(...args) {
let name = args[0];
let testPath = `${__dirname}/connectors/${name}.js`;
2018-12-17 10:28:39 +00:00
if (fs.existsSync(testPath)) {
return {
connector: require(testPath),
error: null
};
2018-12-17 10:28:39 +00:00
}
return _resolveConnector.apply(this, args);
2017-10-13 14:54:07 +00:00
};
2017-10-13 14:22:45 +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`;
app.use(cookieParser());
2017-10-13 14:22:45 +00:00
// Internationalization
2017-10-13 14:54:07 +00:00
if (fs.existsSync(localeDir)) {
i18n.configure({
directory: localeDir,
defaultLocale: 'es'
});
2017-10-13 14:54:07 +00:00
app.use(i18n.init);
}
// View
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-02-14 11:52:24 +00:00
app.getWpAssets = entryPoint => {
const wpAssets = require(`${buildDir}/webpack-assets.json`);
2017-10-13 14:22:45 +00:00
let jsFiles = [];
let regex = new RegExp(`(^|~)${entryPoint}($|~)`);
for (let asset in wpAssets) {
if (regex.test(asset))
jsFiles.push(wpAssets[asset].js);
}
2017-10-13 14:22:45 +00:00
return jsFiles;
};
2017-10-13 14:22:45 +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) {
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');
}
let args = port ? [port, onListen] : [onListen];
return app.listen(...args);
};
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`);
}
let models = {};
for (file of modelConfigFiles) {
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
}
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();
});