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}});
res.send(await Helpers.ViewLoader.load('admin/panel.pug', {
res.template('admin/panel.pug', {
current_route: req.originalUrl,
gb_records,
access_level: user.accessLevel
}));
});
return;
}
@ -74,7 +74,7 @@ async function gb_api(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) => {

View File

@ -7,14 +7,14 @@ async function articles(req, res) {
const articles = await Sequelize.Article.findAll();
res.send(await Helpers.ViewLoader.load('articles/articles.pug', {
res.template('articles/articles.pug', {
current_route: res.originalUrl,
articles
}));
});
}
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,
data: {
'title': {
@ -39,7 +39,7 @@ async function new_article(req, res) {
title: 'New article',
endpoint: '/articles/new',
pref_method: 'POST'
}));
});
}
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');
res.send(await Helpers.ViewLoader.load('guestbook.pug', {
res.template('guestbook.pug', {
current_route: req.originalUrl,
ip: req.ip,
data,
@ -31,7 +31,7 @@ async function handler(req, res, next) {
name: req.session.gb_name,
email: req.session.gb_email,
hidemail: req.session.gb_hidemail
}));
});
return;
} catch (err) {
next(err);
@ -104,11 +104,11 @@ async function submit(req, res, next) {
time: Math.floor(Date.now() / 1000)
});
if (!data) {
res.send(await Helpers.ViewLoader.load('guestbook.pug', {
res.template('guestbook.pug', {
current_route: req.originalUrl,
ip: req.ip,
errors: 'Could not create a new record'
}));
});
}
res.redirect('/guestbook#gb_entry_' + data.id);

View File

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

View File

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