Merge pull request #13 from Klemek/dev

v1.2.2
This commit is contained in:
Klemek
2019-06-26 19:52:25 +02:00
committed by GitHub
3 changed files with 33 additions and 15 deletions
+8
View File
@@ -192,6 +192,14 @@ Here are the steps for Github, if you use another platform adapt it your way (he
* Update your webhook on github to include the secret * Update your webhook on github to include the secret
* Check if Github successfully reached the endpoint * Check if Github successfully reached the endpoint
#### 8. Keep your server always up and running (optionnal)
This project `package.json` comes with a [nodemon](https://github.com/remy/nodemon) config.
After installing (`npm i -g nodemon`) you can then run the app with juste the `nodemon` command in the working directory.
With this method, you can do a simple `git pull` to update your server.
## Writing an article ## Writing an article
[back to top](#gitblog-md) [back to top](#gitblog-md)
+12 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "gitblog.md", "name": "gitblog.md",
"version": "1.2.1", "version": "1.2.2",
"description": "A static blog using Markdown pulled from your git repository.", "description": "A static blog using Markdown pulled from your git repository.",
"main": "src/server.js", "main": "src/server.js",
"dependencies": { "dependencies": {
@@ -46,5 +46,16 @@
"!src/postinstall.js", "!src/postinstall.js",
"!src/lib/*.js" "!src/lib/*.js"
] ]
},
"nodemonConfig": {
"ignore": [
"test/*",
"sample_data/*",
"data/*",
"uml/*",
"*.log",
"README.md"
],
"delay": "2500"
} }
} }
+13 -14
View File
@@ -94,17 +94,16 @@ module.exports = (config) => {
/** /**
* Show an error with the correct page * Show an error with the correct page
* @param req * @param req
* @param resPath - the page of the original error
* @param code - error code
* @param res * @param res
* @param code - error code
*/ */
const showError = (req, resPath, code, res) => { const showError = (req, res, code) => {
const errorPath = path.join(config['data_dir'], config['home']['error']); const errorPath = path.join(config['data_dir'], config['home']['error']);
fs.access(errorPath, fs.constants.R_OK, (err) => { fs.access(errorPath, fs.constants.R_OK, (err) => {
if (err) if (err)
res.sendStatus(code); res.sendStatus(code);
else else
render(req, res, errorPath, {error: code, path: resPath}, code); render(req, res, errorPath, {error: code, path: req.path}, code);
}); });
}; };
@@ -137,7 +136,7 @@ module.exports = (config) => {
const homePath = path.join(config['data_dir'], config['home']['index']); const homePath = path.join(config['data_dir'], config['home']['index']);
fs.access(homePath, fs.constants.R_OK, (err) => { fs.access(homePath, fs.constants.R_OK, (err) => {
if (err) if (err)
showError(req.path, 404, res); showError(req, res, 404);
else else
render(req, res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))}); render(req, res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))});
}); });
@@ -150,15 +149,15 @@ module.exports = (config) => {
const feed = new Rss({ const feed = new Rss({
'title': config['rss']['title'], 'title': config['rss']['title'],
'description': config['rss']['description'], 'description': config['rss']['description'],
'feed_url': 'http://' + req.headers.host + req.url, 'feed_url': host + req.url,
'site_url': 'http://' + req.headers.host 'site_url': host
}); });
Object.values(articles) Object.values(articles)
.slice(0, config['rss']['length']) .slice(0, config['rss']['length'])
.forEach((article) => { .forEach((article) => {
feed.item({ feed.item({
title: article.title, title: article.title,
url: 'http://' + req.headers.host + article.url, url: host + article.url,
date: article.date date: article.date
}); });
}); });
@@ -166,7 +165,7 @@ module.exports = (config) => {
} }
res.type(req.headers['user-agent'].match(/Mozilla/) ? 'text/xml' : 'rss').send(lastRSS); res.type(req.headers['user-agent'].match(/Mozilla/) ? 'text/xml' : 'rss').send(lastRSS);
} else { } else {
showError(req.path, 404, res); showError(req, res, 404);
} }
}); });
@@ -209,19 +208,19 @@ module.exports = (config) => {
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.path, 404, res); showError(req, res, 404);
else { else {
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => { renderer.render(path.join(article.realPath, config['article']['index']), (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}`);
return showError(req.path, 500, res); return showError(req, res, 500);
} }
article.content = html; article.content = html;
const templatePath = path.join(config['data_dir'], config['article']['template']); const templatePath = path.join(config['data_dir'], config['article']['template']);
fs.access(templatePath, fs.constants.R_OK, (err) => { fs.access(templatePath, fs.constants.R_OK, (err) => {
if (err) { if (err) {
console.log(cons.error, `no template found at ${templatePath}`); console.log(cons.error, `no template found at ${templatePath}`);
showError(req.path, 500, res); showError(req, res, 500);
} else } else
render(req, res, templatePath, {article: article}); render(req, res, templatePath, {article: article});
}); });
@@ -235,7 +234,7 @@ module.exports = (config) => {
// catch all hidden file type and return 404 // catch all hidden file type and return 404
app.get('*', (req, res, next) => { app.get('*', (req, res, next) => {
if (config['home']['hidden'].includes(path.extname(req.path))) if (config['home']['hidden'].includes(path.extname(req.path)))
showError(req.path, 404, res); showError(req, res, 404);
else else
next(); next();
}); });
@@ -244,7 +243,7 @@ module.exports = (config) => {
app.get('*', express.static(path.join(__dirname, '..', config['data_dir']))); app.get('*', express.static(path.join(__dirname, '..', config['data_dir'])));
// catch express.static errors (mostly not found) by displaying 404 // catch express.static errors (mostly not found) by displaying 404
app.get('*', (req, res) => { app.get('*', (req, res) => {
showError(req.path, 404, res); showError(req, res, 404);
}); });
// catch all other methods and return 400 // catch all other methods and return 400