Merge pull request #13 from sspeiche/v2tmpl
Move towards a more common welcome page and use MongoDB
This commit is contained in:
commit
ea308ce51f
3 changed files with 387 additions and 53 deletions
11
package.json
11
package.json
|
@ -4,26 +4,21 @@
|
||||||
"description": "Node.js sample app for OpenShift 3",
|
"description": "Node.js sample app for OpenShift 3",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "*"
|
"express": "*",
|
||||||
},
|
"mongodb": "*",
|
||||||
"devDependencies": {
|
"ejs": "*"
|
||||||
"nodemon": "*"
|
|
||||||
},
|
},
|
||||||
"engine": {
|
"engine": {
|
||||||
"node": "*",
|
"node": "*",
|
||||||
"npm": "*"
|
"npm": "*"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "nodemon --ignore node_modules/ server.js",
|
|
||||||
"start": "node server.js"
|
"start": "node server.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "http://github.com/openshift/nodejs-ex.git"
|
"url": "http://github.com/openshift/nodejs-ex.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
|
||||||
"Echo"
|
|
||||||
],
|
|
||||||
"author": "Steve Speicher <sspeiche@gmail.com>",
|
"author": "Steve Speicher <sspeiche@gmail.com>",
|
||||||
"license": "",
|
"license": "",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
|
|
123
server.js
123
server.js
|
@ -1,53 +1,86 @@
|
||||||
var util = require('util');
|
// OpenShift sample Node application
|
||||||
var url = require('url');
|
|
||||||
var qs = require('querystring');
|
|
||||||
var os = require('os')
|
|
||||||
var port = process.env.PORT || process.env.port || process.env.OPENSHIFT_NODEJS_PORT || 8080;
|
|
||||||
var ip = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
|
|
||||||
var nodeEnv = process.env.NODE_ENV || 'unknown';
|
|
||||||
|
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var app = express();
|
var fs = require('fs');
|
||||||
|
var app = express();
|
||||||
|
var eps = require('ejs');
|
||||||
|
|
||||||
app.use(function(req, res, next) {
|
app.engine('html', require('ejs').renderFile);
|
||||||
var url_parts = url.parse(req.url, true);
|
|
||||||
|
|
||||||
var body = '';
|
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
|
||||||
req.on('data', function (data) {
|
var ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
|
||||||
body += data;
|
var mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL;
|
||||||
});
|
var mongoURLLabel = "";
|
||||||
req.on('end', function () {
|
if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
|
||||||
var formattedBody = qs.parse(body);
|
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase();
|
||||||
|
var mongoHost = process.env[mongoServiceName + "_SERVICE_HOST"];
|
||||||
|
var mongoPort = process.env[mongoServiceName + "_SERVICE_PORT"];
|
||||||
|
var mongoUser = process.env.MONGODB_USER
|
||||||
|
if (mongoHost && mongoPort && process.env.MONGODB_DATABASE) {
|
||||||
|
mongoURLLabel = mongoURL = 'mongodb://';
|
||||||
|
if (process.env.MONGODB_USER && process.env.MONGODB_PASSWORD) {
|
||||||
|
mongoURL += process.env.MONGODB_USER + ':' + process.env.MONGODB_PASSWORD + '@';
|
||||||
|
}
|
||||||
|
// Provide UI label that excludes user id and pw
|
||||||
|
|
||||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + process.env.MONGODB_DATABASE;
|
||||||
|
mongoURL += mongoHost + ':' + mongoPort + '/' + process.env.MONGODB_DATABASE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var db = null;
|
||||||
|
var dbDetails = new Object();
|
||||||
|
|
||||||
res.write('Hello OpenShift World! This is a Node.js-based sample application.\n');
|
var initDb = function(callback) {
|
||||||
res.write('Host: ' + req.headers.host + '\n');
|
if (mongoURL == null) return;
|
||||||
res.write('\n');
|
|
||||||
res.write('node.js Production Mode: ' + (nodeEnv == 'production' ? 'yes' : 'no') + '\n');
|
var mongodb = require('mongodb');
|
||||||
res.write('\n');
|
if (mongodb == null) return;
|
||||||
res.write('HTTP/' + req.httpVersion +'\n');
|
|
||||||
res.write('Request headers:\n');
|
mongodb.connect(mongoURL, function(err, conn) {
|
||||||
res.write(util.inspect(req.headers, null) + '\n');
|
if (err) {
|
||||||
res.write('Request query:\n');
|
callback(err);
|
||||||
res.write(util.inspect(url_parts.query, null) + '\n');
|
return;
|
||||||
res.write('Request body:\n');
|
}
|
||||||
res.write(util.inspect(formattedBody, null) + '\n');
|
|
||||||
res.write('\n');
|
db = conn;
|
||||||
res.write('Host: ' + os.hostname() + '\n');
|
dbDetails.databaseName = db.databaseName;
|
||||||
res.write('OS Type: ' + os.type() + '\n');
|
dbDetails.url = mongoURLLabel;
|
||||||
res.write('OS Platform: ' + os.platform() + '\n');
|
dbDetails.type = 'MongoDB';
|
||||||
res.write('OS Arch: ' + os.arch() + '\n');
|
|
||||||
res.write('OS Release: ' + os.release() + '\n');
|
console.log("Connected to MongoDB at: " + mongoURL);
|
||||||
res.write('OS Uptime: ' + os.uptime() + '\n');
|
});
|
||||||
res.write('OS Free memory: ' + os.freemem() / 1024 / 1024 + 'mb\n');
|
};
|
||||||
res.write('OS Total memory: ' + os.totalmem() / 1024 / 1024 + 'mb\n');
|
|
||||||
res.write('OS CPU count: ' + os.cpus().length + '\n');
|
app.get('/', function (req, res) {
|
||||||
res.write('OS CPU model: ' + os.cpus()[0].model + '\n');
|
if (db) {
|
||||||
res.write('OS CPU speed: ' + os.cpus()[0].speed + 'mhz\n');
|
var col = db.collection('counts');
|
||||||
res.end('\n');
|
// Create a document with request IP and current time of request
|
||||||
});
|
col.insert({ip: req.ip, date: Date.now()});
|
||||||
next();
|
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) {
|
||||||
|
if (db) {
|
||||||
|
db.collection('counts').count(function(err, count ){
|
||||||
|
res.send('{ pageCount: ' + count +'}');
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, ip);
|
app.listen(port, ip);
|
||||||
|
|
306
views/index.html
Normal file
306
views/index.html
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue