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.

index.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var http = require("http");
  2. var https = require("https");
  3. var fs = require("fs");
  4. var Context = require("./lib/context.js");
  5. var Db = require("./lib/db.js");
  6. var conf = JSON.parse(fs.readFileSync("conf.json"));
  7. var endpoints = {
  8. //General files
  9. "/favicon.ico": "favicon.ico",
  10. "/global.css": "global.css",
  11. "/global.js": "global.js",
  12. "/404": "404.html",
  13. //Index files
  14. "/": "index/index.node.js",
  15. "/index/script.js": "index/script.js",
  16. "/index/style.css": "index/style.css",
  17. //Viewer files
  18. "/viewer": "viewer/index.node.js",
  19. "/viewer/script.js": "viewer/script.js",
  20. "/viewer/style.css": "viewer/style.css"
  21. }
  22. //Prepare endpoints
  23. var errs = false;
  24. Object.keys(endpoints).forEach(function(i) {
  25. try {
  26. //The endpoint is a function if the file ends with .node.js
  27. if (/\.node\.js$/.test(endpoints[i])) {
  28. endpoints[i] = require("./"+conf.webroot+"/"+endpoints[i]);
  29. //If it doesn't end with .node.js, it's a regular text file and will
  30. //just be served as is
  31. } else {
  32. endpoints[i] = fs.readFileSync(conf.webroot+"/"+endpoints[i]);
  33. }
  34. //Errors will usually be because an endpoint doesn't exist
  35. } catch (err) {
  36. if (err.code == "ENOENT") {
  37. console.log(err.toString());
  38. errs = true;
  39. } else {
  40. throw err;
  41. }
  42. }
  43. });
  44. //No need to proceed if some endpoints don't exist
  45. if (errs) process.exit();
  46. //Prepare all templates
  47. var templates = {};
  48. fs.readdirSync("templates").forEach(function(f) {
  49. templates[f.replace(/\.html$/, "")] = fs.readFileSync("templates/"+f, "utf8");
  50. });
  51. //Prepare all views
  52. var views = {};
  53. fs.readdirSync("views").forEach(function(f) {
  54. views[f.replace(/\.html$/, "")] = fs.readFileSync("views/"+f, "utf8");
  55. });
  56. //Function to run on each request
  57. function onRequest(req, res) {
  58. console.log("Request for "+req.url);
  59. var ep = endpoints[req.url];
  60. //If the file doesn't exist, we 404.
  61. if (!ep) {
  62. ep = endpoints["/404"];
  63. res.writeHead(404);
  64. }
  65. //Execute if it's a .node.js, or just respond with the contents of the file
  66. if (typeof ep == "function") {
  67. ep(new Context({
  68. req: req,
  69. res: res,
  70. templates: templates,
  71. views: views,
  72. conf: conf
  73. }));
  74. } else {
  75. res.end(ep);
  76. }
  77. }
  78. //Initiate a postgresql client
  79. var db = new Db(conf.db, function(err) {
  80. if (err) throw err;
  81. //Create HTTP or HTTPS server
  82. var server;
  83. if (conf.use_https) {
  84. server = https.createServer(conf.https, onRequest);
  85. } else {
  86. server = http.createServer(onRequest);
  87. }
  88. server.listen(conf.port);
  89. console.log("Listening on port "+conf.port+".");
  90. });