server.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env node
  2. var querystring = require("querystring");
  3. var response = require("./response");
  4. var toobusy = require("toobusy-js");
  5. var logging = require("./logging");
  6. var config = require("../config");
  7. var http = require("http");
  8. var mime = require("mime");
  9. var path = require("path");
  10. var url = require("url");
  11. var fs = require("fs");
  12. var server = null;
  13. var routes = {
  14. index: require("./routes/index"),
  15. avatars: require("./routes/avatars"),
  16. skins: require("./routes/skins"),
  17. renders: require("./routes/renders"),
  18. capes: require("./routes/capes"),
  19. };
  20. // serves assets from lib/public
  21. function asset_request(req, callback) {
  22. var filename = path.join(__dirname, "public", req.url.path_list.join("/"));
  23. fs.access(filename, function(fs_err) {
  24. if (!fs_err) {
  25. fs.readFile(filename, function(err, data) {
  26. callback({
  27. body: data,
  28. type: mime.lookup(filename),
  29. err: err,
  30. });
  31. });
  32. } else {
  33. callback({});
  34. }
  35. });
  36. }
  37. // generates a 12 character random string
  38. function request_id() {
  39. return Math.random().toString(36).substring(2, 14);
  40. }
  41. // splits a URL path into an Array
  42. // the path is resolved and decoded
  43. function path_list(pathname) {
  44. // remove double and trailing slashes
  45. pathname = pathname.replace(/\/\/+/g, "/").replace(/(.)\/$/, "$1");
  46. var list = pathname.split("/");
  47. list.shift();
  48. for (var i = 0; i < list.length; i++) {
  49. // URL decode
  50. list[i] = querystring.unescape(list[i]);
  51. }
  52. return list;
  53. }
  54. // handles the +req+ by routing to the request to the appropriate module
  55. function requestHandler(req, res) {
  56. req.url = url.parse(req.url, true);
  57. req.url.query = req.url.query || {};
  58. req.url.path_list = path_list(req.url.pathname);
  59. req.id = request_id();
  60. req.start = Date.now();
  61. var local_path = req.url.path_list[0];
  62. logging.debug(req.id, req.method, req.url.href);
  63. toobusy.maxLag(200);
  64. if (toobusy() && !process.env.TRAVIS) {
  65. response(req, res, {
  66. status: -1,
  67. body: "Server is over capacity :/",
  68. err: "Too busy",
  69. code: 503,
  70. });
  71. return;
  72. }
  73. if (req.method === "GET" || req.method === "HEAD") {
  74. try {
  75. switch (local_path) {
  76. case "":
  77. routes.index(req, function(result) {
  78. response(req, res, result);
  79. });
  80. break;
  81. case "avatars":
  82. routes.avatars(req, function(result) {
  83. response(req, res, result);
  84. });
  85. break;
  86. case "skins":
  87. routes.skins(req, function(result) {
  88. response(req, res, result);
  89. });
  90. break;
  91. case "renders":
  92. routes.renders(req, function(result) {
  93. response(req, res, result);
  94. });
  95. break;
  96. case "capes":
  97. routes.capes(req, function(result) {
  98. response(req, res, result);
  99. });
  100. break;
  101. default:
  102. asset_request(req, function(result) {
  103. response(req, res, result);
  104. });
  105. }
  106. } catch(e) {
  107. var error = JSON.stringify(req.headers) + "\n" + e.stack;
  108. response(req, res, {
  109. status: -1,
  110. body: config.server.debug_enabled ? error : "Internal Server Error",
  111. err: error,
  112. });
  113. }
  114. } else {
  115. response(req, res, {
  116. status: -2,
  117. body: "Method Not Allowed",
  118. code: 405,
  119. });
  120. }
  121. }
  122. var exp = {};
  123. exp.boot = function(callback) {
  124. var port = process.env.PORT || 3000;
  125. var bind_ip = process.env.BIND || "0.0.0.0";
  126. server = http.createServer(requestHandler).listen(port, bind_ip, function() {
  127. logging.log("Server running on http://" + bind_ip + ":" + port + "/");
  128. if (callback) {
  129. callback();
  130. }
  131. });
  132. // stop accepting new connections,
  133. // wait for established connections to finish (30s max),
  134. // then exit
  135. process.on("SIGTERM", function() {
  136. logging.warn("Got SIGTERM, no longer accepting connections!");
  137. setTimeout(function() {
  138. logging.error("Dropping connections after 30s. Force quit.");
  139. process.exit(1);
  140. }, 30000);
  141. server.close(function() {
  142. logging.log("All connections closed, shutting down.");
  143. process.exit();
  144. });
  145. });
  146. };
  147. exp.close = function(callback) {
  148. server.close(callback);
  149. };
  150. module.exports = exp;
  151. if (require.main === module) {
  152. logging.error("Please use 'npm start' or 'www.js'");
  153. process.exit(1);
  154. }