Merge a1c26a4c4d
into d1af14dc9f
This commit is contained in:
commit
d05598b4bb
4 changed files with 130 additions and 120 deletions
13
.editorconfig
Normal file
13
.editorconfig
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# Tells the .editorconfg plugin to stop searching once it finds this file
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_size = 2
|
||||||
|
indent_style = space
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.md]
|
||||||
|
trim_trailing_whitespace = false
|
14
package.json
14
package.json
|
@ -4,18 +4,18 @@
|
||||||
"description": "Node.js sample app for OpenShift 3",
|
"description": "Node.js sample app for OpenShift 3",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chai": "^3.5.0",
|
|
||||||
"chai-http": "^2.0.1",
|
|
||||||
"ejs": "^2.4.1",
|
"ejs": "^2.4.1",
|
||||||
"express": "^4.13.4",
|
"express": "^4.13.4",
|
||||||
"mocha": "^2.4.5",
|
|
||||||
"mongodb": "^2.1.16",
|
"mongodb": "^2.1.16",
|
||||||
"morgan": "^1.7.0",
|
"morgan": "^1.7.0"
|
||||||
"object-assign":"4.1.0"
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"chai": "^3.5.0",
|
||||||
|
"chai-http": "^2.0.1",
|
||||||
|
"mocha": "^2.4.5"
|
||||||
},
|
},
|
||||||
"engine": {
|
"engine": {
|
||||||
"node": "*",
|
"node": ">=4"
|
||||||
"npm": "*"
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server.js",
|
"start": "node server.js",
|
||||||
|
|
166
server.js
166
server.js
|
@ -1,105 +1,97 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
// OpenShift sample Node application
|
// OpenShift sample Node application
|
||||||
var express = require('express'),
|
const express = require('express'),
|
||||||
app = express(),
|
app = express(),
|
||||||
morgan = require('morgan');
|
morgan = require('morgan'),
|
||||||
|
MongoClient = require('mongodb').MongoClient;
|
||||||
Object.assign=require('object-assign')
|
|
||||||
|
|
||||||
app.engine('html', require('ejs').renderFile);
|
app.engine('html', require('ejs').renderFile);
|
||||||
app.use(morgan('combined'))
|
app.use(morgan('combined'));
|
||||||
|
|
||||||
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
|
const port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
|
||||||
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
|
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
|
||||||
mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
|
mongoInfo = getMongoInfo();
|
||||||
mongoURLLabel = "";
|
|
||||||
|
|
||||||
if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
|
const dbPromise = MongoClient.connect(mongoInfo.url);
|
||||||
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) {
|
app.get('/', function (req, res, next) {
|
||||||
mongoURLLabel = mongoURL = 'mongodb://';
|
return dbPromise.then((db) => {
|
||||||
if (mongoUser && mongoPassword) {
|
const countsCol = db.collection('counts');
|
||||||
mongoURL += mongoUser + ':' + mongoPassword + '@';
|
|
||||||
}
|
|
||||||
// Provide UI label that excludes user id and pw
|
|
||||||
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
|
|
||||||
mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
|
|
||||||
|
|
||||||
}
|
return countsCol.insert({ ip: req.ip, date: Date.now() })
|
||||||
}
|
.then(() => countsCol.count())
|
||||||
var db = null,
|
.then((count) => {
|
||||||
dbDetails = new Object();
|
res.render('index.html', {
|
||||||
|
pageCountMessage: count,
|
||||||
var initDb = function(callback) {
|
dbInfo: {
|
||||||
if (mongoURL == null) return;
|
databaseName: db.databaseName,
|
||||||
|
url: mongoInfo.label,
|
||||||
var mongodb = require('mongodb');
|
type: 'MongoDB'
|
||||||
if (mongodb == null) return;
|
}
|
||||||
|
});
|
||||||
mongodb.connect(mongoURL, function(err, conn) {
|
})
|
||||||
if (err) {
|
})
|
||||||
callback(err);
|
.catch(next);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
db = conn;
|
|
||||||
dbDetails.databaseName = db.databaseName;
|
|
||||||
dbDetails.url = mongoURLLabel;
|
|
||||||
dbDetails.type = 'MongoDB';
|
|
||||||
|
|
||||||
console.log('Connected to MongoDB at: %s', mongoURL);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
app.get('/', function (req, res) {
|
|
||||||
// try to initialize the db on every request if it's not already
|
|
||||||
// initialized.
|
|
||||||
if (!db) {
|
|
||||||
initDb(function(err){});
|
|
||||||
}
|
|
||||||
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) {
|
app.get('/pagecount', function (req, res, next) {
|
||||||
// try to initialize the db on every request if it's not already
|
return dbPromise.then((db) => db.collection('counts').count())
|
||||||
// initialized.
|
.then((count) => res.json({
|
||||||
if (!db) {
|
pageCount: count
|
||||||
initDb(function(err){});
|
}))
|
||||||
}
|
.catch(next);
|
||||||
if (db) {
|
|
||||||
db.collection('counts').count(function(err, count ){
|
|
||||||
res.send('{ pageCount: ' + count + '}');
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.send('{ pageCount: -1 }');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// error handling
|
// error handling
|
||||||
app.use(function(err, req, res, next){
|
app.use(function (err, req, res, next) {
|
||||||
console.error(err.stack);
|
console.error(err.stack || err);
|
||||||
res.status(500).send('Something bad happened!');
|
res.status(500).send('Something bad happened!');
|
||||||
});
|
});
|
||||||
|
|
||||||
initDb(function(err){
|
app.listen(port, ip, () => console.log('Server running on http://%s:%s', ip, port));
|
||||||
console.log('Error connecting to Mongo. Message:\n'+err);
|
|
||||||
|
// In case there was an error initializing the database
|
||||||
|
dbPromise.catch((err) => {
|
||||||
|
console.error('Error initializing database:', err.stack || err);
|
||||||
|
process.exit(-1);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, ip);
|
|
||||||
console.log('Server running on http://%s:%s', ip, port);
|
|
||||||
|
|
||||||
module.exports = app ;
|
|
||||||
|
function getMongoInfo() {
|
||||||
|
const mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL;
|
||||||
|
if (mongoURL) {
|
||||||
|
return {
|
||||||
|
url: mongoURL,
|
||||||
|
label: ''
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const serviceName = process.env.DATABASE_SERVICE_NAME;
|
||||||
|
if (!serviceName) {
|
||||||
|
throw new Error('One of OPENSHIFT_MONGODB_DB_URL|MONGO_URL|DATABASE_SERVICE_NAME env variables must be defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
const 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) {
|
||||||
|
throw new Error('When using DATABASE_SERVICE_NAME, you must also provide _SERVICE_HOST, _SERVICE_PORT, and _DATABASE env variables');
|
||||||
|
}
|
||||||
|
|
||||||
|
let mongoAuth = '';
|
||||||
|
if (mongoUser && mongoPassword) {
|
||||||
|
mongoAuth = mongoUser + ':' + mongoPassword + '@';
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = mongoHost + ':' + mongoPort + '/' + mongoDatabase;
|
||||||
|
return {
|
||||||
|
url: 'mongodb://' + mongoAuth + url,
|
||||||
|
label: 'mongodb://' + url
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -1,31 +1,36 @@
|
||||||
var server = require('../server'),
|
'use strict';
|
||||||
chai = require('chai'),
|
|
||||||
chaiHTTP = require('chai-http'),
|
const server = require('../server'),
|
||||||
should = chai.should();
|
chai = require('chai'),
|
||||||
|
chaiHTTP = require('chai-http'),
|
||||||
|
should = chai.should();
|
||||||
|
|
||||||
chai.use(chaiHTTP);
|
chai.use(chaiHTTP);
|
||||||
|
|
||||||
reqServer = process.env.HTTP_TEST_SERVER || server
|
const reqServer = process.env.HTTP_TEST_SERVER || server;
|
||||||
|
|
||||||
describe('Basic routes tests', function() {
|
describe('Basic routes tests', function () {
|
||||||
|
it('GET to / should return 200', function (done) {
|
||||||
|
chai.request(reqServer)
|
||||||
|
.get('/')
|
||||||
|
.end(function (err, res) {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
res.should.have.status(200);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('GET to / should return 200', function(done){
|
it('GET to /pagecount should return 200', function (done) {
|
||||||
chai.request(reqServer)
|
chai.request(reqServer)
|
||||||
.get('/')
|
.get('/pagecount')
|
||||||
.end(function(err, res) {
|
.end(function (err, res) {
|
||||||
res.should.have.status(200);
|
if (err) {
|
||||||
done();
|
return done(err);
|
||||||
})
|
}
|
||||||
|
res.should.have.status(200);
|
||||||
})
|
done();
|
||||||
|
});
|
||||||
it('GET to /pagecount should return 200', function(done){
|
});
|
||||||
chai.request(reqServer)
|
});
|
||||||
.get('/pagecount')
|
|
||||||
.end(function(err, res) {
|
|
||||||
res.should.have.status(200);
|
|
||||||
done();
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue