server.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // OpenShift sample Node application
  2. var express = require('express'),
  3. app = express(),
  4. morgan = require('morgan');
  5. Object.assign=require('object-assign')
  6. app.engine('html', require('ejs').renderFile);
  7. app.use(morgan('combined'))
  8. var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
  9. ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
  10. mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
  11. mongoURLLabel = "";
  12. if (mongoURL == null) {
  13. var mongoHost, mongoPort, mongoDatabase, mongoPassword, mongoUser;
  14. // If using plane old env vars via service discovery
  15. if (process.env.DATABASE_SERVICE_NAME) {
  16. var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase();
  17. mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'];
  18. mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'];
  19. mongoDatabase = process.env[mongoServiceName + '_DATABASE'];
  20. mongoPassword = process.env[mongoServiceName + '_PASSWORD'];
  21. mongoUser = process.env[mongoServiceName + '_USER'];
  22. // If using env vars from secret from service binding
  23. } else if (process.env.database_name) {
  24. mongoDatabase = process.env.database_name;
  25. mongoPassword = process.env.password;
  26. mongoUser = process.env.username;
  27. var mongoUriParts = process.env.uri && process.env.uri.split("//");
  28. if (mongoUriParts.length == 2) {
  29. mongoUriParts = mongoUriParts[1].split(":");
  30. if (mongoUriParts && mongoUriParts.length == 2) {
  31. mongoHost = mongoUriParts[0];
  32. mongoPort = mongoUriParts[1];
  33. }
  34. }
  35. }
  36. if (mongoHost && mongoPort && mongoDatabase) {
  37. mongoURLLabel = mongoURL = 'mongodb://';
  38. if (mongoUser && mongoPassword) {
  39. mongoURL += mongoUser + ':' + mongoPassword + '@';
  40. }
  41. // Provide UI label that excludes user id and pw
  42. mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
  43. mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
  44. }
  45. }
  46. var db = null,
  47. dbDetails = new Object();
  48. var initDb = function(callback) {
  49. if (mongoURL == null) return;
  50. var mongodb = require('mongodb');
  51. if (mongodb == null) return;
  52. mongodb.connect(mongoURL, function(err, conn) {
  53. if (err) {
  54. callback(err);
  55. return;
  56. }
  57. db = conn;
  58. dbDetails.databaseName = db.databaseName;
  59. dbDetails.url = mongoURLLabel;
  60. dbDetails.type = 'MongoDB';
  61. console.log('Connected to MongoDB at: %s', mongoURL);
  62. });
  63. };
  64. app.get('/', function (req, res) {
  65. // try to initialize the db on every request if it's not already
  66. // initialized.
  67. if (!db) {
  68. initDb(function(err){});
  69. }
  70. if (db) {
  71. var col = db.collection('counts');
  72. // Create a document with request IP and current time of request
  73. col.insert({ip: req.ip, date: Date.now()});
  74. col.count(function(err, count){
  75. if (err) {
  76. console.log('Error running count. Message:\n'+err);
  77. }
  78. res.render('index.html', { pageCountMessage : count, dbInfo: dbDetails });
  79. });
  80. } else {
  81. res.render('index.html', { pageCountMessage : null});
  82. }
  83. });
  84. app.get('/pagecount', function (req, res) {
  85. // try to initialize the db on every request if it's not already
  86. // initialized.
  87. if (!db) {
  88. initDb(function(err){});
  89. }
  90. if (db) {
  91. db.collection('counts').count(function(err, count ){
  92. res.send('{ pageCount: ' + count + '}');
  93. });
  94. } else {
  95. res.send('{ pageCount: -1 }');
  96. }
  97. });
  98. // error handling
  99. app.use(function(err, req, res, next){
  100. console.error(err.stack);
  101. res.status(500).send('Something bad happened!');
  102. });
  103. initDb(function(err){
  104. console.log('Error connecting to Mongo. Message:\n'+err);
  105. });
  106. app.listen(port, ip);
  107. console.log('Server running on http://%s:%s', ip, port);
  108. module.exports = app ;