Skip to content

Commit

Permalink
Merge in ambrosechua's work on CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
wulfsolter committed Nov 7, 2015
1 parent 7b82a1f commit 68ddc76
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
80 changes: 69 additions & 11 deletions app.js
@@ -1,30 +1,88 @@
#!/usr/bin/env node

'use strict';

var express = require('express');
var config = require('./config');
var fs = require('fs');
var https = require('https');
var middleware = require('./middleware');
var app = express();
var defaultPort = 80;
var server = app;
var config;
var sslOptions;

try {
config = require('./config');
} catch (e) {
config = require('./config.default');

var argv = require('minimist')(process.argv.slice(2), {
alias: {
u: 'username',
p: 'password',
_: ['database', 'd'],
a: 'admin'
},
'boolean': ['a'],
'default': {
port: '8081'
}
});

var app = express();
config.site.port = Number.parseInt(argv.port, 10);

if (argv.u && argv.p && (argv.a ? !argv._.length : argv._.length)) { //TODO: Improve validation
if (argv.a) {
config.mongodb.admin = true;
config.mongodb.adminUsername = argv.u;
config.mongodb.adminPassword = argv.p;
} else {
config.mongodb.admin = false;
config.mongodb.auth[0] = {
username: argv.u,
password: argv.p,
database: argv._[0]
};
if ([argv.u, argv.p, argv._].every(function (ele) { return typeof ele === 'object'; })) { //TODO: Improve validation
argv.u.forEach(function (ele, i) {
config.mongodb.auth[i] = {
username: argv.u[i],
password: argv.p[i],
database: argv._[i]
};
});
}
}
} else {
console.log('Usage: mongoe [options] [dbname]');
console.log('');
console.log('Options:');
console.log(' -a, --admin enable admin login');
console.log(' -u, --username=USERNAME username for authentication. can pass multiple values.');
console.log(' -p, --password=PASSWORD password for authentication. can pass multiple values.');
console.log(' -d, --database=DATABASE use DATABASE. not needed when using admin login. can pass multiple values.');
console.log(' --port=PORT listen on PORT.');
console.log('');
console.log('Create config.js to supress this message.');
process.exit(0);
}
}

app.use('/', middleware(config));
app.set('read_only', config.options.readOnly || false);

var defaultPort = 80;
var server = app;

if (config.site.sslEnabled){
defaultPort = 443;
var https = require('https');
var fs = require('fs');
var sslOptions = {
sslOptions = {
key: fs.readFileSync(config.site.sslKey),
cert: fs.readFileSync(config.site.sslCert)
};
server = https.createServer(sslOptions, app);
server = https.createServer(sslOptions, app);
}

server.listen(config.site.port, config.site.host, function() {
console.log('Mongo Express server listening',
'on port ' + (config.site.port || defaultPort),
'at ' + (config.site.host || '0.0.0.0'));
'on port ' + (config.site.port || defaultPort),
'at ' + (config.site.host || '0.0.0.0'));
});
8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -2,7 +2,10 @@
"author": "https://github.com/andzdroid",
"name": "mongo-express",
"description": "Web-based admin interface for MongoDB",
"version": "0.24.1",
"version": "0.25.0",
"bin" : {
"mongoe" : "./app.js"
},
"repository": {
"type": "git",
"url": "git://github.com/andzdroid/mongo-express.git"
Expand All @@ -16,12 +19,13 @@
"errorhandler": "1.4.2",
"express": "4.13.3",
"express-session": "1.11.3",
"minimist": "~1.2.0",
"method-override": "2.3.5",
"mongodb": "2.0.43",
"morgan": "1.6.1",
"serve-favicon": "2.3.0",
"swig": "1.4.2",
"underscore": "1.8.3"
"underscore": "~1.8.0"
},
"devDependencies": {
"mocha": "2.3.3",
Expand Down

4 comments on commit 68ddc76

@serverwentdown
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@serverwentdown
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although i rewrote it using commander.js based on recommendations by @h2non cleaner here

@wulfsolter
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! More like this? 37e76b9

Was a bit too far from master for easy merging.

@serverwentdown
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... wrote it months ago. Sorry 😄 have been busy. It's perfect!

Please sign in to comment.