Simple image host.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

server.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var http = require("http");
  2. var https = require("https");
  3. var fs = require("fs");
  4. var domain = require("domain");
  5. var zlib = require("zlib");
  6. var loader = require("./lib/loader.js");
  7. var pg = require("pg");
  8. var Context = require("./lib/context.js");
  9. var conf = JSON.parse(fs.readFileSync("conf.json"));
  10. var endpoints = {
  11. //General
  12. "/favicon.ico": "favicon.ico",
  13. "/global.css": "global.css",
  14. "/global.js": "global.js",
  15. "/404": "404.node.js",
  16. //Index
  17. "/": "index/index.node.js",
  18. "/index/script.js": "index/script.js",
  19. "/index/style.css": "index/style.css",
  20. //Register
  21. "/register": "register/index.node.js",
  22. "/register/style.css": "register/style.css",
  23. "/register/script.js": "register/script.js",
  24. //Profile
  25. "/profile": "profile/index.node.js",
  26. "/profile/style.css": "profile/style.css",
  27. "/profile/script.js": "profile/script.js",
  28. //Settings
  29. "/settings": "settings/index.node.js",
  30. "/settings/style.css": "settings/style.css",
  31. "/settings/script.js": "settings/script.js",
  32. //Viewer
  33. "/view": "view/index.node.js",
  34. "/view/style.css": "view/style.css",
  35. //Plain images
  36. "/i": "i/index.node.js",
  37. //API
  38. "/api/template": "api/template.node.js",
  39. "/api/image_create": "api/image_create.node.js",
  40. "/api/collection_create": "api/collection_create.node.js",
  41. "/api/account_create": "api/account_create.node.js",
  42. "/api/account_login": "api/account_login.node.js",
  43. "/api/account_logout": "api/account_logout.node.js",
  44. "/api/account_change_password": "api/account_change_password.node.js"
  45. }
  46. //We cache static resources for a long time. However, we want to invalidate
  47. //the browser's cache whenever a file updates. Therefore, we append
  48. //a number to all static files, and the number increases every time we start
  49. //the server. currentRun is that number.
  50. var currentRun;
  51. try {
  52. currentRun = parseInt(fs.readFileSync(".currentRun", "utf8"));
  53. } catch (err) {
  54. if (err.code === "ENOENT")
  55. currentRun = 0;
  56. else
  57. throw err;
  58. }
  59. currentRun = (currentRun >= conf.maxRuns ? 0 : currentRun);
  60. currentRun = (currentRun || 0) + 1;
  61. conf.web.currentRun = currentRun.toString();
  62. fs.writeFileSync(".currentRun", currentRun, "utf8");
  63. var loaded = loader.load(endpoints, conf);
  64. var db = new pg.Client(conf.db);
  65. var gzipCache = {};
  66. //Function to run on each request
  67. function onRequest(req, res) {
  68. var ctx = new Context({
  69. req: req,
  70. res: res,
  71. templates: loaded.templates,
  72. views: loaded.views,
  73. db: db,
  74. conf: conf
  75. });
  76. var ep = loaded.endpoints[req.url.split("?")[0]];
  77. //If the file doesn't exist, we 404.
  78. if (!ep) {
  79. ep = loaded.endpoints["/404"];
  80. ctx.setStatus(404);
  81. }
  82. //Execute if it's a .node.js, or just respond with the contents of the file
  83. if (typeof ep == "function") {
  84. ep(ctx);
  85. } else {
  86. //Cache content for a year
  87. ctx.setHeader("Cache-Control", "public, max-age=31536000");
  88. //Gzip and such
  89. if (ctx.shouldGzip && gzipCache[req.url]) {
  90. ctx.end(gzipCache[req.url], true);
  91. } else if (ctx.shouldGzip) {
  92. zlib.gzip(ep, function(err, res) {
  93. gzipCache[req.url] = res;
  94. ctx.end(res, true);
  95. });
  96. } else {
  97. ctx.end(ep);
  98. }
  99. }
  100. }
  101. //Initiate a postgresql client
  102. db.connect(function() {
  103. //Create HTTP or HTTPS server
  104. var server;
  105. if (conf.use_https) {
  106. server = https.createServer(conf.https, onRequest);
  107. } else {
  108. server = http.createServer(onRequest);
  109. }
  110. server.listen(conf.port);
  111. console.log("Listening on port "+conf.port+".");
  112. });
  113. //We don't want to crash even if something throws an uncaught exception.
  114. if (!conf.debug) {
  115. var d = domain.create();
  116. d.on("error", function(err) {
  117. console.trace(err);
  118. });
  119. process.on("uncaughtException", function(err) {
  120. console.trace(err);
  121. });
  122. }