From 8eddffbacb8f100242f5834b465e432d6e879f9e Mon Sep 17 00:00:00 2001 From: Nicolas Jaremek Date: Fri, 25 Sep 2015 10:53:48 +0200 Subject: [PATCH] Added configurable log path. Splitted send email logic into two independent functions for clarity. --- config/default.json | 3 +- config/development.json | 31 -------------- config/test.json | 5 +++ .../dockerized-service/docker-compose.yml | 2 +- lib/platforms/email.js | 42 ++++++++++++++----- lib/routes/email.js | 2 +- lib/util/logger.js | 16 +++++-- package.json | 4 +- 8 files changed, 55 insertions(+), 50 deletions(-) delete mode 100755 config/development.json diff --git a/config/default.json b/config/default.json index 57966a8..bdf1e17 100755 --- a/config/default.json +++ b/config/default.json @@ -7,7 +7,8 @@ "port": 3000, "log": { "level": "debug", - "path": "/tmp/resonator.log" + "directory": "./logs/", + "filename": "resonator.log" }, "db": { "conn": "mongodb://localhost:27017/resonator-backend" diff --git a/config/development.json b/config/development.json deleted file mode 100755 index d67ad00..0000000 --- a/config/development.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "db": { - "conn": "mongodb://mongodb:27017/resonator-backend" - }, - "transport": { - "gcm": { - "googleApiKey" : "", - "batch_limit": 500 - }, - "apn": { - "pfx" : "", - "passphrase" : "", - "production" : true, - "notificationExpiry" : 3600, - "feedbackInterval" : 3600, - "batch_limit": 500 - }, - "twilio": { - "base_url": "https://api.twilio.com/", - "account_sid": "", - "auth_token": "", - "max_sms_length": 1600, - "batch_limit": 200 - }, - "mailgun": { - "apiKey": "", - "domain": "", - "batch_limit": 10 - } - } -} diff --git a/config/test.json b/config/test.json index 5a2e445..df44a37 100755 --- a/config/test.json +++ b/config/test.json @@ -2,6 +2,11 @@ "db": { "conn": "mongodb://localhost:27017/resonator-test" }, + "log": { + "level": "debug", + "directory": "./logs/", + "filename": "resonator-test.log" + }, "mongoose": { "debug": false }, diff --git a/examples/dockerized-service/docker-compose.yml b/examples/dockerized-service/docker-compose.yml index b186dca..0b76793 100644 --- a/examples/dockerized-service/docker-compose.yml +++ b/examples/dockerized-service/docker-compose.yml @@ -5,7 +5,7 @@ resonator: links: - mongodb volumes: - - ./production.json:/app/config/production.json + - ./development.json:/app/config/development.json - /tmp:/tmp environment: - NODE_ENV=development diff --git a/lib/platforms/email.js b/lib/platforms/email.js index adb8853..fb77456 100644 --- a/lib/platforms/email.js +++ b/lib/platforms/email.js @@ -10,17 +10,10 @@ function sendEmailNotification(identities, body, callback) { var emails, content; - if (!identities) { - emails = body.to; - content = body; - content.subject = body.subject; - content.message = content.html; - } else { - emails = _.flatten(_.map(identities, function(identity) { - return identity.devices.email; - })); - content = body.content; - } + emails = _.flatten(_.map(identities, function(identity) { + return identity.devices.email; + })); + content = body.content; log.info('Sending request to send emails', emails, content); @@ -41,6 +34,32 @@ function sendEmailNotification(identities, body, callback) { }); } +function sendSingleEmail(body, callback) { + + var emails = body.to; + var content = body; + content.subject = body.subject; + content.message = content.html; + + log.info('Sending request to sendSingleEmail', emails, content); + + transport.send(emails, content, function (err, response, body) { + if (err) { + return callback(err); + } + + var output = { + response: response, + body: body + }; + + log.info('Receiving response from email request'); + log.debug('Receiving response from email request', body); + + return callback(null, output); + }); +} + var emailOptions = { resourceName: 'devices.email', batchLimit: _.min([BATCH_LIMIT, CUSTOM_BATCH_LIMIT]), @@ -49,5 +68,6 @@ var emailOptions = { module.exports = { send: sendEmailNotification, + sendSingleEmail: sendSingleEmail, options: emailOptions }; diff --git a/lib/routes/email.js b/lib/routes/email.js index 49c302b..668b2fa 100644 --- a/lib/routes/email.js +++ b/lib/routes/email.js @@ -25,7 +25,7 @@ routes.sendEmailNotification = function(req, res, next) { routes.sendSingleEmail = function(req, res, next) { - emailPlatform.send(undefined, req.body, function(err) { + emailPlatform.sendSingleEmail(req.body, function(err) { if (err) { return next(err); } diff --git a/lib/util/logger.js b/lib/util/logger.js index 02408ea..15b3be1 100755 --- a/lib/util/logger.js +++ b/lib/util/logger.js @@ -1,15 +1,25 @@ var config = require('config'), bunyan = require('bunyan'), + fs = require('fs'), logger, options, streams, - defaultStreams = [{ stream: process.stdout }, { path: config.get('log.path') }]; + logDirectory = config.get('log.directory'), + logFullPath = logDirectory + config.get('log.filename'), +defaultStreams = [{ stream: process.stdout }, { path: logFullPath }] + ; + +var dir = logDirectory; +if (!fs.existsSync(dir)){ + fs.mkdirSync(dir); +} + switch (process.env.NODE_ENV) { case 'production': - streams = [{ path: config.get('log.path') }]; + streams = [{ path: logFullPath }]; break; case 'test': case 'test_ci': - streams = [{ path: config.get('log.path') }]; + streams = [{ path: logFullPath }]; break; default: diff --git a/package.json b/package.json index 2d42831..fc92ce6 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "node ./bin/server.js", "start-mock": "node ./bin/mocked-server.js", - "fixtures": "node scripts/load_fixtures", + "fixtures": "node ./scripts/load_fixtures", "test": "npm run test-cucumber && npm run test-mocha", "test-cucumber": "NODE_ENV=test npm run fixtures && NODE_ENV=test cucumber-js cucumber/features/ --tags ~@ignore -r cucumber/features/support -r cucumber/features/", "test-mocha": "NODE_ENV=test npm run fixtures && NODE_ENV=test ./node_modules/.bin/mocha --reporter nyan", @@ -40,7 +40,7 @@ "license": "ISC", "dependencies": { "apn": "^1.7.4", - "async": "^0.9.0", + "async": "^1.4.2", "body-parser": "^1.13.3", "bson": "^0.4.11", "bunyan": "^1.3.5",