Simple image host.
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.

loader.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var fs = require("fs");
  2. var zlib = require("zlib");
  3. var browserPrefix = require("browser-prefix");
  4. var log = require("mlogger");
  5. var minify = require("./minify.js");
  6. var includeHtml = require("./includeHtml.js");
  7. exports.load = function(endpoints, conf) {
  8. var res = {
  9. endpoints: {},
  10. templates: {},
  11. views: {}
  12. }
  13. //Prepare endpoints
  14. var errs = false;
  15. var eps = Object.keys(endpoints).map(function(i) {
  16. var ep = {
  17. path: endpoints[i],
  18. url: i
  19. }
  20. try {
  21. //The endpoint is a function if the file ends with .node.js
  22. if (/\.node\.js$/.test(ep.path)) {
  23. ep.func = require("../"+conf.dir.web+"/"+ep.path);
  24. return ep;
  25. //If it doesn't end with .node.js, it's a regular text file and will
  26. //just be served as is
  27. } else {
  28. ep.str = fs.readFileSync(conf.dir.web+"/"+ep.path, "utf8");
  29. //Add browser prefixes
  30. if (/\.css$/.test(ep.path)) {
  31. ep.str = browserPrefix(ep.str);
  32. ep.mimeType = "text/css";
  33. if (conf.minify) ep.str = minify.css(ep.str);
  34. } else if (/\.html$/.test(ep.path)) {
  35. ep.mimeType = "text/html";
  36. if (conf.minify) ep.str = minify.html(ep.str);
  37. } else if (/\.js$/.test(ep.path)) {
  38. ep.mimeType = "application/javascript";
  39. if (conf.minify) ep.str = minify.js(ep.str);
  40. }
  41. return ep;
  42. }
  43. //Errors will usually be because an endpoint doesn't exist
  44. } catch (err) {
  45. if (err.code === "ENOENT" || err.code === "MODULE_NOT_FOUND") {
  46. log.error("File not found: "+conf.dir.web+"/"+ep.path);
  47. errs = true;
  48. } else {
  49. throw err;
  50. }
  51. }
  52. });
  53. //No need to proceed if some endpoints don't exist
  54. if (errs) process.exit();
  55. eps.forEach(function(ep) {
  56. res.endpoints[ep.url] = ep;
  57. });
  58. //Prepare all templates
  59. var templates = {};
  60. fs.readdirSync("templates").forEach(function(f) {
  61. var name = f.replace(/\.html$/, "");
  62. res.templates[name] = includeHtml("templates/"+f, conf);
  63. });
  64. //Prepare all views
  65. var views = {};
  66. fs.readdirSync("views").forEach(function(f) {
  67. var name = f.replace(/\.html$/, "");
  68. res.views[name] = includeHtml("views/"+f, conf);
  69. });
  70. return res;
  71. }