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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var http = require("http");
  2. var https = require("https");
  3. var fs = require("fs");
  4. var domain = require("domain");
  5. var loader = require("./lib/loader.js");
  6. var pg = require("pg");
  7. var Context = require("./lib/context.js");
  8. var conf = JSON.parse(fs.readFileSync("conf.json"));
  9. var endpoints = {
  10. //General
  11. "/favicon.ico": "favicon.ico",
  12. "/global.css": "global.css",
  13. "/global.js": "global.js",
  14. "/404": "404.node.js",
  15. //Index
  16. "/": "index/index.node.js",
  17. "/index/script.js": "index/script.js",
  18. "/index/style.css": "index/style.css",
  19. //Register
  20. "/register": "register/index.node.js",
  21. "/register/style.css": "register/style.css",
  22. "/register/script.js": "register/script.js",
  23. //Viewer
  24. "/view": "view/index.node.js",
  25. "/view/style.css": "view/style.css",
  26. //Plain images
  27. "/i": "i/index.node.js",
  28. //API
  29. "/api/template": "api/template.node.js",
  30. "/api/image_create": "api/image_create.node.js",
  31. "/api/collection_create": "api/collection_create.node.js",
  32. "/api/account_create": "api/account_create.node.js",
  33. "/api/account_login": "api/account_login.node.js",
  34. "/api/account_logout": "api/account_logout.node.js"
  35. }
  36. var loaded = loader.load(endpoints, conf);
  37. var db = new pg.Client(conf.db);
  38. //Function to run on each request
  39. function onRequest(req, res) {
  40. console.log("Request for "+req.url);
  41. var ctx = new Context({
  42. req: req,
  43. res: res,
  44. templates: loaded.templates,
  45. views: loaded.views,
  46. db: db,
  47. conf: conf
  48. });
  49. var ep = loaded.endpoints[req.url.split("?")[0]];
  50. //If the file doesn't exist, we 404.
  51. if (!ep) {
  52. ep = loaded.endpoints["/404"];
  53. ctx.setStatus(404);
  54. }
  55. //Execute if it's a .node.js, or just respond with the contents of the file
  56. if (typeof ep == "function") {
  57. ep(ctx);
  58. } else {
  59. ctx.end(ep);
  60. }
  61. }
  62. //Initiate a postgresql client
  63. db.connect(function() {
  64. //Create HTTP or HTTPS server
  65. var server;
  66. if (conf.use_https) {
  67. server = https.createServer(conf.https, onRequest);
  68. } else {
  69. server = http.createServer(onRequest);
  70. }
  71. server.listen(conf.port);
  72. console.log("Listening on port "+conf.port+".");
  73. });
  74. //We don't want to crash even if something throws an uncaught exception.
  75. if (!conf.debug) {
  76. var d = domain.create();
  77. d.on("error", function(err) {
  78. console.trace(err);
  79. });
  80. process.on("uncaughtException", function(err) {
  81. console.trace(err);
  82. });
  83. }