Access log file option

This commit is contained in:
Klemek
2019-06-22 00:07:54 +02:00
parent 4171f565f8
commit 3cfa2bfc71
4 changed files with 96 additions and 63 deletions
+2 -22
View File
@@ -191,74 +191,54 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link
[back to top](#gitblog-md) [back to top](#gitblog-md)
* `node_port` (default: 3000) * `node_port` (default: 3000)
the port the server is listening to the port the server is listening to
* `data_dir` (default: data) * `data_dir` (default: data)
the directory where will be located the git repo with templates and articles the directory where will be located the git repo with templates and articles
* `view_engine` (default: ejs) * `view_engine` (default: ejs)
the Express view engine used to render pages from templates the Express view engine used to render pages from templates
* `access_log` (default: access.log)
log file where to save access requests (empty to disable)
* `modules` * `modules`
* `rss` (default: true) * `rss` (default: true)
activate the RSS endpoint and its features activate the RSS endpoint and its features
* `webhook` (default: true) * `webhook` (default: true)
activate the webhook endpoint and its features activate the webhook endpoint and its features
* `prism` (default: true) * `prism` (default: true)
activate Prism code highlighting activate Prism code highlighting
* `home` * `home`
* `index` (default: index.ejs) * `index` (default: index.ejs)
the name of the home page template on the data directory the name of the home page template on the data directory
it will receive `articles`, a list of articles for rendering it will receive `articles`, a list of articles for rendering
* `error` (default: error.ejs) * `error` (default: error.ejs)
the name of the error page template on the data directory the name of the error page template on the data directory
it will receive `error`, the error code it will receive `error`, the error code
* `hidden` (default: `[.ejs]`) * `hidden` (default: `[.ejs]`)
file extensions to be returned 404 when reached file extensions to be returned 404 when reached
* `article` * `article`
* `index` (default: index.md) * `index` (default: index.md)
the name of the Markdown page of the article on the `/year/month/day/` directory the name of the Markdown page of the article on the `/year/month/day/` directory
* `template` (default: template.ejs) * `template` (default: template.ejs)
the name of the article page template on the data directory the name of the article page template on the data directory
* `thumbnail_tag`: (default: thumbnail) * `thumbnail_tag`: (default: thumbnail)
the alt text searched to get the thumbnail image on the article the alt text searched to get the thumbnail image on the article
as in `![<thumbnail_tag>](<url of the image>)` as in `![<thumbnail_tag>](<url of the image>)`
* `default_title`: (default: Untitled) * `default_title`: (default: Untitled)
the title of the article in case a level 1 title was not found the title of the article in case a level 1 title was not found
* `default_thumbnail`: (default: none) * `default_thumbnail`: (default: none)
the path of the default thumbnail to get from the data directory the path of the default thumbnail to get from the data directory
* `rss` * `rss`
* `title`: (default: mygitblog RSS feed) * `title`: (default: mygitblog RSS feed)
* `description`: (default: a generated RSS feed from my articles) * `description`: (default: a generated RSS feed from my articles)
* `endpoint`: (default: /rss) * `endpoint`: (default: /rss)
* `length`: (default: 10) * `length`: (default: 10)
how many last articles will be present in the feed how many last articles will be present in the feed
* `webhook` * `webhook`
* `endpoint`: (default: /webhook) * `endpoint`: (default: /webhook)
* `secret`: (default: none) * `secret`: (default: none)
see [above](#7-securize-your-webhook-optional-) see [above](#7-securize-your-webhook-optional-)
* `signature_header`: (default: none) * `signature_header`: (default: none)
see [above](#7-securize-your-webhook-optional-) see [above](#7-securize-your-webhook-optional-)
* `pull_command`: (default: git pull) * `pull_command`: (default: git pull)
the command used by the server on webhook trigger the command used by the server on webhook trigger
* `showdown` * `showdown`
Options to be applied to Showdown renderer (see [showdown options](https://github.com/showdownjs/showdown#valid-options) for more info) Options to be applied to Showdown renderer (see [showdown options](https://github.com/showdownjs/showdown#valid-options) for more info)
+18
View File
@@ -3,6 +3,8 @@ const app = express();
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
app.enable('trust proxy');
//rss //rss
const Rss = require('rss'); const Rss = require('rss');
@@ -94,6 +96,22 @@ module.exports = (config) => {
}); });
}; };
//log request at result end
app.use((req, res, next) => {
if (config['access_log']) {
const end = res.end;
res.end = (chunk, encoding) => {
fs.appendFile(config['access_log'],
res.statusCode + ' ' + req.method + ' ' + req.url + ' ' + new Date().toUTCString() + ' ' + (req.ips.join(' ') || req.ip) + '\n',
{encoding: 'UTF-8'}, () => {
res.end = end;
res.end(chunk, encoding);
});
};
}
next();
});
// home endpoint : send the correct index page or error if not existing // home endpoint : send the correct index page or error if not existing
app.get('/', (req, res) => { app.get('/', (req, res) => {
const homePath = path.join(config['data_dir'], config['home']['index']); const homePath = path.join(config['data_dir'], config['home']['index']);
+1
View File
@@ -2,6 +2,7 @@
"node_port": 3000, "node_port": 3000,
"data_dir": "data", "data_dir": "data",
"view_engine": "ejs", "view_engine": "ejs",
"access_log": "access.log",
"modules": { "modules": {
"rss": true, "rss": true,
"webhook": true, "webhook": true,
+34
View File
@@ -20,6 +20,7 @@ config['home']['hidden'].push('.test');
config['rss']['endpoint'] = '/rsstest'; config['rss']['endpoint'] = '/rsstest';
config['rss']['length'] = 2; config['rss']['length'] = 2;
config['webhook']['endpoint'] = '/webhooktest'; config['webhook']['endpoint'] = '/webhooktest';
config['access_log'] = path.join(dataDir, 'access.log');
const app = require('../src/app')(config); const app = require('../src/app')(config);
@@ -35,6 +36,39 @@ afterAll(() => {
} }
}); });
describe('Test logging', () => {
test('test get 200', (done) => {
request(app).get('/rsstest').then(() => {
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
expect(err).toBeNull();
expect(data).toBe('200 GET /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
done();
});
});
});
test('test post 400', (done) => {
request(app).post('/rsstest').then(() => {
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
expect(err).toBeNull();
expect(data).toBe('400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
done();
});
});
});
test('test 2 requests', (done) => {
request(app).get('/rss').then(() => {
request(app).post('/rsstest').then(() => {
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
expect(err).toBeNull();
expect(data).toBe('404 GET /rss ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n' +
'400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
done();
});
});
});
});
});
describe('Test root path', () => { describe('Test root path', () => {
test('404 no index no error', (done) => { test('404 no index no error', (done) => {
request(app).get('/').then((response) => { request(app).get('/').then((response) => {