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.

154 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
  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. .set(process.argv);
  37. /*
  38. Include any options from "package.json", ".local-web-server.json" or "~/.local-web-server.json", in that order
  39. */
  40. var pkgPath = path.join(process.cwd(), "package.json"),
  41. lwsPath = path.join(process.cwd(), ".local-web-server.json"),
  42. homePath = path.join(w.getHomeDir(), ".local-web-server.json");
  43. if (fs.existsSync(pkgPath)){
  44. argv.set(require(pkgPath)["local-web-server"]);
  45. }
  46. if (fs.existsSync(lwsPath)){
  47. argv.set(require(lwsPath));
  48. }
  49. if (fs.existsSync(homePath)){
  50. argv.set(require(homePath));
  51. }
  52. /**
  53. Die here if invalid args received
  54. */
  55. if (!argv.valid) halt(argv.validationMessages);
  56. /**
  57. $ ws --help
  58. */
  59. if (argv.help){
  60. console.log(usage);
  61. } else {
  62. var total = {
  63. req: 0,
  64. bytes: 0,
  65. connections: 0
  66. };
  67. process.on("SIGINT", function(){
  68. console.showCursor();
  69. console.log();
  70. process.exit(0);
  71. });
  72. /**
  73. customised connect.logger :date token, purely to satisfy Logstalgia.
  74. */
  75. connect.logger.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. var app = connect();
  81. /*
  82. log using --log-format (if supplied), else output statics
  83. */
  84. if(argv["log-format"]){
  85. app.use(connect.logger(argv["log-format"]));
  86. } else {
  87. app.use(function(req, res, next){
  88. console.column(1).write(++total.req);
  89. next();
  90. });
  91. }
  92. /**
  93. --compress enables compression
  94. */
  95. if (argv.compress) app.use(connect.compress());
  96. /**
  97. static file server including directory browsing support
  98. */
  99. app.use(connect.static(argv.directory))
  100. .use(connect.directory(argv.directory, { icons: true }));
  101. /**
  102. launch server
  103. */
  104. var server = http.createServer(app)
  105. .on("error", handleServerError)
  106. .listen(argv.port);
  107. /*
  108. write status to stderr so stdout can be piped to disk ($ ws > log.txt)
  109. */
  110. if (argv.directory === process.cwd()){
  111. console.error("serving at %u{%s}", "http://localhost:" + argv.port);
  112. } else {
  113. console.error("serving %u{%s} at %u{%s}", argv.directory, "http://localhost:" + argv.port);
  114. }
  115. /**
  116. in stats mode, monitor connections and bytes transferred
  117. */
  118. if (!argv["log-format"]){
  119. console.hideCursor();
  120. console.log("%u{Requests} %u{Data} %u{Connections}");
  121. server.on("connection", function(socket){
  122. var oldWrite = socket.write;
  123. socket.write = function(data) {
  124. if (!Buffer.isBuffer(data)) {
  125. data = new Buffer(data);
  126. }
  127. oldWrite.call(this, data);
  128. total.bytes += data.length;
  129. console.column(12).write((w.bytesToSize(total.bytes, 2) + " ").slice(0,12));
  130. };
  131. console.column(24).write(++total.connections);
  132. socket.on("close", function(){
  133. console.column(24).write((--total.connections + " ").slice(0,12));
  134. });
  135. });
  136. }
  137. }