Files
hiring-test-one/bin/ws.js

152 lines
4.6 KiB
JavaScript
Raw Normal View History

2014-02-05 15:28:23 +01:00
#!/usr/bin/env node
2014-02-12 10:52:07 +01:00
"use strict";
2015-06-30 21:18:34 +01:00
var dope = require("console-dope");
var http = require("http");
var cliArgs = require("command-line-args");
var o = require("object-tools");
var t = require("typical");
var path = require("path");
var loadConfig = require("config-master");
var homePath = require("home-path");
var logStats = require("stream-log-stats");
var connect = require("connect");
var morgan = require("morgan");
var serveStatic = require("serve-static");
var directory = require("serve-index");
var compress = require("compression");
var cliOptions = require("../lib/cli-options");
2014-06-17 00:40:41 +01:00
2014-06-17 09:52:26 +01:00
/* specify the command line arg definitions and usage forms */
2014-06-17 00:40:41 +01:00
var cli = cliArgs(cliOptions);
2014-08-11 10:05:33 +02:00
var usage = cli.getUsage({
2015-06-30 21:18:34 +01:00
title: "local-web-server",
2015-07-09 09:38:37 +01:00
description: "Lightweight static web server, zero configuration.",
2015-06-30 21:18:34 +01:00
footer: "Project home: https://github.com/75lb/local-web-server",
2014-06-17 00:40:41 +01:00
forms: [
"$ ws <server options>",
"$ ws --config",
"$ ws --help"
2015-06-30 21:18:34 +01:00
],
groups: {
server: "Server",
2015-07-09 09:38:37 +01:00
misc: "Misc"
2015-06-30 21:18:34 +01:00
}
2014-06-17 00:40:41 +01:00
});
2013-06-04 12:45:30 +01:00
2014-06-17 00:40:41 +01:00
/* parse command line args */
try {
var argv = cli.parse();
} catch(err){
halt(err.message);
2014-02-12 10:52:07 +01:00
}
/* Load and merge together options from
2014-05-29 23:01:39 +01:00
- ~/.local-web-server.json
- {cwd}/.local-web-server.json
- the `local-web-server` property of {cwd}/package.json
2014-05-29 23:01:39 +01:00
*/
var storedConfig = loadConfig(
2014-06-07 22:21:51 +01:00
path.join(homePath(), ".local-web-server.json"),
path.join(process.cwd(), ".local-web-server.json"),
2014-06-13 17:20:25 +01:00
{ jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" }
);
2014-02-26 15:23:16 +01:00
2014-06-16 22:42:21 +01:00
var builtInDefaults = {
port: 8000,
directory: process.cwd(),
"refresh-rate": 500,
mime: {}
2014-06-16 22:42:21 +01:00
};
/* override built-in defaults with stored config and then command line args */
2015-06-30 21:18:34 +01:00
argv.server = o.extend(builtInDefaults, storedConfig, argv.server);
2014-02-09 08:40:32 +01:00
/* user input validation */
2015-06-30 21:18:34 +01:00
var logFormat = argv.server["log-format"];
if (!t.isNumber(argv.server.port)) {
halt("please supply a numeric port value");
}
2015-06-30 21:18:34 +01:00
if (argv.misc.config){
2014-05-08 15:05:06 +02:00
dope.log("Stored config: ");
dope.log(storedConfig);
process.exit(0);
2015-06-30 21:18:34 +01:00
} else if (argv.misc.help){
2014-03-12 14:34:52 +01:00
dope.log(usage);
2013-06-15 00:41:43 +01:00
} else {
process.on("SIGINT", function(){
2014-03-12 14:34:52 +01:00
dope.showCursor();
dope.log();
process.exit(0);
});
2014-06-17 10:27:13 +01:00
2014-02-10 21:13:13 +01:00
var app = connect();
2014-06-17 10:27:13 +01:00
/* enable cross-origin requests on all resources */
app.use(function(req, res, next){
res.setHeader("Access-Control-Allow-Origin", "*");
next();
});
2014-06-12 19:37:12 +01:00
/* log using --log-format (if supplied) */
2014-06-16 22:42:21 +01:00
if(logFormat) {
if (logFormat === "none"){
2014-06-15 19:43:35 +01:00
// do nothing, no logging required
} else {
2014-06-16 22:42:21 +01:00
if (logFormat === "logstalgia"){
2014-06-15 19:43:35 +01:00
/* customised logger :date token, purely to satisfy Logstalgia. */
morgan.token("date", function(){
var a = new Date();
return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
.replace("GMT", "").replace(" (BST)", "");
});
2014-08-11 10:05:33 +02:00
logFormat = "combined";
2014-06-15 19:43:35 +01:00
}
2014-06-16 22:42:21 +01:00
app.use(morgan(logFormat));
}
2014-06-16 22:42:21 +01:00
/* if no `--log-format` was specified, pipe the default format output
into `log-stats`, which prints statistics to the console */
} else {
2014-06-17 10:27:13 +01:00
dope.hideCursor();
2015-06-30 21:18:34 +01:00
app.use(morgan("common", { stream: logStats({ refreshRate: argv.server["refresh-rate"] }) }));
2014-02-10 21:13:13 +01:00
}
/* --compress enables compression */
2015-06-30 21:18:34 +01:00
if (argv.server.compress) app.use(compress());
2014-02-17 10:20:32 +00:00
/* set the mime-type overrides specified in the config */
2015-06-30 21:18:34 +01:00
serveStatic.mime.define(argv.server.mime);
2014-06-17 09:52:26 +01:00
/* enable static file server, including directory browsing support */
2015-06-30 21:18:34 +01:00
app.use(serveStatic(path.resolve(argv.server.directory)))
.use(directory(path.resolve(argv.server.directory), { icons: true }));
/* launch server */
http.createServer(app)
.on("error", function(err){
if (err.code === "EADDRINUSE"){
2015-06-30 21:18:34 +01:00
halt("port " + argv.server.port + " is already is use");
} else {
halt(err.message);
}
})
2015-06-30 21:18:34 +01:00
.listen(argv.server.port);
2013-06-15 00:41:43 +01:00
2014-06-16 22:42:21 +01:00
/* write launch information to stderr (stdout is reserved for web log output) */
2015-06-30 21:18:34 +01:00
if (path.resolve(argv.server.directory) === process.cwd()){
dope.error("serving at %underline{%s}", "http://localhost:" + argv.server.port);
2014-02-12 10:52:07 +01:00
} else {
2015-06-30 21:18:34 +01:00
dope.error("serving %underline{%s} at %underline{%s}", argv.server.directory, "http://localhost:" + argv.server.port);
2014-02-12 10:52:07 +01:00
}
2013-06-15 00:41:43 +01:00
}
2014-06-17 00:40:41 +01:00
function halt(message){
dope.red.log("Error: %s", message);
dope.log(usage);
process.exit(1);
}