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.

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