salix/services/loopback/server/server.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-10-13 14:22:45 +00:00
let loopback = require('loopback');
let boot = require('loopback-boot');
let path = require('path');
let fs = require('fs');
let i18n = require('i18n');
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
let localeDir = `${__dirname}/../common/locale`;
2017-10-13 14:22:45 +00:00
if (fs.existsSync(localeDir)) {
2017-10-13 14:22:45 +00:00
i18n.configure({
directory: localeDir,
2017-10-13 14:22:45 +00:00
defaultLocale: 'es'
});
app.use(i18n.init);
2017-10-13 14:22:45 +00:00
}
2017-10-13 14:54:07 +00:00
// View
2018-01-31 12:40:26 +00:00
let viewDir = `${rootDir}/../client`;
2017-10-13 14:54:07 +00:00
if (fs.existsSync(viewDir)) {
app.set('view engine', 'ejs');
app.set('views', viewDir);
app.use(loopback.static(path.resolve(rootDir, '../client')));
}
2017-10-13 14:22:45 +00:00
// Initialization
2017-10-13 14:54:07 +00:00
let packageJson = require(rootDir + '/../package.json');
2017-10-13 14:22:45 +00:00
let appName = packageJson.name;
app.start = function() {
return app.listen(function() {
app.emit('started');
let baseUrl = app.get('url').replace(/\/$/, '');
2017-10-13 14:22:45 +00:00
console.log(`Web server ${appName} listening at: %s`, baseUrl);
});
};
let config = require('./config.json');
for (let key in config) {
2017-10-13 14:22:45 +00:00
app.set(key, config[key]);
}
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,
models: modelConfig,
2017-10-13 14:22:45 +00:00
modelSources: [
`loopback/common/models`,
`loopback/server/models`,
`${__dirname}/../common/models`,
`${__dirname}/models`,
`${rootDir}/../common/models`,
`${rootDir}/models`
2017-10-13 14:22:45 +00:00
],
mixinDirs: [
`loopback/common/mixins`,
`loopback/server/mixins`,
`${__dirname}/../common/mixins`,
`${__dirname}/mixins`,
`${rootDir}/../common/mixins`,
`${rootDir}/mixins`
2017-10-13 14:22:45 +00:00
],
bootDirs: [
`${__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
});
}