Skip to content

Commit 00017da

Browse files
added a url rewrite rule to standardise url requests. At the moment all requests get accounted by the metrics module, including static assets. If later we want to we can selectively ignore specific paths.
1 parent 7dbf7db commit 00017da

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

Diff for: api/metrics/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020

2121
function parse(path) {
2222
var clean_path = path;
23+
var ignore_list = ['css', 'img', 'js'];
2324

24-
if (path[path.length - 1] != '/') {
25-
if (!path.includes('.')) {
26-
clean_path = path.substr(0, path.lastIndexOf('/') + 1);
27-
}
28-
};
25+
if (ignore_list.indexOf(path.split('/')[1]) != -1) {
26+
clean_path = '/' + path.split('/')[1] + '/';
27+
}
2928

3029
return clean_path;
3130
}
@@ -35,8 +34,7 @@
3534
if (path !== '/metrics' && path !== '/metrics/') {
3635
var duration = s(start);
3736
var method = method.toLowerCase();
38-
var clean_path = parse(path);
39-
metric.http.requests.duration.labels(method, clean_path, statusCode).observe(duration);
37+
metric.http.requests.duration.labels(method, path, statusCode).observe(duration);
4038
}
4139
};
4240

Diff for: helpers/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
res.end();
5252
}
5353

54+
/* Rewrites and redirects any url that doesn't end with a slash. */
55+
helpers.rewriteSlash = function(req, res, next) {
56+
if(req.url.substr(-1) == '/' && req.url.length > 1)
57+
res.redirect(301, req.url.slice(0, -1));
58+
else
59+
next();
60+
}
61+
5462
/* Public: performs an HTTP GET request to the given URL
5563
*
5664
* url - the URL where the external service can be reached out

Diff for: server.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ var request = require("request")
1515
, metrics = require("./api/metrics")
1616
, app = express()
1717

18-
app.use(express.static("public"));
18+
19+
app.use(helpers.rewriteSlash);
1920
app.use(metrics);
21+
app.use(express.static("public"));
2022
if(process.env.SESSION_REDIS) {
2123
console.log('Using the redis based session manager');
2224
app.use(session(config.session_redis));

0 commit comments

Comments
 (0)