Pictures!
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.

server.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var fs = require("fs");
  2. var http = require("http");
  3. var crypto = require("crypto");
  4. var pathlib = require("path");
  5. var urllib = require("url");
  6. var Slideshow = require("./js/slideshow");
  7. var fileserver = require("./js/fileserver");
  8. var admin = require("./js/admin");
  9. var error = require("./js/error");
  10. var conf = JSON.parse(fs.readFileSync("conf.json"));
  11. var index = fs.readFileSync("web/index.html", "utf-8")
  12. .replace(/<<transition_time>>/g, conf.transition_time);
  13. var slideshow = Slideshow(conf.slides, conf.interval);
  14. admin.init(slideshow);
  15. function onexit(code) {
  16. console.log("exiting", code);
  17. slideshow.sendEvent("reload");
  18. process.exit();
  19. }
  20. process.on("exit", onexit);
  21. process.on("SIGINT", onexit);
  22. process.on("SIGTERM", onexit);
  23. process.on("uncaughtException", onexit);
  24. function handler(req, res) {
  25. var parts = urllib.parse(req.url);
  26. var pathname = parts.pathname;
  27. // Serve index.html (not part of fileserver because the
  28. // substitution of <<transition_time>>)
  29. if (pathname === "/") {
  30. res.end(index);
  31. // Send various files from web/ to the client
  32. } else if (fileserver.canServe(parts)) {
  33. fileserver.serve(parts, res);
  34. // /init: send initial information about current slide
  35. } else if (pathname === "/init") {
  36. res.end(slideshow.getSlideName());
  37. // /await: long polling, request won't end before a new slide comes
  38. } else if (pathname === "/await") {
  39. slideshow.pushAwaiter(res);
  40. // /admin/*: respond to admin queries
  41. } else if (admin.canServe(parts)) {
  42. admin.serve(parts, conf, req, res);
  43. // Serve slide files
  44. } else {
  45. slideshow.serve(parts, res);
  46. }
  47. }
  48. var server = http.createServer(handler);
  49. server.on("error", err => {
  50. console.error(err);
  51. system.exit(1);
  52. });
  53. server.listen(conf.port);
  54. console.log("Server running on port "+conf.port+".");