From e7d3ff92723cfe858721719365c53364d7e1ec45 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Sat, 3 Feb 2018 22:53:02 +0100 Subject: [PATCH] Refactor gulp and config --- gulpfile.js | 87 ++++++++++++------- services/db/script.cmd | 3 - services/loopback/common/models/address.js | 2 +- services/mailer/application/auth.js | 14 ++- services/mailer/application/config.js | 27 +++--- services/nginx/Dockerfile | 2 +- services/nginx/config.json | 17 ++-- services/nginx/config.production.json | 4 + services/nginx/config.test.json | 4 + services/nginx/nginx.development.mst | 4 +- .../nginx/{nginx.production.mst => nginx.mst} | 14 +-- services/nginx/start.cmd | 2 +- services/nginx/start.sh | 2 +- services/print/application/config.js | 29 +++---- 14 files changed, 112 insertions(+), 99 deletions(-) create mode 100644 services/nginx/config.production.json create mode 100644 services/nginx/config.test.json rename services/nginx/{nginx.production.mst => nginx.mst} (67%) diff --git a/gulpfile.js b/gulpfile.js index 40605ee49..4d8736fc5 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -13,9 +13,8 @@ const path = require('path'); // Configuration -const defaultService = 'salix'; -const defaultPort = 3000; -const devServerPort = 8081; +const isWindows = /^win/.test(process.platform); +const env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development'; const langs = ['es', 'en']; const srcDir = './client'; @@ -26,6 +25,16 @@ const buildDir = `${nginxDir}/static`; const modules = require('./client/modules.json'); const webpackConfig = require('./webpack.config.js'); +let proxyConf = require(`${nginxDir}/config.json`); +let proxyEnvFile = `${nginxDir}/config.${env}.json`; + +if (fs.existsSync(proxyEnvFile)) + Object.assign(proxyConf, require(proxyEnvFile)); + +const defaultService = proxyConf.main; +const defaultPort = proxyConf.defaultPort; +const devServerPort = proxyConf.devServerPort; + // Development gulp.task('default', () => { @@ -37,26 +46,25 @@ gulp.task('client', ['clean'], () => { }); gulp.task('services', callback => { - let isWindows = /^win/.test(process.platform); - let command = isWindows ? 'docker inspect dblocal | findstr Status' : 'docker inspect dblocal | grep Status'; + let command = isWindows + ? 'docker inspect dblocal | findstr Status' + : 'docker inspect dblocal | grep Status'; exec(command, (err, stdout, stderr) => { + if (err) return callback(err); let isNotRunning = !stdout.includes('running'); - if (isNotRunning) { + if (isNotRunning) runSequence('docker-wait', 'services-run', callback); - } else { + else runSequence('services-run', callback); - } }); }); gulp.task('services-run', async () => { const services = await getServices(); - for (let service of services) require(service.index).start(service.port); - await renderNginxConf(services, 'development'); return gulp.start('nginx'); }); @@ -72,13 +80,13 @@ gulp.task('e2e-run', () => { gulp.task('clean', () => { const del = require('del'); - return del([`${buildDir}/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true}); + return del([`${buildDir}/*`, `${nginxDir}/temp/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true}); }); gulp.task('install', () => { const install = require('gulp-install'); - const jsonFile = []; - const services = fs.readdirSync(servicesDir); + let jsonFile = []; + let services = fs.readdirSync(servicesDir); services.push('..'); services.forEach(service => { jsonFile.push(`${servicesDir}/${service}/package.json`); @@ -98,7 +106,7 @@ gulp.task('build', ['clean'], () => { return gulp.start('routes', 'locales', 'webpack', 'build-conf'); }); -gulp.task('build-conf', async callback => { +gulp.task('build-conf', async () => { let compose = await fs.readFile('./docker-compose.tpl.yml', 'utf8'); const yaml = require('js-yaml'); @@ -129,58 +137,73 @@ gulp.task('build-conf', async callback => { let ymlString = yaml.safeDump(composeYml); await fs.writeFile('./docker-compose.yml', ymlString); - await renderNginxConf(services, 'production'); + await buildNginxConf(); }); // Services -async function getServices() { - let startPort = defaultPort + 1; - let servicesData = []; +let services; - const services = fs.readdirSync(servicesDir); +async function getServices() { + if (services) return services; + + let startPort = defaultPort + 1; + services = []; + + const serviceDirs = await fs.readdir(servicesDir); const exclude = ['loopback']; - for (let service of services) { + for (let service of serviceDirs) { let index = `${servicesDir}/${service}/server/server.js`; if (!await fs.exists(index) || exclude.indexOf(service) !== -1) continue; let port = service == defaultService ? defaultPort : startPort++; - servicesData.push({ + services.push({ name: service, index: index, port: port }); } - return servicesData; + return services; } // Nginx -gulp.task('nginx', callback => { - let isWindows = /^win/.test(process.platform); +gulp.task('nginx', async () => { + await buildNginxConf(); let command = isWindows ? 'start.cmd' : 'start.sh'; command = path.join(`${nginxDir}/${command}`); - exec(command, (err, stdout, stderr) => { - if (stderr) console.log(stderr); - callback(err); + return new Promise((resolve, reject) => { + exec(command, (err, stdout, stderr) => { + if (stderr) console.log(stderr); + if (err) return reject(err); + resolve(); + }); }); }); -async function renderNginxConf(services, env) { +async function buildNginxConf() { const mustache = require('mustache'); let params = { - services: services, + services: await getServices(), defaultService: defaultService, defaultPort: defaultPort, - devServerPort: devServerPort + devServerPort: devServerPort, + port: proxyConf.port, + host: proxyConf.host }; - let template = await fs.readFile(`${nginxDir}/nginx.${env}.mst`, 'utf8'); + + let confFile = `${nginxDir}/nginx.${env}.mst`; + + if (!await fs.exists(confFile)) + confFile = `${nginxDir}/nginx.mst`; + + let template = await fs.readFile(confFile, 'utf8'); let nginxConf = mustache.render(template, params); - await fs.writeFile(`${nginxDir}/temp/nginx.${env}.conf`, nginxConf); + await fs.writeFile(`${nginxDir}/temp/nginx.conf`, nginxConf); } // Webpack diff --git a/services/db/script.cmd b/services/db/script.cmd index 49a8ad532..2c7090522 100755 --- a/services/db/script.cmd +++ b/services/db/script.cmd @@ -1,5 +1,2 @@ mysqldump --defaults-extra-file=connect.ini --default-character-set=utf8 --no-data --triggers --routines --events --databases account util vn2008 vn edi bs bi pbx cache salix vncontrol hedera > 01-structure.sql - - - diff --git a/services/loopback/common/models/address.js b/services/loopback/common/models/address.js index 993f52497..04a4ef80b 100644 --- a/services/loopback/common/models/address.js +++ b/services/loopback/common/models/address.js @@ -38,7 +38,7 @@ module.exports = function(Self) { if (changes.isActive == false && finalState.isDefaultAddress) throw new UserError('The default consignee can not be unchecked'); - if (changes.isDefaultAddress == true) { + if (changes.isDefaultAddress == true && finalState.isActive != false) { let filter = { clientFk: finalState.clientFk, isDefaultAddress: {neq: false} diff --git a/services/mailer/application/auth.js b/services/mailer/application/auth.js index b77b104c1..49918eb57 100644 --- a/services/mailer/application/auth.js +++ b/services/mailer/application/auth.js @@ -32,15 +32,13 @@ module.exports = { return this.response.status(401).send({message: 'Token expired'}); // Set proxy host - let host = this.request.headers.host.split(':')[0]; - let proxy; + let proxy = config.proxy; - if (host == '127.0.0.1') - proxy = config.proxy.localhost; - else if (process.env.NODE_ENV == 'production') - proxy = config.proxy.salix; - else if (process.env.NODE_ENV == 'development') - proxy = config.proxy.testSalix; + if (!proxy) + proxy = { + host: 'localhost', + port: 80 + }; this.request.proxyHost = `http://${proxy.host}:${proxy.port}`; this.request.user = { diff --git a/services/mailer/application/config.js b/services/mailer/application/config.js index 34be3cf30..cfb06544d 100644 --- a/services/mailer/application/config.js +++ b/services/mailer/application/config.js @@ -1,23 +1,20 @@ -var path = require('path'); -let defaultFile = 'datasources.json'; +var fs = require('fs-extra'); -function getFile(fileName) { - return require(path.join(__dirname, `/config/${fileName}`)); -} +let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development'; -try { - let envFile = 'datasources.development.json'; +let config = require(`${__dirname}/config/datasources.json`); +let configEnvFile = `${__dirname}/config/datasources.${env}.json`; - if (process.env.NODE_ENV === 'test') - envFile = 'datasources.test.json'; +if (fs.existsSync(configEnvFile)) + Object.assign(config, require(configEnvFile)); - config = getFile(envFile); -} catch (e) { - if (e.code == 'MODULE_NOT_FOUND') - config = getFile(defaultFile); -} +let proxyConf = require(`../../nginx/config.json`); +let proxyEnvFile = `../../nginx/config.${env}.json`; -config.proxy = require('../../nginx/config.json'); +if (fs.existsSync(proxyEnvFile)) + Object.assign(proxyConf, require(proxyEnvFile)); + +config.proxy = proxyConf; config.package = require('../package.json'); module.exports = config; diff --git a/services/nginx/Dockerfile b/services/nginx/Dockerfile index ef071a4d3..6c7376aa3 100644 --- a/services/nginx/Dockerfile +++ b/services/nginx/Dockerfile @@ -3,7 +3,7 @@ FROM nginx RUN rm /etc/nginx/nginx.conf RUN rm /etc/nginx/conf.d/default.conf -COPY tmp/nginx.production.conf /etc/nginx/nginx.conf +COPY tmp/nginx.conf /etc/nginx/nginx.conf COPY static /usr/share/nginx/html RUN apt-get update && apt-get -y install vim dnsmasq dnsutils diff --git a/services/nginx/config.json b/services/nginx/config.json index 050b6724d..44812fe1b 100644 --- a/services/nginx/config.json +++ b/services/nginx/config.json @@ -1,14 +1,7 @@ { - "localhost": { - "host": "localhost", - "port": 5000 - }, - "testSalix": { - "host": "test-salix", - "port": 80 - }, - "salix": { - "host": "salix", - "port": 80 - } + "main": "salix", + "defaultPort": 3000, + "devServerPort": 8081, + "host": "localhost", + "port": 5000 } \ No newline at end of file diff --git a/services/nginx/config.production.json b/services/nginx/config.production.json new file mode 100644 index 000000000..6fde3b708 --- /dev/null +++ b/services/nginx/config.production.json @@ -0,0 +1,4 @@ +{ + "host": "salix", + "port": 80 +} \ No newline at end of file diff --git a/services/nginx/config.test.json b/services/nginx/config.test.json new file mode 100644 index 000000000..2d4f04528 --- /dev/null +++ b/services/nginx/config.test.json @@ -0,0 +1,4 @@ +{ + "host": "test-salix", + "port": 80 +} \ No newline at end of file diff --git a/services/nginx/nginx.development.mst b/services/nginx/nginx.development.mst index a02573f15..25faeddfd 100644 --- a/services/nginx/nginx.development.mst +++ b/services/nginx/nginx.development.mst @@ -21,8 +21,8 @@ http { scgi_temp_path temp/scgi; server { - listen 5000; - server_name localhost; + listen {{port}}; + server_name {{host}}; autoindex off; {{#services}} diff --git a/services/nginx/nginx.production.mst b/services/nginx/nginx.mst similarity index 67% rename from services/nginx/nginx.production.mst rename to services/nginx/nginx.mst index 75cb61aba..146fd25d8 100644 --- a/services/nginx/nginx.production.mst +++ b/services/nginx/nginx.mst @@ -6,15 +6,15 @@ events { } http { - sendfile on; - gzip on; - default_type application/octet-stream; - resolver 127.0.0.1; - include /etc/nginx/mime.types; + sendfile on; + gzip on; + default_type application/octet-stream; + resolver 127.0.0.1; + include /etc/nginx/mime.types; server { - listen 80; - server_name localhost; + listen {{port}}; + server_name {{host}}; autoindex off; root /usr/share/nginx/html; diff --git a/services/nginx/start.cmd b/services/nginx/start.cmd index a4bfa0fba..b7478eb71 100644 --- a/services/nginx/start.cmd +++ b/services/nginx/start.cmd @@ -2,7 +2,7 @@ set currentDir=%cd% set nginxPrefix=%currentDir%\services\nginx -set nginxConf=%nginxPrefix%\temp\nginx.development.conf +set nginxConf=%nginxPrefix%\temp\nginx.conf if "%1"=="" goto caseStart if "%1"=="start" goto caseStart diff --git a/services/nginx/start.sh b/services/nginx/start.sh index 3dd58a28f..90037daa5 100755 --- a/services/nginx/start.sh +++ b/services/nginx/start.sh @@ -2,7 +2,7 @@ nginxBin="/usr/sbin/nginx" nginxPrefix="${PWD}/services/nginx" -nginxConf="$nginxPrefix/temp/nginx.development.conf" +nginxConf="$nginxPrefix/temp/nginx.conf" if [ ! -f $nginxBin ]; then nginxBin="nginx" diff --git a/services/print/application/config.js b/services/print/application/config.js index 53a157937..cfb06544d 100644 --- a/services/print/application/config.js +++ b/services/print/application/config.js @@ -1,23 +1,20 @@ -var path = require('path'); -let defaultFile = 'datasources.json'; +var fs = require('fs-extra'); -function getFile(fileName) { - return require(path.join(__dirname, `/config/${fileName}`)); -} +let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development'; -try { - let envFile = 'datasources.development.json'; +let config = require(`${__dirname}/config/datasources.json`); +let configEnvFile = `${__dirname}/config/datasources.${env}.json`; - if (process.env.NODE_ENV === 'test') - envFile = 'datasources.test.json'; +if (fs.existsSync(configEnvFile)) + Object.assign(config, require(configEnvFile)); - config = getFile(envFile); -} catch (e) { - if (e.code == 'MODULE_NOT_FOUND') - config = getFile(defaultFile); -} +let proxyConf = require(`../../nginx/config.json`); +let proxyEnvFile = `../../nginx/config.${env}.json`; -config.proxy = require('../../nginx/config.json'); +if (fs.existsSync(proxyEnvFile)) + Object.assign(proxyConf, require(proxyEnvFile)); + +config.proxy = proxyConf; config.package = require('../package.json'); -module.exports = config; \ No newline at end of file +module.exports = config;