refactor template loading mechanism

This commit is contained in:
b1ek 2023-03-12 01:54:53 +10:00
parent 7b6c3a5e93
commit 7c3ff9b699
Signed by: blek
GPG Key ID: 14546221E3595D0C
6 changed files with 36 additions and 46 deletions

View File

@ -1,12 +0,0 @@
const Helpers = require('../helpers');
const handler = require('express-async-handler');
async function about(req, res) {
res.send(await Helpers.ViewLoader.load('about.pug', {
current_route: req.originalUrl
}));
}
module.exports = (router) => {
router.get('/about', handler(about));
}

View File

@ -41,11 +41,11 @@ async function panel(req, res) {
const articles = await db.Article.findAll({where: {hidden: true}}); const articles = await db.Article.findAll({where: {hidden: true}});
res.send(await Helpers.ViewLoader.load('admin/panel.pug', { res.template('admin/panel.pug', {
current_route: req.originalUrl, current_route: req.originalUrl,
gb_records, gb_records,
access_level: user.accessLevel access_level: user.accessLevel
})); });
return; return;
} }
@ -74,7 +74,7 @@ async function gb_api(req, res) {
} }
async function article_new(req, res) { async function article_new(req, res) {
res.send(await Helpers.ViewLoader.load('articles/new.pug')) res.template('articles/new.pug')
} }
module.exports = (router) => { module.exports = (router) => {

View File

@ -7,14 +7,14 @@ async function articles(req, res) {
const articles = await Sequelize.Article.findAll(); const articles = await Sequelize.Article.findAll();
res.send(await Helpers.ViewLoader.load('articles/articles.pug', { res.template('articles/articles.pug', {
current_route: res.originalUrl, current_route: res.originalUrl,
articles articles
})); });
} }
async function new_article(req, res) { async function new_article(req, res) {
res.send(await Helpers.ViewLoader.load('admin/data_edit.pug', { res.template('admin/data_edit.pug', {
current_route: req.originalUrl, current_route: req.originalUrl,
data: { data: {
'title': { 'title': {
@ -39,7 +39,7 @@ async function new_article(req, res) {
title: 'New article', title: 'New article',
endpoint: '/articles/new', endpoint: '/articles/new',
pref_method: 'POST' pref_method: 'POST'
})); });
} }
async function new_article_post(req, res) { async function new_article_post(req, res) {

View File

@ -23,7 +23,7 @@ async function handler(req, res, next) {
if (!data) throw new Error('Failed to get guestbook entries'); if (!data) throw new Error('Failed to get guestbook entries');
res.send(await Helpers.ViewLoader.load('guestbook.pug', { res.template('guestbook.pug', {
current_route: req.originalUrl, current_route: req.originalUrl,
ip: req.ip, ip: req.ip,
data, data,
@ -31,7 +31,7 @@ async function handler(req, res, next) {
name: req.session.gb_name, name: req.session.gb_name,
email: req.session.gb_email, email: req.session.gb_email,
hidemail: req.session.gb_hidemail hidemail: req.session.gb_hidemail
})); });
return; return;
} catch (err) { } catch (err) {
next(err); next(err);
@ -104,11 +104,11 @@ async function submit(req, res, next) {
time: Math.floor(Date.now() / 1000) time: Math.floor(Date.now() / 1000)
}); });
if (!data) { if (!data) {
res.send(await Helpers.ViewLoader.load('guestbook.pug', { res.template('guestbook.pug', {
current_route: req.originalUrl, current_route: req.originalUrl,
ip: req.ip, ip: req.ip,
errors: 'Could not create a new record' errors: 'Could not create a new record'
})); });
} }
res.redirect('/guestbook#gb_entry_' + data.id); res.redirect('/guestbook#gb_entry_' + data.id);

View File

@ -9,7 +9,14 @@ async function resume(req, res) {
console.log(process.env.APP_DEBUG); console.log(process.env.APP_DEBUG);
} }
async function about(req, res) {
res.template('about.pug', {
current_route: req.originalUrl
});
}
module.exports = (router) => { module.exports = (router) => {
router.get('/services', handler(services)) router.get('/services', handler(services))
router.get('/resume', handler(resume)) router.get('/resume', handler(resume))
router.get('/about', handler(about));
} }

View File

@ -1,13 +1,11 @@
const Helpers = require('../helpers'); const Helpers = require('../helpers');
async function handler(req, res) { async function handler(req, res) {
res.send( res.template(
await Helpers.ViewLoader.load(
'project.pug', 'project.pug',
{ {
current_route: '/project' current_route: '/project'
} }
)
); );
return; return;
} }
@ -16,26 +14,23 @@ async function viewer(req, res) {
const id = req.params.id; const id = req.params.id;
const view = 'projects/' + id + '.pug'; const view = 'projects/' + id + '.pug';
if (!(await Helpers.ViewLoader.exists(view))) { if (!(await Helpers.ViewLoader.exists(view))) {
res.status(404).send( res.status(404).template(
await Helpers.ViewLoader.load(
'error.pug', 'error.pug',
{ {
current_route: req.originalUrl, current_route: req.originalUrl,
error: '404 Not Found', error: '404 Not Found',
message: 'Requested project does not exists. Are you sure this is the valid link?' message: 'Requested project does not exists. Are you sure this is the valid link?'
} }
)
); );
return; return;
} }
res.send( res.template(
await Helpers.ViewLoader.load(
view, view,
{ {
current_route: req.originalUrl current_route: req.originalUrl
} }
) )
)
} }
module.exports = (router) => { module.exports = (router) => {