salix/services/loopback/server/server.js

100 lines
2.4 KiB
JavaScript

let loopback = require('loopback');
let boot = require('loopback-boot');
let path = require('path');
let fs = require('fs');
let i18n = require('i18n');
module.exports = {
loopback: loopback,
boot: vnBoot
};
function vnBoot(app, rootDir, rootModule) {
// Internationalization
let localeDir = `${__dirname}/../common/locale`;
if (fs.existsSync(localeDir)) {
i18n.configure({
directory: localeDir,
defaultLocale: 'es'
});
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')));
}
// Initialization
let packageJson = require(rootDir + '/../package.json');
let appName = packageJson.name;
app.start = function() {
return app.listen(function() {
app.emit('started');
let baseUrl = app.get('url').replace(/\/$/, '');
console.log(`Web server ${appName} listening at: %s`, baseUrl);
});
};
let config = require('./config.json');
for (let key in config) {
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 = {
appRootDir: __dirname,
appConfigRootDir: rootDir,
modelsRootDir: rootDir,
models: modelConfig,
modelSources: [
`loopback/common/models`,
`loopback/server/models`,
`${__dirname}/../common/models`,
`${__dirname}/models`,
`${rootDir}/../common/models`,
`${rootDir}/models`
],
mixinDirs: [
`loopback/common/mixins`,
`loopback/server/mixins`,
`${__dirname}/../common/mixins`,
`${__dirname}/mixins`,
`${rootDir}/../common/mixins`,
`${rootDir}/mixins`
],
bootDirs: [
`${__dirname}/boot`,
`${rootDir}/boot`
]
};
boot(app, bootOptions, function(err) {
if (err) throw err;
if (require.main === rootModule)
app.start();
});
}