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.

124 lines
3.7 KiB

11 years ago
11 years ago
10 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
10 years ago
11 years ago
10 years ago
11 years ago
10 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. connect = require("connect"),
  5. http = require("http"),
  6. cliArgs = require("command-line-args"),
  7. o = require("object-ting"),
  8. s = require("string-ting"),
  9. path = require("path"),
  10. loadConfig = require("config-master"),
  11. morgan = require("morgan"),
  12. serveStatic = require("serve-static"),
  13. directory = require("serve-index"),
  14. compress = require("compression"),
  15. homePath = require("home-path"),
  16. byteSize = require("byte-size"),
  17. clf = require("common-log-format"),
  18. logStats = require("stream-log-stats");
  19. var usage =
  20. "usage: \n\
  21. $ ws [--directory|-d <dir>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny] [--compress|-c]\n\
  22. $ ws --config\n\
  23. $ ws --help|-h";
  24. function halt(message){
  25. dope.red.log("Error: %s", message);
  26. dope.log(usage);
  27. process.exit(1);
  28. }
  29. /* Merge together options from
  30. - ~/.local-web-server.json
  31. - {cwd}/.local-web-server.json
  32. - {cwd}/package.json
  33. */
  34. var storedConfig = loadConfig(
  35. path.join(homePath(), ".local-web-server.json"),
  36. path.join(process.cwd(), ".local-web-server.json"),
  37. { jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" }
  38. );
  39. /* override stored config with values parsed from command line */
  40. try {
  41. var argv = cliArgs([
  42. { name: "port", alias: "p", type: Number, defaultOption: true, value: 8000 },
  43. { name: "log-format", alias: "f", type: String },
  44. { name: "help", alias: "h", type: Boolean },
  45. { name: "directory", alias: "d", type: String, value: process.cwd() },
  46. { name: "config", type: Boolean },
  47. { name: "logstalgia", type: Boolean },
  48. { name: "compress", alias: "c", type: Boolean }
  49. ]).parse();
  50. } catch(err){
  51. halt(err.message);
  52. }
  53. argv = o.extend(storedConfig, argv);
  54. if (argv.logstalgia){
  55. /* customised logger :date token, purely to satisfy Logstalgia. */
  56. morgan.token("date", function(){
  57. var a = new Date();
  58. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  59. .replace("GMT", "").replace(" (BST)", "");
  60. });
  61. }
  62. if (argv.config){
  63. dope.log("Stored config: ");
  64. dope.log(storedConfig);
  65. process.exit(0);
  66. } else if (argv.help){
  67. dope.log(usage);
  68. } else {
  69. var total = {
  70. req: 0,
  71. bytes: 0,
  72. connections: 0
  73. };
  74. process.on("SIGINT", function(){
  75. dope.showCursor();
  76. dope.log();
  77. process.exit(0);
  78. });
  79. var app = connect();
  80. /* log using --log-format (if supplied) */
  81. if(argv["log-format"]) {
  82. app.use(morgan(argv["log-format"]));
  83. } else {
  84. var statStream = clf();
  85. statStream.pipe(logStats());
  86. app.use(morgan({ stream: statStream }));
  87. }
  88. /* --compress enables compression */
  89. if (argv.compress) app.use(compress());
  90. /* static file server including directory browsing support */
  91. app.use(serveStatic(path.resolve(argv.directory)))
  92. .use(directory(path.resolve(argv.directory), { icons: true }));
  93. /* launch server */
  94. var server = http.createServer(app)
  95. .on("error", function(err){
  96. if (err.code === "EADDRINUSE"){
  97. halt("port " + argv.port + " is already is use");
  98. } else {
  99. halt(err.message);
  100. }
  101. })
  102. .listen(argv.port);
  103. /* write status to stderr so stdout can be piped to disk ($ ws > log.txt) */
  104. if (path.resolve(argv.directory) === process.cwd()){
  105. dope.error("serving at %underline{%s}", "http://localhost:" + argv.port);
  106. } else {
  107. dope.error("serving %underline{%s} at %underline{%s}", argv.directory, "http://localhost:" + argv.port);
  108. }
  109. }