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.

129 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. var dope = require("console-dope"),
  4. connect = require("connect"),
  5. http = require("http"),
  6. fs = require("fs"),
  7. Model = require("nature").Model,
  8. w = require("wodge"),
  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. var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny] [--compress|-c]";
  16. function halt(message){
  17. dope.red.log("Error: %s", message);
  18. dope.log(usage);
  19. process.exit(1);
  20. }
  21. /* parse command-line args */
  22. var argv = new Model()
  23. .define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
  24. .define({ name: "log-format", alias: "f", type: "string" })
  25. .define({ name: "help", alias: "h", type: "boolean" })
  26. .define({ name: "directory", alias: "d", type: "string", value: process.cwd() })
  27. .define({ name: "compress", alias: "c", type: "boolean" })
  28. .on("error", function(err){
  29. halt(err.message);
  30. });
  31. /* Merge together options from "~/.local-web-server.json", "{cwd}/.local-web-server.json" and "{cwd}/package.json", in that order. */
  32. var storedConfig = loadConfig(
  33. path.join(w.getHomeDir(), ".local-web-server.json"),
  34. path.join(process.cwd(), ".local-web-server.json"),
  35. path.join(process.cwd(), "package.json:local-web-server")
  36. );
  37. argv.set(storedConfig);
  38. /* Finally, set the options from the command-line, overriding all defaults. */
  39. argv.set(process.argv);
  40. /* Die here if invalid args received */
  41. if (!argv.valid) halt(argv.validationMessages);
  42. if (argv.help){
  43. dope.log(usage);
  44. } else {
  45. var total = {
  46. req: 0,
  47. bytes: 0,
  48. connections: 0
  49. };
  50. process.on("SIGINT", function(){
  51. dope.showCursor();
  52. dope.log();
  53. process.exit(0);
  54. });
  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. var app = connect();
  62. /* log using --log-format (if supplied), else output statics */
  63. if(argv["log-format"]){
  64. app.use(morgan(argv["log-format"]));
  65. } else {
  66. app.use(function(req, res, next){
  67. dope.column(1).write(++total.req);
  68. next();
  69. });
  70. }
  71. /* --compress enables compression */
  72. if (argv.compress) app.use(compress());
  73. /* static file server including directory browsing support */
  74. app.use(serveStatic(path.resolve(argv.directory)))
  75. .use(directory(path.resolve(argv.directory), { icons: true }));
  76. /* launch server */
  77. var server = http.createServer(app)
  78. .on("error", function(err){
  79. if (err.code === "EADDRINUSE"){
  80. halt("port " + argv.port + " is already is use");
  81. } else {
  82. halt(err.message);
  83. }
  84. })
  85. .listen(argv.port);
  86. /* write status to stderr so stdout can be piped to disk ($ ws > log.txt) */
  87. if (path.resolve(argv.directory) === process.cwd()){
  88. dope.error("serving at %underline{%s}", "http://localhost:" + argv.port);
  89. } else {
  90. dope.error("serving %underline{%s} at %underline{%s}", argv.directory, "http://localhost:" + argv.port);
  91. }
  92. /* in stats mode, monitor connections and bytes transferred */
  93. if (!argv["log-format"]){
  94. dope.hideCursor();
  95. dope.log("%underline{Requests} %underline{Data} %underline{Connections}");
  96. server.on("connection", function(socket){
  97. var oldWrite = socket.write;
  98. socket.write = function(data) {
  99. if (!Buffer.isBuffer(data)) {
  100. data = new Buffer(data);
  101. }
  102. oldWrite.call(this, data);
  103. total.bytes += data.length;
  104. dope.column(12).write(w.padRight(w.bytesToSize(total.bytes, 2), 12));
  105. };
  106. dope.column(24).write(++total.connections);
  107. socket.on("close", function(){
  108. dope.column(24).write(w.padRight(--total.connections));
  109. });
  110. });
  111. }
  112. }