work in progress hit_counter

This commit is contained in:
Klemek
2021-03-30 15:30:54 +02:00
parent fa5cf31983
commit b0ba52e140
2 changed files with 35 additions and 2 deletions
+18 -2
View File
@@ -51,6 +51,7 @@ module.exports = (config) => {
let showError;
const fw = require('./file_walker')(config);
const renderer = require('./renderer')(config);
const hc = require('./hit_counter')(config);
// set view engine from configuration
app.set('view engine', config['view_engine']);
@@ -157,14 +158,22 @@ module.exports = (config) => {
if (err) {
showError(req, res, 404);
} else {
hc.count(req, '/');
render(req, res, homePath,
{
articles: Object.values(articles)
.filter(d => !d.draft).sort((a, b) => ('' + b.path).localeCompare(a.path))
.filter(d => !d.draft).sort((a, b) => ('' + b.path).localeCompare(a.path)),
});
}
});
});
app.get('/stats', (req, res) => {
const data = hc.read('/');
res.json({
hits: data.hits,
visitors: data.visitors,
});
});
//RSS endpoint
app.get(config['rss']['endpoint'], (req, res) => {
@@ -229,12 +238,19 @@ module.exports = (config) => {
// catch all article urls and render them
app.get('*', (req, res, next) => {
if (/^\/\d{4}\/\d{2}\/\d{2}\/$/.test(req.path)) {
if (/^\/\d{4}\/\d{2}\/\d{2}\/(stats)?$/.test(req.path)) {
const articlePath = req.path.substr(1, 10);
const article = articles[articlePath];
if (!article) {
showError(req, res, 404);
} else if (req.path.endsWith('stats')) {
const data = hc.read(articlePath);
res.json({
hits: data.hits,
visitors: data.visitors,
});
} else {
hc.count(req, articlePath);
renderer.render(article.realPath, (err, html) => {
if (err) {
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
+17
View File
@@ -0,0 +1,17 @@
module.exports = (config) => {
const count = (req, path) => {
};
const read = (path) => {
return {
hits: 0,
visitors: 0,
};
};
return {
count: count,
read: read,
};
};