Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b82a1f
commit 68ddc76
Showing
2 changed files
with
75 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68ddc76
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
68ddc76
There was a problem hiding this comment.
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
68ddc76
There was a problem hiding this comment.
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.
68ddc76
There was a problem hiding this comment.
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!