aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--diller.js51
-rw-r--r--src/config.js12
2 files changed, 54 insertions, 9 deletions
diff --git a/diller.js b/diller.js
index 7627d31..aaad6c1 100644
--- a/diller.js
+++ b/diller.js
@@ -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
+}