salix/services/loopback/server/server.js

169 lines
4.0 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();
let rootDir = __dirname;
2017-10-13 14:22:45 +00:00
module.exports = app;
2017-10-13 14:22:45 +00:00
// TODO: It should be stored at some config file
app.set('api key', 'salix');
app.set('url auth', '/auth');
app.set('applications', require('./application.json'));
2017-10-13 14:22:45 +00:00
app.use(cookieParser());
2017-10-13 14:22:45 +00:00
// Internationalization
2017-10-13 14:54:07 +00:00
let localeDir = `${__dirname}/../common/locale`;
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
let viewDir = `${rootDir}/../client`;
if (fs.existsSync(viewDir)) {
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
let assetsPath = [
viewDir,
`${__dirname}/../../../dist`
];
2018-02-14 11:52:24 +00:00
let wpAssets;
let wpAssetsFound = false;
2018-02-14 11:52:24 +00:00
for (let assetsDir of assetsPath) {
let wpAssetsFile = `${assetsDir}/webpack-assets.json`;
wpAssetsFound = fs.existsSync(wpAssetsFile);
if (wpAssetsFound) {
wpAssets = require(wpAssetsFile);
break;
}
}
2018-02-14 11:52:24 +00:00
app.getWpAssets = function(entryPoint) {
if (!wpAssetsFound)
throw new Error('Webpack assets file not found: 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
app.start = function(port, callback) {
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 servicesDir = path.resolve(`${__dirname}/../../../modules`);
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`
];
let services = require(`../../services.yml`);
for (let service of services) {
let baseDir = `${servicesDir}/${service}`;
modelConfigFiles.push(`${baseDir}/model-config.json`);
modelSources.push(`${baseDir}/models`);
mixinDirs.push(`${baseDir}/mixins`);
bootDirs.push(`${baseDir}/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();
});