Simple image host.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.js 2.4KB

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