2015-06-17 20:05:32 +00:00
|
|
|
// OpenShift sample Node application
|
2016-04-11 18:52:20 +00:00
|
|
|
var express = require('express'),
|
|
|
|
app = express(),
|
|
|
|
morgan = require('morgan');
|
2016-07-09 15:39:32 +00:00
|
|
|
|
|
|
|
Object.assign=require('object-assign')
|
2015-06-17 20:05:32 +00:00
|
|
|
|
|
|
|
app.engine('html', require('ejs').renderFile);
|
2016-04-11 18:52:20 +00:00
|
|
|
app.use(morgan('combined'))
|
|
|
|
|
|
|
|
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
|
|
|
|
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
|
|
|
|
mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
|
|
|
|
mongoURLLabel = "";
|
2015-06-17 20:05:32 +00:00
|
|
|
|
|
|
|
if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
|
2016-04-11 18:52:20 +00:00
|
|
|
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(),
|
|
|
|
mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'],
|
|
|
|
mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'],
|
|
|
|
mongoDatabase = process.env[mongoServiceName + '_DATABASE'],
|
|
|
|
mongoPassword = process.env[mongoServiceName + '_PASSWORD']
|
|
|
|
mongoUser = process.env[mongoServiceName + '_USER'];
|
|
|
|
|
|
|
|
if (mongoHost && mongoPort && mongoDatabase) {
|
2015-06-17 20:05:32 +00:00
|
|
|
mongoURLLabel = mongoURL = 'mongodb://';
|
2016-04-11 18:52:20 +00:00
|
|
|
if (mongoUser && mongoPassword) {
|
|
|
|
mongoURL += mongoUser + ':' + mongoPassword + '@';
|
2015-06-17 20:05:32 +00:00
|
|
|
}
|
|
|
|
// Provide UI label that excludes user id and pw
|
2016-04-11 18:52:20 +00:00
|
|
|
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
|
|
|
|
mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
|
2015-06-17 20:05:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 18:52:20 +00:00
|
|
|
var db = null,
|
|
|
|
dbDetails = new Object();
|
2015-06-17 20:05:32 +00:00
|
|
|
|
|
|
|
var initDb = function(callback) {
|
|
|
|
if (mongoURL == null) return;
|
|
|
|
|
2016-04-11 18:52:20 +00:00
|
|
|
var mongodb = require('mongodb');
|
2015-06-17 20:05:32 +00:00
|
|
|
if (mongodb == null) return;
|
|
|
|
|
|
|
|
mongodb.connect(mongoURL, function(err, conn) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
db = conn;
|
|
|
|
dbDetails.databaseName = db.databaseName;
|
|
|
|
dbDetails.url = mongoURLLabel;
|
|
|
|
dbDetails.type = 'MongoDB';
|
|
|
|
|
2016-04-11 18:52:20 +00:00
|
|
|
console.log('Connected to MongoDB at: %s', mongoURL);
|
2015-06-17 20:05:32 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
app.get('/', function (req, res) {
|
2016-06-29 17:59:22 +00:00
|
|
|
// try to initialize the db on every request if it's not already
|
|
|
|
// initialized.
|
|
|
|
if (!db) {
|
|
|
|
initDb(function(err){});
|
|
|
|
}
|
2015-06-17 20:05:32 +00:00
|
|
|
if (db) {
|
|
|
|
var col = db.collection('counts');
|
|
|
|
// Create a document with request IP and current time of request
|
|
|
|
col.insert({ip: req.ip, date: Date.now()});
|
|
|
|
col.count(function(err, count){
|
|
|
|
res.render('index.html', { pageCountMessage : count, dbInfo: dbDetails });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.render('index.html', { pageCountMessage : null});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/pagecount', function (req, res) {
|
2016-06-29 17:59:22 +00:00
|
|
|
// try to initialize the db on every request if it's not already
|
|
|
|
// initialized.
|
|
|
|
if (!db) {
|
|
|
|
initDb(function(err){});
|
|
|
|
}
|
2015-06-17 20:05:32 +00:00
|
|
|
if (db) {
|
|
|
|
db.collection('counts').count(function(err, count ){
|
2016-04-11 18:52:20 +00:00
|
|
|
res.send('{ pageCount: ' + count + '}');
|
2015-06-17 20:05:32 +00:00
|
|
|
});
|
2016-04-11 18:52:20 +00:00
|
|
|
} else {
|
2015-06-17 20:05:32 +00:00
|
|
|
res.send('{ pageCount: -1 }');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// error handling
|
|
|
|
app.use(function(err, req, res, next){
|
|
|
|
console.error(err.stack);
|
|
|
|
res.status(500).send('Something bad happened!');
|
|
|
|
});
|
|
|
|
|
|
|
|
initDb(function(err){
|
|
|
|
console.log('Error connecting to Mongo. Message:\n'+err);
|
2014-05-15 23:42:42 +00:00
|
|
|
});
|
2015-06-04 14:16:04 +00:00
|
|
|
|
|
|
|
app.listen(port, ip);
|
2016-04-11 18:52:20 +00:00
|
|
|
console.log('Server running on http://%s:%s', ip, port);
|
|
|
|
|
|
|
|
module.exports = app ;
|