From efb54c43e5432875c15e504e7a0668a33d744a22 Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Sun, 29 Jul 2012 16:53:41 +0200
Subject: o Validating the DATABASE_URL property before starting.

---
 app.js          | 11 +++++++++++
 routes/index.js |  8 +++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/app.js b/app.js
index f7dea78..f6565a1 100644
--- a/app.js
+++ b/app.js
@@ -6,6 +6,13 @@ var accept = require('http-accept')
 
 var app = express();
 
+var database_url = process.env.DATABASE_URL;
+
+if(typeof database_url != 'string' || database_url.length == 0) {
+  console.warn('Missing environment property DATABASE_URL');
+  process.exit(1);
+}
+
 app.configure(function(){
   app.set('port', process.env.PORT || 3000);
   app.set('views', __dirname + '/views');
@@ -21,6 +28,10 @@ app.configure(function(){
   app.use(accept);
   app.use(app.router);
   app.use(express.static(__dirname + '/public', {maxAge: 60 * 60 * 1000}));
+
+  routes.setup({
+    database_url: process.env.DATABASE_URL
+  });
 });
 
 app.configure('development', function(){
diff --git a/routes/index.js b/routes/index.js
index 30f0f9b..24ddf9f 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -4,6 +4,12 @@ var collection_json = require('collection_json')
   , EmployeeDao = require('../lib/dao.js')
   , _ = require('underscore');
 
+var database_url;
+
+module.exports.setup = function(options) {
+  database_url = options.database_url;
+}
+
 function mapRow(row) {
   var data = _.map(row, function(value, key) {
       return {name: key, value: value};
@@ -50,7 +56,7 @@ function pager(req) {
 }
 
 function connect(res, cb) {
-  pg.connect(process.env.DATABASE_URL, function(err, client) {
+  pg.connect(database_url, function(err, client) {
     if(err) {
       res.send('Unable to connect to PostgreSQL.', {'Content-Type': 'text/plain'}, 500);
     }
-- 
cgit v1.2.3