Json routes will hit backend & get 404'd there,

allows for pages with missing json that arent prebuilt to build when json route is hit,
without requiring html pageload first better for api
merge-requests/208/head
Thomas Lynch 4 years ago
parent 636c9072ff
commit 1399dda5fd
  1. 4
      configs/nginx/nginx.example
  2. 4
      configs/nginx/nginx_no_https.example
  3. 3
      configs/nginx/snippets/jschan_routes.conf
  4. 7
      controllers/pages.js
  5. 2
      helpers/render.js
  6. 22
      helpers/tasks.js
  7. 13
      models/pages/board.js
  8. 30
      models/pages/boardlist.js
  9. 16
      models/pages/catalog.js
  10. 18
      models/pages/thread.js

@ -126,8 +126,8 @@ server {
location ~* \.json$ { location ~* \.json$ {
expires 0; expires 0;
root /path/to/jschan/static/json; root /path/to/jschan/static/json;
try_files $uri =404; try_files $uri @backend;
#json doesnt hit backend if it doesnt exist yet. #some json files will build pages on-demand
} }
# CSS # CSS

@ -126,8 +126,8 @@ server {
location ~* \.json$ { location ~* \.json$ {
expires 0; expires 0;
root /path/to/jschan/static/json; root /path/to/jschan/static/json;
try_files $uri =404; try_files $uri @backend;
#json doesnt hit backend if it doesnt exist yet. #some json files will build pages on-demand
} }
# CSS # CSS

@ -87,8 +87,7 @@ location ~* \.json$ {
testcookie off; testcookie off;
expires 0; expires 0;
root /path/to/jschan/static/json; root /path/to/jschan/static/json;
try_files $uri =404; try_files $uri @backend;
#json doesnt hit backend if it doesnt exist yet.
} }
# CSS # CSS

@ -33,11 +33,12 @@ router.get('/news.html', news);
//board list //board list
router.get('/boards.html', useSession, sessionRefresh, calcPerms, boardlist); router.get('/boards.html', useSession, sessionRefresh, calcPerms, boardlist);
router.get('/boards.json', useSession, sessionRefresh, calcPerms, boardlist);
//board pages //board pages
router.get('/:board/:page(1[0-9]{1,}|[2-9][0-9]{0,}|index).html', Boards.exists, paramConverter, board); //index router.get('/:board/:page(1[0-9]{1,}|[2-9][0-9]{0,}|index).(html|json)', Boards.exists, paramConverter, board); //index
router.get('/:board/thread/:id([1-9][0-9]{0,}).html', Boards.exists, paramConverter, Posts.exists, thread); //thread view router.get('/:board/thread/:id([1-9][0-9]{0,}).(html|json)', Boards.exists, paramConverter, Posts.exists, thread); //thread view
router.get('/:board/catalog.html', Boards.exists, catalog); //catalog router.get('/:board/catalog.(html|json)', Boards.exists, catalog); //catalog
router.get('/:board/logs.html', Boards.exists, modloglist);//modlog list router.get('/:board/logs.html', Boards.exists, modloglist);//modlog list
router.get('/:board/logs/:date(\\d{2}-\\d{2}-\\d{4}).html', Boards.exists, paramConverter, modlog); //daily log router.get('/:board/logs/:date(\\d{2}-\\d{2}-\\d{4}).html', Boards.exists, paramConverter, modlog); //daily log
router.get('/:board/banners.html', Boards.exists, banners); //banners router.get('/:board/banners.html', Boards.exists, banners); //banners

@ -49,5 +49,5 @@ module.exports = async (htmlName, templateName, options, json=null) => {
} }
await Promise.all([htmlPromise, jsonPromise]); await Promise.all([htmlPromise, jsonPromise]);
await lock.unlock(); await lock.unlock();
return html; return { html, json };
}; };

@ -16,7 +16,7 @@ module.exports = {
buildBanners: async (options) => { buildBanners: async (options) => {
const label = `/${options.board._id}/banners.html`; const label = `/${options.board._id}/banners.html`;
const start = process.hrtime(); const start = process.hrtime();
const html = await render(label, 'banners.pug', options, { const { html } = await render(label, 'banners.pug', options, {
'name': `/${options.board._id}/banners.json`, 'name': `/${options.board._id}/banners.json`,
'data': options.board.banners 'data': options.board.banners
}); });
@ -32,7 +32,7 @@ module.exports = {
options.board = await Boards.findOne(options.board); options.board = await Boards.findOne(options.board);
} }
const threads = await Posts.getCatalog(options.board._id); const threads = await Posts.getCatalog(options.board._id);
const html = await render(label, 'catalog.pug', { const { html, json } = await render(label, 'catalog.pug', {
...options, ...options,
threads, threads,
}, { }, {
@ -41,7 +41,7 @@ module.exports = {
}); });
const end = process.hrtime(start); const end = process.hrtime(start);
debugLogs && console.log(timeDiffString(label, end)); debugLogs && console.log(timeDiffString(label, end));
return html; return { html, json };
}, },
buildThread: async (options) => { buildThread: async (options) => {
@ -54,7 +54,7 @@ module.exports = {
if (!thread) { if (!thread) {
return; //this thread may have been an OP that was deleted return; //this thread may have been an OP that was deleted
} }
const html = await render(label, 'thread.pug', { const { html, json } = await render(label, 'thread.pug', {
...options, ...options,
thread, thread,
}, { }, {
@ -63,7 +63,7 @@ module.exports = {
}); });
const end = process.hrtime(start); const end = process.hrtime(start);
debugLogs && console.log(timeDiffString(label, end)); debugLogs && console.log(timeDiffString(label, end));
return html; return { html, json };
}, },
buildBoard: async (options) => { buildBoard: async (options) => {
@ -73,7 +73,7 @@ module.exports = {
if (!options.maxPage) { if (!options.maxPage) {
options.maxPage = Math.min(Math.ceil((await Posts.getPages(options.board._id)) / 10), Math.ceil(options.board.settings.threadLimit/10)); options.maxPage = Math.min(Math.ceil((await Posts.getPages(options.board._id)) / 10), Math.ceil(options.board.settings.threadLimit/10));
} }
const html = await render(label, 'board.pug', { const { html, json } = await render(label, 'board.pug', {
...options, ...options,
threads, threads,
}, { }, {
@ -82,7 +82,7 @@ module.exports = {
}); });
const end = process.hrtime(start); const end = process.hrtime(start);
debugLogs && console.log(timeDiffString(label, end)); debugLogs && console.log(timeDiffString(label, end));
return html; return { html, json };
}, },
//building multiple pages (for rebuilds) //building multiple pages (for rebuilds)
@ -130,7 +130,7 @@ module.exports = {
const label = '/news.html'; const label = '/news.html';
const start = process.hrtime(); const start = process.hrtime();
const news = await News.find(); const news = await News.find();
const html = await render('news.html', 'news.pug', { const { html } = await render('news.html', 'news.pug', {
news news
}); });
const end = process.hrtime(start); const end = process.hrtime(start);
@ -155,7 +155,7 @@ module.exports = {
if (!options.logs) { if (!options.logs) {
options.logs = await Modlogs.findBetweenDate(options.board, options.startDate, options.endDate); options.logs = await Modlogs.findBetweenDate(options.board, options.startDate, options.endDate);
} }
const html = await render(label, 'modlog.pug', { const { html } = await render(label, 'modlog.pug', {
...options ...options
}); });
const end = process.hrtime(start); const end = process.hrtime(start);
@ -187,7 +187,7 @@ module.exports = {
await Modlogs.deleteOld(options.board, pruneAfter); await Modlogs.deleteOld(options.board, pruneAfter);
} }
} }
const html = await render(label, 'modloglist.pug', { const { html } = await render(label, 'modloglist.pug', {
board: options.board, board: options.board,
dates dates
}); });
@ -205,7 +205,7 @@ module.exports = {
Files.activeContent(), //size ans number of files Files.activeContent(), //size ans number of files
News.find(maxRecentNews), //some recent newsposts News.find(maxRecentNews), //some recent newsposts
]); ]);
const html = await render('index.html', 'home.pug', { const { html } = await render('index.html', 'home.pug', {
totalStats, totalStats,
boards, boards,
fileStats, fileStats,

@ -1,26 +1,31 @@
'use strict'; 'use strict';
const Posts = require(__dirname+'/../../db/posts.js') const Posts = require(__dirname+'/../../db/posts.js')
, buildQueue = require(__dirname+'/../../queue.js')
, { buildBoard } = require(__dirname+'/../../helpers/tasks.js'); , { buildBoard } = require(__dirname+'/../../helpers/tasks.js');
module.exports = async (req, res, next) => { module.exports = async (req, res, next) => {
const page = req.params.page === 'index' ? 1 : Number(req.params.page); const page = req.params.page === 'index' ? 1 : Number(req.params.page);
let html; let html, json;
try { try {
const maxPage = Math.min(Math.ceil((await Posts.getPages(req.params.board)) / 10), Math.ceil(res.locals.board.settings.threadLimit/10)) || 1; const maxPage = Math.min(Math.ceil((await Posts.getPages(req.params.board)) / 10), Math.ceil(res.locals.board.settings.threadLimit/10)) || 1;
if (page > maxPage) { if (page > maxPage) {
return next(); return next();
} }
html = await buildBoard({ ({ html, json } = await buildBoard({
board: res.locals.board, board: res.locals.board,
page, page,
maxPage maxPage
}); }));
} catch (err) { } catch (err) {
return next(err); return next(err);
} }
return res.send(html); if (req.path.endsWith('.json')) {
return res.json(json);
} else {
return res.send(html);
}
} }

@ -87,15 +87,25 @@ module.exports = async (req, res, next) => {
} }
res res
.set('Cache-Control', `${isGlobalStaff ? 'private' : 'public'}, max-age=60`) .set('Cache-Control', `${isGlobalStaff ? 'private' : 'public'}, max-age=60`);
.render('boardlist', {
localBoards, if (req.path === '/boards.json') {
webringBoards, res.json({
page, localBoards,
maxPage, webringBoards,
query: req.query, page,
search, maxPage,
queryString, });
}); } else {
res.render('boardlist', {
localBoards,
webringBoards,
page,
maxPage,
query: req.query,
search,
queryString,
});
}
} }

@ -5,12 +5,16 @@ const { buildCatalog } = require(__dirname+'/../../helpers/tasks.js');
module.exports = async (req, res, next) => { module.exports = async (req, res, next) => {
let html; let html;
try { try {
html = await buildCatalog({ board: res.locals.board }); ({ html } = await buildCatalog({ board: res.locals.board }));
} catch (err) { } catch (err) {
return next(err); return next(err);
} }
return res.send(html); if (req.path.endsWith('.json')) {
return res.json(json);
} else {
return res.send(html);
}
} }

@ -4,16 +4,20 @@ const { buildThread } = require(__dirname+'/../../helpers/tasks.js');
module.exports = async (req, res, next) => { module.exports = async (req, res, next) => {
let html; let html, json;
try { try {
html = await buildThread({ ({ html, json } = await buildThread({
threadId: res.locals.thread.postId, threadId: res.locals.thread.postId,
board: res.locals.board board: res.locals.board
}); }));
} catch (err) { } catch (err) {
return next(err); return next(err);
} }
return res.send(html); if (req.path.endsWith('.json')) {
return res.json(json);
} else {
return res.send(html);
}
} }

Loading…
Cancel
Save