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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var http = require("http");
  2. var https = require("https");
  3. var fs = require("fs");
  4. var loader = require("./lib/loader.js");
  5. var pg = require("pg");
  6. var Context = require("./lib/context.js");
  7. var conf = JSON.parse(fs.readFileSync("conf.json"));
  8. var endpoints = {
  9. //General files
  10. "/favicon.ico": "favicon.ico",
  11. "/global.css": "global.css",
  12. "/global.js": "global.js",
  13. "/404": "404.html",
  14. //Index files
  15. "/": "index/index.node.js",
  16. "/index/script.js": "index/script.js",
  17. "/index/style.css": "index/style.css",
  18. //Viewer files
  19. "/view": "view/index.node.js",
  20. "/view/style.css": "view/style.css",
  21. //Plain image files
  22. "/i": "i/index.node.js",
  23. //API files
  24. "/api/image_create": "api/image_create.node.js",
  25. "/api/collection_create": "api/collection_create.node.js"
  26. }
  27. var loaded = loader.load(endpoints, conf);
  28. var db = new pg.Client(conf.db);
  29. //Function to run on each request
  30. function onRequest(req, res) {
  31. console.log("Request for "+req.url);
  32. var ep = loaded.endpoints[req.url.split("?")[0]];
  33. //If the file doesn't exist, we 404.
  34. if (!ep) {
  35. ep = loaded.endpoints["/404"];
  36. res.writeHead(404);
  37. }
  38. //Execute if it's a .node.js, or just respond with the contents of the file
  39. if (typeof ep == "function") {
  40. ep(new Context({
  41. req: req,
  42. res: res,
  43. templates: loaded.templates,
  44. views: loaded.views,
  45. db: db,
  46. conf: conf
  47. }));
  48. } else {
  49. res.end(ep);
  50. }
  51. }
  52. //Initiate a postgresql client
  53. db.connect(function() {
  54. //Create HTTP or HTTPS server
  55. var server;
  56. if (conf.use_https) {
  57. server = https.createServer(conf.https, onRequest);
  58. } else {
  59. server = http.createServer(onRequest);
  60. }
  61. server.listen(conf.port);
  62. console.log("Listening on port "+conf.port+".");
  63. });