Simple image host.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

server.js 3.1KB

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