You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.1 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. #!/usr/bin/env node
  2. "use strict";
  3. var dope = require("console-dope"),
  4. http = require("http"),
  5. cliArgs = require("command-line-args"),
  6. o = require("object-tools"),
  7. path = require("path"),
  8. loadConfig = require("config-master"),
  9. homePath = require("home-path"),
  10. logStats = require("stream-log-stats"),
  11. connect = require("connect"),
  12. morgan = require("morgan"),
  13. serveStatic = require("serve-static"),
  14. directory = require("serve-index"),
  15. compress = require("compression"),
  16. cliOptions = require("../lib/cli-options");
  17. /* specify the command line arg definitions and usage forms */
  18. var cli = cliArgs(cliOptions);
  19. var usage = cli.usage({
  20. forms: [
  21. "$ ws <server options>",
  22. "$ ws --config",
  23. "$ ws --help"
  24. ]
  25. });
  26. /* parse command line args */
  27. try {
  28. var argv = cli.parse();
  29. } catch(err){
  30. halt(err.message);
  31. }
  32. /* Load and merge together options from
  33. - ~/.local-web-server.json
  34. - {cwd}/.local-web-server.json
  35. - the `local-web-server` property of {cwd}/package.json
  36. */
  37. var storedConfig = loadConfig(
  38. path.join(homePath(), ".local-web-server.json"),
  39. path.join(process.cwd(), ".local-web-server.json"),
  40. { jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" }
  41. );
  42. var builtInDefaults = {
  43. port: 8000,
  44. directory: process.cwd(),
  45. refreshRate: 500
  46. };
  47. /* override built-in defaults with stored config and then command line args */
  48. argv.Server = o.extend(builtInDefaults, storedConfig, argv.Server);
  49. if (argv.Misc.config){
  50. dope.log("Stored config: ");
  51. dope.log(storedConfig);
  52. process.exit(0);
  53. } else if (argv.Misc.help){
  54. dope.log(usage);
  55. } else {
  56. process.on("SIGINT", function(){
  57. dope.showCursor();
  58. dope.log();
  59. process.exit(0);
  60. });
  61. var app = connect();
  62. /* enable cross-origin requests on all resources */
  63. app.use(function(req, res, next){
  64. res.setHeader("Access-Control-Allow-Origin", "*");
  65. next();
  66. });
  67. /* log using --log-format (if supplied) */
  68. var logFormat = argv.Server["log-format"];
  69. if(logFormat) {
  70. if (logFormat === "none"){
  71. // do nothing, no logging required
  72. } else {
  73. if (logFormat === "logstalgia"){
  74. /* customised logger :date token, purely to satisfy Logstalgia. */
  75. morgan.token("date", function(){
  76. var a = new Date();
  77. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  78. .replace("GMT", "").replace(" (BST)", "");
  79. });
  80. logFormat = "default";
  81. }
  82. app.use(morgan(logFormat));
  83. }
  84. /* if no `--log-format` was specified, pipe the default format output
  85. into `log-stats`, which prints statistics to the console */
  86. } else {
  87. dope.hideCursor();
  88. app.use(morgan({ stream: logStats({ refreshRate: argv.Server.refreshRate }) }));
  89. }
  90. /* --compress enables compression */
  91. if (argv.Server.compress) app.use(compress());
  92. /* enable static file server, including directory browsing support */
  93. app.use(serveStatic(path.resolve(argv.Server.directory)))
  94. .use(directory(path.resolve(argv.Server.directory), { icons: true }));
  95. /* launch server */
  96. http.createServer(app)
  97. .on("error", function(err){
  98. if (err.code === "EADDRINUSE"){
  99. halt("port " + argv.Server.port + " is already is use");
  100. } else {
  101. halt(err.message);
  102. }
  103. })
  104. .listen(argv.Server.port);
  105. /* write launch information to stderr (stdout is reserved for web log output) */
  106. if (path.resolve(argv.Server.directory) === process.cwd()){
  107. dope.error("serving at %underline{%s}", "http://localhost:" + argv.Server.port);
  108. } else {
  109. dope.error("serving %underline{%s} at %underline{%s}", argv.Server.directory, "http://localhost:" + argv.Server.port);
  110. }
  111. }
  112. function halt(message){
  113. dope.red.log("Error: %s", message);
  114. dope.log(usage);
  115. process.exit(1);
  116. }