work in progress hit_counter
This commit is contained in:
+18
-2
@@ -51,6 +51,7 @@ module.exports = (config) => {
|
|||||||
let showError;
|
let showError;
|
||||||
const fw = require('./file_walker')(config);
|
const fw = require('./file_walker')(config);
|
||||||
const renderer = require('./renderer')(config);
|
const renderer = require('./renderer')(config);
|
||||||
|
const hc = require('./hit_counter')(config);
|
||||||
|
|
||||||
// set view engine from configuration
|
// set view engine from configuration
|
||||||
app.set('view engine', config['view_engine']);
|
app.set('view engine', config['view_engine']);
|
||||||
@@ -157,14 +158,22 @@ module.exports = (config) => {
|
|||||||
if (err) {
|
if (err) {
|
||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
} else {
|
} else {
|
||||||
|
hc.count(req, '/');
|
||||||
render(req, res, homePath,
|
render(req, res, homePath,
|
||||||
{
|
{
|
||||||
articles: Object.values(articles)
|
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
|
//RSS endpoint
|
||||||
app.get(config['rss']['endpoint'], (req, res) => {
|
app.get(config['rss']['endpoint'], (req, res) => {
|
||||||
@@ -229,12 +238,19 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
// catch all article urls and render them
|
// catch all article urls and render them
|
||||||
app.get('*', (req, res, next) => {
|
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 articlePath = req.path.substr(1, 10);
|
||||||
const article = articles[articlePath];
|
const article = articles[articlePath];
|
||||||
if (!article) {
|
if (!article) {
|
||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
|
} else if (req.path.endsWith('stats')) {
|
||||||
|
const data = hc.read(articlePath);
|
||||||
|
res.json({
|
||||||
|
hits: data.hits,
|
||||||
|
visitors: data.visitors,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
|
hc.count(req, articlePath);
|
||||||
renderer.render(article.realPath, (err, html) => {
|
renderer.render(article.realPath, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
module.exports = (config) => {
|
||||||
|
const count = (req, path) => {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
const read = (path) => {
|
||||||
|
return {
|
||||||
|
hits: 0,
|
||||||
|
visitors: 0,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
count: count,
|
||||||
|
read: read,
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user