Added rate limiting logging and an error page for hitting the rate limit on opening characters
This commit is contained in:
@@ -33,7 +33,6 @@ var ifKeyValid = function(apiKey, response, callback){
|
||||
response.writeHead(403, "API key is invalid");
|
||||
response.end();
|
||||
} else if (isRateLimited(apiKey)){
|
||||
console.log(`Rate limit hit by API key ${apiKey}`);
|
||||
response.writeHead(429, "Too many requests");
|
||||
response.end();
|
||||
} else {
|
||||
@@ -53,5 +52,11 @@ var rateLimiter = new RateLimiter();
|
||||
rateLimiter.addRule({apiKey: String}, 2, 10000);
|
||||
|
||||
var isRateLimited = function(apiKey){
|
||||
return !rateLimiter.check({apiKey}).allowed;
|
||||
const limited = !rateLimiter.check({apiKey}).allowed
|
||||
if (limited) {
|
||||
console.log(`Rate limit hit by API key ${apiKey}`);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -13,6 +13,11 @@ Router.plugin("ensureSignedIn", {
|
||||
|
||||
Router.plugin("dataNotFound", {notFoundTemplate: "notFound"});
|
||||
|
||||
var handleSubError = function(e){
|
||||
Session.set("error", {reason: e.reason, href: location.href});
|
||||
Router.go("/error");
|
||||
};
|
||||
|
||||
Router.map(function() {
|
||||
this.route("/", {
|
||||
name: "home",
|
||||
@@ -36,7 +41,9 @@ Router.map(function() {
|
||||
path: "/character/:_id/",
|
||||
waitOn: function(){
|
||||
return [
|
||||
subsManager.subscribe("singleCharacter", this.params._id),
|
||||
subsManager.subscribe(
|
||||
"singleCharacter", this.params._id, {onError: handleSubError}
|
||||
),
|
||||
];
|
||||
},
|
||||
action: function(){
|
||||
@@ -52,7 +59,9 @@ Router.map(function() {
|
||||
path: "/character/:_id/:urlName",
|
||||
waitOn: function(){
|
||||
return [
|
||||
subsManager.subscribe("singleCharacter", this.params._id),
|
||||
subsManager.subscribe(
|
||||
"singleCharacter", this.params._id, {onError: handleSubError}
|
||||
),
|
||||
];
|
||||
},
|
||||
data: function() {
|
||||
@@ -82,7 +91,9 @@ Router.map(function() {
|
||||
path: "/character/:_id/:urlName/print",
|
||||
waitOn: function(){
|
||||
return [
|
||||
subsManager.subscribe("singleCharacter", this.params._id),
|
||||
subsManager.subscribe(
|
||||
"singleCharacter", this.params._id, {onError: handleSubError}
|
||||
),
|
||||
];
|
||||
},
|
||||
data: function() {
|
||||
@@ -153,4 +164,11 @@ Router.map(function() {
|
||||
document.title = appName;
|
||||
},
|
||||
});
|
||||
|
||||
this.route("/error", {
|
||||
name: "error",
|
||||
onAfterAction: function() {
|
||||
document.title = `${appName} - Error`;
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user