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.

server.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. //Profile
  24. "/profile": "profile/index.node.js",
  25. "/profile/style.css": "profile/style.css",
  26. "/profile/script.js": "profile/script.js",
  27. //Settings
  28. "/settings": "settings/index.node.js",
  29. "/settings/style.css": "settings/style.css",
  30. "/settings/script.js": "settings/script.js",
  31. //Viewer
  32. "/view": "view/index.node.js",
  33. "/view/style.css": "view/style.css",
  34. //Plain images
  35. "/i": "i/index.node.js",
  36. //API
  37. "/api/template": "api/template.node.js",
  38. "/api/image_create": "api/image_create.node.js",
  39. "/api/collection_create": "api/collection_create.node.js",
  40. "/api/account_create": "api/account_create.node.js",
  41. "/api/account_login": "api/account_login.node.js",
  42. "/api/account_logout": "api/account_logout.node.js",
  43. "/api/account_change_password": "api/account_change_password.node.js"
  44. }
  45. var loaded = loader.load(endpoints, conf);
  46. var db = new pg.Client(conf.db);
  47. //Function to run on each request
  48. function onRequest(req, res) {
  49. var ctx = new Context({
  50. req: req,
  51. res: res,
  52. templates: loaded.templates,
  53. views: loaded.views,
  54. db: db,
  55. conf: conf
  56. });
  57. var ep = loaded.endpoints[req.url.split("?")[0]];
  58. //If the file doesn't exist, we 404.
  59. if (!ep) {
  60. ep = loaded.endpoints["/404"];
  61. ctx.setStatus(404);
  62. }
  63. //Execute if it's a .node.js, or just respond with the contents of the file
  64. if (typeof ep == "function") {
  65. ep(ctx);
  66. } else {
  67. ctx.end(ep);
  68. }
  69. }
  70. //Initiate a postgresql client
  71. db.connect(function() {
  72. //Create HTTP or HTTPS server
  73. var server;
  74. if (conf.use_https) {
  75. server = https.createServer(conf.https, onRequest);
  76. } else {
  77. server = http.createServer(onRequest);
  78. }
  79. server.listen(conf.port);
  80. console.log("Listening on port "+conf.port+".");
  81. });
  82. //We don't want to crash even if something throws an uncaught exception.
  83. if (!conf.debug) {
  84. var d = domain.create();
  85. d.on("error", function(err) {
  86. console.trace(err);
  87. });
  88. process.on("uncaughtException", function(err) {
  89. console.trace(err);
  90. });
  91. }
  92. function command(tokens) {
  93. switch(tokens[0]) {
  94. //Reload configuration
  95. case "reload-conf":
  96. var c = JSON.parse(fs.readFileSync("conf.json"));
  97. for (var i in c)
  98. conf[i] = c[i];
  99. break;
  100. //Reload HTML
  101. case "reload-html":
  102. var l = loader.load(endpoints, conf);
  103. for (var i in l)
  104. loaded[i] = l[i];
  105. break;
  106. //Reload everything
  107. case "reload":
  108. command(["reload-conf"]);
  109. command(["reload-html"]);
  110. break;
  111. default: return false;
  112. }
  113. return true;
  114. }
  115. process.stdin.on("data", function(line) {
  116. var tokens = line.toString().split(/\s+/);
  117. if (command(tokens)) {
  118. return console.log(tokens[0]+" completed successfully.");
  119. } else {
  120. return console.log("Command not found: "+tokens[0]);
  121. }
  122. });