salix/loopback/server/server.js

140 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

require('require-yaml');
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`;
2019-01-25 22:02:29 +00:00
let modulesDir = `${appDir}/modules`;
2017-10-13 14:22:45 +00:00
// Internationalization
2017-10-13 14:54:07 +00:00
2019-01-08 19:15:47 +00:00
app.disconnect = async function() {
let promises = [];
2019-02-11 21:57:48 +00:00
for (let ds in this.dataSources)
promises.push(this.dataSources[ds].disconnect());
2019-01-08 19:15:47 +00:00
return await Promise.all(promises);
};
2019-02-11 21:57:48 +00:00
app.start = function(bootOptions, port, callback) {
let onListen = () => {
let baseUrl = this.get('url').replace(/\/$/, '');
let explorerPath = this.get('loopback-component-explorer').mountPath;
console.log(`Browse your REST API at: %s`, `${baseUrl}${explorerPath}`);
2019-02-11 21:57:48 +00:00
this.emit('started');
callback && callback();
};
this.boot(bootOptions, err => {
if (err) throw err;
let args = port ? [port, onListen] : [onListen];
return this.listen(...args);
});
};
2019-02-12 08:24:20 +00:00
app.boot = function(bootOptions, callback) {
2019-02-11 21:57:48 +00:00
// Internatinalization
if (fs.existsSync(localeDir)) {
i18n.configure({
directory: localeDir,
defaultLocale: 'es'
});
this.use(i18n.init);
}
2019-02-11 21:57:48 +00:00
// Initialization
let config = require('./config.json');
for (let key in config)
this.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`
];
addPath(`${appDir}/back`);
let modules = fs.readdirSync(modulesDir);
for (let mod of modules)
addPath(`${modulesDir}/${mod}/back`);
function addPath(path) {
modelConfigFiles.push(`${path}/model-config.json`);
modelSources.push(`${path}/models`);
mixinDirs.push(`${path}/mixins`);
bootDirs.push(`${path}/boot`);
}
2019-02-11 21:57:48 +00:00
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);
}
}
2019-02-12 08:24:20 +00:00
let myBootOptions = {
2019-02-11 21:57:48 +00:00
appRootDir: __dirname,
appConfigRootDir: rootDir,
modelsRootDir: rootDir,
models: models,
modelSources: modelSources,
mixinDirs: mixinDirs,
bootDirs: bootDirs
};
if (fs.existsSync(`/etc/salix`))
2019-02-12 08:24:20 +00:00
myBootOptions.dsRootDir = `/etc/salix`;
2019-02-11 21:57:48 +00:00
2019-02-12 08:24:20 +00:00
Object.assign(myBootOptions, bootOptions);
boot(this, myBootOptions, callback);
2019-02-11 21:57:48 +00:00
};
2019-02-11 21:57:48 +00:00
if (require.main === module)
app.start();