goldilocks.js/serve.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-06-24 21:45:54 +00:00
#!/usr/bin/env node
2015-06-24 21:36:17 +00:00
'use strict';
var https = require('https');
var path = require('path');
2015-06-30 23:11:01 +00:00
function createServer(port, pubdir, content) {
var options = require('localhost.daplie.com-certificates');
var server = https.createServer(options);
2015-06-24 21:36:17 +00:00
var app = require('./app');
2015-06-30 23:11:01 +00:00
var directive = { public: pubdir, content: content };
2015-06-24 21:36:17 +00:00
server.on('error', function (err) {
console.error(err);
process.exit(1);
});
server.listen(port, function () {
var msg = 'Serving ' + pubdir + ' at https://localhost.daplie.com';
var p = server.address().port;
if (443 !== p) {
msg += ':' + p;
}
2015-06-30 23:11:01 +00:00
console.info(msg);
2015-06-24 21:36:17 +00:00
});
if ('function' === typeof app) {
2015-06-30 23:11:01 +00:00
app = app(directive);
2015-06-24 21:36:17 +00:00
} else if ('function' === typeof app.create) {
2015-06-30 23:11:01 +00:00
app = app.create(directive);
2015-06-24 21:36:17 +00:00
}
Promise.resolve(app).then(function (app) {
server.on('request', app);
});
}
module.exports.createServer = createServer;
function run() {
var minimist = require('minimist');
var argv = minimist(process.argv.slice(2));
var port = argv.p || argv._[0] || 1443;
var pubdir = path.resolve(argv.d || argv._[1] || process.cwd());
2015-06-30 23:11:01 +00:00
var content = argv.c;
createServer(port, pubdir, content);
2015-06-24 21:36:17 +00:00
}
if (require.main === module) {
run();
}