Network rewrite/major cleanup, major caching changes, etc

Work on implementing capes
Update to no-express
Add more render tests
Add capes express route
Add documentation
Add undefined images to .gitignore
Fix no-express server again
Cleanup config a bit
General cleanup
Add CodeClimate badge
Test on Heroku
Speed optimizations
Fix tests
Code cleanup
This commit is contained in:
Jake
2015-01-02 01:21:15 -06:00
parent dd7e46f377
commit 3def0910bc
23 changed files with 566 additions and 478 deletions

View File

@@ -54,7 +54,7 @@ module.exports = function(req, res) {
}
}
etag = image && hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] == '"' + etag + '"';
var matches = req.headers["if-none-match"] === '"' + etag + '"';
if (image) {
var http_status = 200;
if (matches) {
@@ -103,6 +103,6 @@ module.exports = function(req, res) {
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status == 304 ? null : image);
res.end(http_status === 304 ? null : image);
}
};
};

View File

@@ -1,18 +1,27 @@
var logging = require("../modules/logging");
var helpers = require("../modules/helpers");
var config = require("../modules/config");
var router = require("express").Router();
var lwip = require("lwip");
/* GET skin request. */
router.get("/:uuid.:ext?", function (req, res) {
var uuid = (req.params.uuid || "");
var def = req.query.default;
var human_status = {
0: "none",
1: "cached",
2: "downloaded",
3: "checked",
"-1": "error"
};
// GET cape request
module.exports = function(req, res) {
var start = new Date();
var uuid = (req.url.pathname.split("/")[2] || "").split(".")[0];
var etag = null;
if (!helpers.uuid_valid(uuid)) {
res.status(422).send("422 Invalid UUID");
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("Invalid ID");
return;
}
@@ -20,13 +29,13 @@ router.get("/:uuid.:ext?", function (req, res) {
uuid = uuid.replace(/-/g, "");
try {
helpers.get_cape(uuid, function (err, hash, image) {
logging.log(uuid);
helpers.get_cape(uuid, function(err, status, image, hash) {
logging.log(uuid + " - " + human_status[status]);
if (err) {
logging.error(err);
logging.error(uuid + " " + err);
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.get("If-None-Match") == "\"" + etag + "\"";
var matches = req.headers["if-none-match"] === '"' + etag + '"';
if (image) {
var http_status = 200;
if (matches) {
@@ -34,33 +43,37 @@ router.get("/:uuid.:ext?", function (req, res) {
} else if (err) {
http_status = 503;
}
logging.debug("Etag: " + req.get("If-None-Match"));
logging.debug("Etag: " + req.headers["if-none-match"]);
logging.debug("matches: " + matches);
logging.log("status: " + http_status);
sendimage(http_status, image);
sendimage(http_status, status, image);
} else {
res.status(404).send("404 not found");
res.writeHead(404, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("404 not found");
}
});
} catch (e) {
logging.error("Error!");
} catch(e) {
logging.error(uuid + " error:");
logging.error(e);
res.status(500).send("500 error while retrieving cape");
res.writeHead(500, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("500 server error");
}
function sendimage(http_status, image) {
function sendimage(http_status, img_status, image) {
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": "downloaded",
"X-Storage-Type": human_status[img_status],
"Access-Control-Allow-Origin": "*",
"Etag": "\"" + etag + "\""
"Etag": '"' + etag + '"'
});
res.end(http_status == 304 ? null : image);
res.end(http_status === 304 ? null : image);
}
});
module.exports = router;
};

View File

@@ -23,7 +23,7 @@ module.exports = function(req, res) {
var raw_type = (req.url.path_list[2] || "");
// validate type
if (raw_type != "body" && raw_type != "head") {
if (raw_type !== "body" && raw_type !== "head") {
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
@@ -32,7 +32,7 @@ module.exports = function(req, res) {
return;
}
var body = raw_type == "body";
var body = raw_type === "body";
var uuid = (req.url.path_list[3] || "").split(".")[0];
var def = req.url.query.default;
var scale = parseInt(req.url.query.scale) || config.default_scale;
@@ -65,7 +65,7 @@ module.exports = function(req, res) {
logging.error(uuid + " " + err);
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] == '"' + etag + '"';
var matches = req.headers["if-none-match"] === '"' + etag + '"';
if (image) {
var http_status = 200;
if (matches) {
@@ -128,6 +128,6 @@ module.exports = function(req, res) {
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status == 304 ? null : image);
res.end(http_status === 304 ? null : image);
}
};

View File

@@ -31,7 +31,7 @@ module.exports = function(req, res) {
logging.error(uuid + " " + err);
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] == '"' + etag + '"';
var matches = req.headers["if-none-match"] === '"' + etag + '"';
if (image) {
var http_status = 200;
if (matches) {
@@ -82,6 +82,6 @@ module.exports = function(req, res) {
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status == 304 ? null : image);
res.end(http_status === 304 ? null : image);
}
};