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

163 lines
4.8 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");
2015-08-09 15:08:35 +01:00
var url = require("url");
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-08-14 09:26:31 +01:00
title: "local-web-server",
2015-07-09 09:38:37 +01:00
description: "Lightweight static web server, zero configuration.",
2015-08-09 19:58:23 +01:00
footer: "Project home: [underline]{https://github.com/75lb/local-web-server}",
2015-08-09 15:08:35 +01:00
usage: {
forms: [
"$ ws <server options>",
"$ ws --config",
"$ ws --help"
]
},
2015-06-30 21:18:34 +01:00
groups: {
2015-08-14 09:26:31 +01:00
server: "Server",
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 {
2015-08-09 15:08:35 +01:00
var wsOptions = cli.parse();
2014-06-17 00:40:41 +01:00
} 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-08-09 15:08:35 +01:00
wsOptions.server = o.extend(builtInDefaults, storedConfig, wsOptions.server);
2014-02-09 08:40:32 +01:00
/* user input validation */
2015-08-09 15:08:35 +01:00
if (!t.isNumber(wsOptions.server.port)) {
halt("please supply a numeric port value");
}
2015-08-09 15:08:35 +01:00
if (wsOptions.misc.config){
2014-05-08 15:05:06 +02:00
dope.log("Stored config: ");
dope.log(storedConfig);
process.exit(0);
2015-08-09 15:08:35 +01:00
} else if (wsOptions.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
2015-08-09 15:08:35 +01:00
dope.hideCursor();
launchServer();
/* write launch information to stderr (stdout is reserved for web log output) */
if (path.resolve(wsOptions.server.directory) === process.cwd()){
dope.error("serving at %underline{%s}", "http://localhost:" + wsOptions.server.port);
} else {
dope.error("serving %underline{%s} at %underline{%s}", wsOptions.server.directory, "http://localhost:" + wsOptions.server.port);
}
}
function halt(message){
dope.red.log("Error: %s", message);
dope.log(usage);
process.exit(1);
}
function launchServer(){
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();
});
2015-08-09 15:08:35 +01:00
app.use(getLogger());
/* --compress enables compression */
if (wsOptions.server.compress) app.use(compress());
/* set the mime-type overrides specified in the config */
serveStatic.mime.define(wsOptions.server.mime);
/* enable static file server, including directory browsing support */
app.use(serveStatic(path.resolve(wsOptions.server.directory)))
.use(directory(path.resolve(wsOptions.server.directory), { icons: true }));
/* launch server */
http.createServer(app)
.on("error", function(err){
if (err.code === "EADDRINUSE"){
halt("port " + wsOptions.server.port + " is already is use");
} else {
halt(err.message);
}
})
.listen(wsOptions.server.port);
}
function getLogger(){
2014-06-12 19:37:12 +01:00
/* log using --log-format (if supplied) */
2015-08-09 15:08:35 +01:00
var logFormat = wsOptions.server["log-format"];
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(){
2015-08-09 15:08:35 +01:00
var d = new Date();
return (d.getDate() + "/" + d.getUTCMonth() + "/" + d.getFullYear() + ":" + d.toTimeString())
2014-06-15 19:43:35 +01:00
.replace("GMT", "").replace(" (BST)", "");
});
2014-08-11 10:05:33 +02:00
logFormat = "combined";
2014-06-15 19:43:35 +01:00
}
2015-08-09 15:08:35 +01:00
return 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 {
2015-08-09 15:08:35 +01:00
return morgan("common", { stream: logStats({ refreshRate: wsOptions.server["refresh-rate"] }) });
2014-02-12 10:52:07 +01:00
}
2013-06-15 00:41:43 +01:00
}