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.

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