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.

144 lines
4.3 KiB

11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
10 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
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env node
  2. "use strict";
  3. var dope = require("console-dope"),
  4. http = require("http"),
  5. cliArgs = require("command-line-args"),
  6. o = require("object-tools"),
  7. t = require("typical"),
  8. path = require("path"),
  9. loadConfig = require("config-master"),
  10. homePath = require("home-path"),
  11. logStats = require("stream-log-stats"),
  12. connect = require("connect"),
  13. morgan = require("morgan"),
  14. serveStatic = require("serve-static"),
  15. directory = require("serve-index"),
  16. compress = require("compression"),
  17. cliOptions = require("../lib/cli-options");
  18. /* specify the command line arg definitions and usage forms */
  19. var cli = cliArgs(cliOptions);
  20. var usage = cli.getUsage({
  21. forms: [
  22. "$ ws <server options>",
  23. "$ ws --config",
  24. "$ ws --help"
  25. ]
  26. });
  27. /* parse command line args */
  28. try {
  29. var argv = cli.parse();
  30. } catch(err){
  31. halt(err.message);
  32. }
  33. /* Load and merge together options from
  34. - ~/.local-web-server.json
  35. - {cwd}/.local-web-server.json
  36. - the `local-web-server` property of {cwd}/package.json
  37. */
  38. var storedConfig = loadConfig(
  39. path.join(homePath(), ".local-web-server.json"),
  40. path.join(process.cwd(), ".local-web-server.json"),
  41. { jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" }
  42. );
  43. var builtInDefaults = {
  44. port: 8000,
  45. directory: process.cwd(),
  46. "refresh-rate": 500,
  47. mime: {}
  48. };
  49. /* override built-in defaults with stored config and then command line args */
  50. argv.Server = o.extend(builtInDefaults, storedConfig, argv.Server);
  51. /* user input validation */
  52. var logFormat = argv.Server["log-format"];
  53. if (!t.isNumber(argv.Server.port)) {
  54. halt("please supply a numeric port value");
  55. }
  56. if (argv.Misc.config){
  57. dope.log("Stored config: ");
  58. dope.log(storedConfig);
  59. process.exit(0);
  60. } else if (argv.Misc.help){
  61. dope.log(usage);
  62. } else {
  63. process.on("SIGINT", function(){
  64. dope.showCursor();
  65. dope.log();
  66. process.exit(0);
  67. });
  68. var app = connect();
  69. /* enable cross-origin requests on all resources */
  70. app.use(function(req, res, next){
  71. res.setHeader("Access-Control-Allow-Origin", "*");
  72. next();
  73. });
  74. /* log using --log-format (if supplied) */
  75. if(logFormat) {
  76. if (logFormat === "none"){
  77. // do nothing, no logging required
  78. } else {
  79. if (logFormat === "logstalgia"){
  80. /* customised logger :date token, purely to satisfy Logstalgia. */
  81. morgan.token("date", function(){
  82. var a = new Date();
  83. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  84. .replace("GMT", "").replace(" (BST)", "");
  85. });
  86. logFormat = "combined";
  87. }
  88. app.use(morgan(logFormat));
  89. }
  90. /* if no `--log-format` was specified, pipe the default format output
  91. into `log-stats`, which prints statistics to the console */
  92. } else {
  93. dope.hideCursor();
  94. app.use(morgan("common", { stream: logStats({ refreshRate: argv.Server["refresh-rate"] }) }));
  95. }
  96. /* --compress enables compression */
  97. if (argv.Server.compress) app.use(compress());
  98. /* set the mime-type overrides specified in the config */
  99. serveStatic.mime.define(argv.Server.mime);
  100. /* enable static file server, including directory browsing support */
  101. app.use(serveStatic(path.resolve(argv.Server.directory)))
  102. .use(directory(path.resolve(argv.Server.directory), { icons: true }));
  103. /* launch server */
  104. http.createServer(app)
  105. .on("error", function(err){
  106. if (err.code === "EADDRINUSE"){
  107. halt("port " + argv.Server.port + " is already is use");
  108. } else {
  109. halt(err.message);
  110. }
  111. })
  112. .listen(argv.Server.port);
  113. /* write launch information to stderr (stdout is reserved for web log output) */
  114. if (path.resolve(argv.Server.directory) === process.cwd()){
  115. dope.error("serving at %underline{%s}", "http://localhost:" + argv.Server.port);
  116. } else {
  117. dope.error("serving %underline{%s} at %underline{%s}", argv.Server.directory, "http://localhost:" + argv.Server.port);
  118. }
  119. }
  120. function halt(message){
  121. dope.red.log("Error: %s", message);
  122. dope.log(usage);
  123. process.exit(1);
  124. }