diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-18 16:44:27 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-18 16:44:27 +0200 |
commit | 9339e6269d6f5a5d0421c23c5e9ba3c8500fb535 (patch) | |
tree | d4e89f81db55197df84836b7197fc35905315b99 | |
parent | cc0dcb25054e9dff3fdc4b58eaf861d35d0b06de (diff) | |
download | diller-server-9339e6269d6f5a5d0421c23c5e9ba3c8500fb535.tar.gz diller-server-9339e6269d6f5a5d0421c23c5e9ba3c8500fb535.tar.bz2 diller-server-9339e6269d6f5a5d0421c23c5e9ba3c8500fb535.tar.xz diller-server-9339e6269d6f5a5d0421c23c5e9ba3c8500fb535.zip |
wip
-rw-r--r-- | diller.js | 51 | ||||
-rw-r--r-- | src/config.js | 12 |
2 files changed, 54 insertions, 9 deletions
@@ -1,19 +1,52 @@ var mqtt = require('mqtt'); +var fs = require('fs'); var _ = require('underscore'); var winston = require('winston'); var bunyan = require('bunyan'); var pg = require('pg'); -var connectionString = process.env.DATABASE_URL || 'postgres://diller:diller@localhost:5432/diller'; -var mqttUrl = process.env.MQTT_URL || 'mqtt://trygvis.io'; -var pgClient = new pg.Client(connectionString); +var config = require('./src/config'); + +function configureLogging(config) { + var cfg = { + name: 'main' + }; + + if (config.isProd()) { + cfg.streams = [ + { + level: 'warn', + stream: process.stdout + }, + { + level: 'debug', + path: 'log/diller.log' + } + ]; + + var stat; + try { + stat = fs.lstatSync('log'); + } catch(e) { + // Assume this to to be ENOENT + fs.mkdirSync('log'); + } + if (stat && !stat.isDirectory()) { + throw 'Not a directory: log'; + } + } + + return bunyan.createLogger(cfg); +} + +var log = configureLogging(config); + +var pgClient = new pg.Client(config.postgresqlUrl); pgClient.connect(); pgClient.on('connect', function () { log.info('connected to pg'); }); -var log = bunyan.createLogger({name: 'main'}); - var apps = [ './apps/logger', './apps/device', @@ -23,7 +56,7 @@ var apps = [ appConfig = { pg: { connect: function (cb) { - pg.connect(connectionString, cb); + pg.connect(config.postgresqlUrl, cb); } } }; @@ -33,13 +66,13 @@ apps = _.map(apps, function (name) { var app = require(name); var cfg = _.clone(appConfig); - cfg.log = bunyan.createLogger({name: name}); + cfg.log = log.child({app: name}); app.init(cfg); return {name: name, instance: app}; }); -log.info('Connecting to ' + mqttUrl); -var mqttClient = mqtt.connect(mqttUrl); +log.info('Connecting to ' + config.mqttUrl); +var mqttClient = mqtt.connect(config.mqttUrl); mqttClient.on('offline', function () { log.info('offline'); diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..949c284 --- /dev/null +++ b/src/config.js @@ -0,0 +1,12 @@ +function isProd() { + return process.env.NODE_ENV == 'prod'; +} + +var mqttUrl = process.env.MQTT_URL || 'mqtt://trygvis.io'; +var postgresqlUrl = process.env.DATABASE_URL || 'postgres://diller:diller@localhost:5432/diller'; + +module.exports = { + isProd: isProd, + mqttUrl: mqttUrl, + postgresqlUrl: postgresqlUrl +} |