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.

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