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.

server.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  35. var loaded = loader.load(endpoints, conf);
  36. var db = new pg.Client(conf.db);
  37. //Function to run on each request
  38. function onRequest(req, res) {
  39. console.log("Request for "+req.url);
  40. var ctx = new Context({
  41. req: req,
  42. res: res,
  43. templates: loaded.templates,
  44. views: loaded.views,
  45. db: db,
  46. conf: conf
  47. });
  48. var ep = loaded.endpoints[req.url.split("?")[0]];
  49. //If the file doesn't exist, we 404.
  50. if (!ep) {
  51. ep = loaded.endpoints["/404"];
  52. ctx.setStatus(404);
  53. }
  54. //Execute if it's a .node.js, or just respond with the contents of the file
  55. if (typeof ep == "function") {
  56. ep(ctx);
  57. } else {
  58. ctx.end(ep);
  59. }
  60. }
  61. //Initiate a postgresql client
  62. db.connect(function() {
  63. //Create HTTP or HTTPS server
  64. var server;
  65. if (conf.use_https) {
  66. server = https.createServer(conf.https, onRequest);
  67. } else {
  68. server = http.createServer(onRequest);
  69. }
  70. server.listen(conf.port);
  71. console.log("Listening on port "+conf.port+".");
  72. });
  73. //We don't want to crash even if something throws an uncaught exception.
  74. if (!conf.debug) {
  75. var d = domain.create();
  76. d.on("error", function(err) {
  77. console.trace(err);
  78. });
  79. process.on("uncaughtException", function(err) {
  80. console.trace(err);
  81. });
  82. }