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.

155 lines
4.4 KiB

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
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. fs = require("fs"),
  7. Thing = require("nature").Thing,
  8. w = require("wodge"),
  9. path = require("path");
  10. var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny] [--compress|-c]";
  11. function halt(message){
  12. dope.red.log("Error: %s", message);
  13. dope.log(usage);
  14. process.exit(1);
  15. }
  16. /**
  17. parse command-line args
  18. */
  19. var argv = new Thing()
  20. .define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
  21. .define({ name: "log-format", alias: "f", type: "string" })
  22. .define({ name: "help", alias: "h", type: "boolean" })
  23. .define({ name: "directory", alias: "d", type: "string", value: process.cwd() })
  24. .define({ name: "compress", alias: "c", type: "boolean" })
  25. .on("error", function(err){
  26. halt(err.message);
  27. });
  28. /*
  29. Set default options from "package.json", ".local-web-server.json" or "~/.local-web-server.json", in that order
  30. */
  31. var pkgPath = path.join(process.cwd(), "package.json"),
  32. lwsPath = path.join(process.cwd(), ".local-web-server.json"),
  33. homePath = path.join(w.getHomeDir(), ".local-web-server.json");
  34. if (fs.existsSync(pkgPath)){
  35. argv.set(require(pkgPath)["local-web-server"]);
  36. }
  37. if (fs.existsSync(lwsPath)){
  38. argv.set(require(lwsPath));
  39. }
  40. if (fs.existsSync(homePath)){
  41. argv.set(require(homePath));
  42. }
  43. /*
  44. Finally, set the options from the command-line, overriding all defaults.
  45. */
  46. argv.set(process.argv);
  47. /**
  48. Die here if invalid args received
  49. */
  50. if (!argv.valid) halt(argv.validationMessages);
  51. /**
  52. $ ws --help
  53. */
  54. if (argv.help){
  55. dope.log(usage);
  56. } else {
  57. var total = {
  58. req: 0,
  59. bytes: 0,
  60. connections: 0
  61. };
  62. process.on("SIGINT", function(){
  63. dope.showCursor();
  64. dope.log();
  65. process.exit(0);
  66. });
  67. /**
  68. customised connect.logger :date token, purely to satisfy Logstalgia.
  69. */
  70. connect.logger.token("date", function(){
  71. var a = new Date();
  72. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  73. .replace("GMT", "").replace(" (BST)", "");
  74. });
  75. var app = connect();
  76. /*
  77. log using --log-format (if supplied), else output statics
  78. */
  79. if(argv["log-format"]){
  80. app.use(connect.logger(argv["log-format"]));
  81. } else {
  82. app.use(function(req, res, next){
  83. dope.column(1).write(++total.req);
  84. next();
  85. });
  86. }
  87. /**
  88. --compress enables compression
  89. */
  90. if (argv.compress) app.use(connect.compress());
  91. /**
  92. static file server including directory browsing support
  93. */
  94. app.use(connect.static(path.resolve(argv.directory)))
  95. .use(connect.directory(path.resolve(argv.directory), { icons: true }));
  96. /**
  97. launch server
  98. */
  99. var server = http.createServer(app)
  100. .on("error", function(err){
  101. if (err.code === "EADDRINUSE"){
  102. halt("port " + argv.port + " is already is use");
  103. } else {
  104. halt(err.message);
  105. }
  106. })
  107. .listen(argv.port);
  108. /*
  109. write status to stderr so stdout can be piped to disk ($ ws > log.txt)
  110. */
  111. if (path.resolve(argv.directory) === process.cwd()){
  112. dope.error("serving at %underline{%s}", "http://localhost:" + argv.port);
  113. } else {
  114. dope.error("serving %underline{%s} at %underline{%s}", argv.directory, "http://localhost:" + argv.port);
  115. }
  116. /**
  117. in stats mode, monitor connections and bytes transferred
  118. */
  119. if (!argv["log-format"]){
  120. dope.hideCursor();
  121. dope.log("%underline{Requests} %underline{Data} %underline{Connections}");
  122. server.on("connection", function(socket){
  123. var oldWrite = socket.write;
  124. socket.write = function(data) {
  125. if (!Buffer.isBuffer(data)) {
  126. data = new Buffer(data);
  127. }
  128. oldWrite.call(this, data);
  129. total.bytes += data.length;
  130. dope.column(12).write(w.padRight(w.bytesToSize(total.bytes, 2), 12));
  131. };
  132. dope.column(24).write(++total.connections);
  133. socket.on("close", function(){
  134. dope.column(24).write(w.padRight(--total.connections));
  135. });
  136. });
  137. }
  138. }