var express = require('express'); var router = express.Router(); const config = require('../../config/app'); const models = require('../../models'); const common = require('./common'); const utils = require('../../libs/utils'); const { getListBuilder } = require('../../libs/pager'); const redis = require('../../libs/redis'); const { coloringData } = require('./config'); const CACHE_PREFIX = "art_v2"; // const CACHE_EXPIRES = 60; // 60s刷新一次 const CACHE_EXPIRES = 600; const artSelect = 'name title desc seoTitle seoDescription width height date publishTime tags lastMod mystery hasSpecial useSpecialThumb publishVersion totalStartCount totalDoneCount completionRate'; // 首页路由 router.get('/', function (req, res, next) { // 限制严格一点 (async function () { let cacheKey = `${CACHE_PREFIX}_/`; let htmlData = await redis.getAsync(cacheKey); htmlData = null; if (!htmlData) { // 最新上线 let latest = await models.Art .find({ status: 9000 }) .select(artSelect) .populate('user', 'username name') .sort({ publishTime: 'desc' }) .limit(12) .lean() .exec(); common.organizeData(latest); let data = { latest, uri: req.originalUrl, }; // 渲染EJS模板到内存中 res.render('v2/index', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } // 渲染成功,存redis, 发送数据到客户端 htmlData = html; try { await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES); } catch (e) { console.error(e); } res.send(htmlData); }); } else { // 缓存命中, 直接发送缓存数据 res.set({ 'X-From-Cache': 'true' }); res.send(htmlData); } })().catch(next); }); // play页路由 router.get('/play/:id', function (req, res, next) { (async function () { let id = req.params.id; utils.validators.validateId(id); let host = config.cdnHost ?? config.resHost; let workUrl = `${host}/thumbs/coloring-page/work/480/${id}.webp`; // let cacheKey = `${CACHE_PREFIX}_play_${id}`; // let htmlData = await redis.getAsync(cacheKey); let htmlData = null; if (!htmlData) { let doc = await models.Art.findById(id); if (!doc) throw createError(404, 'Art Not Found!'); // deeplink 相关 let applink = `https://art.pcoloring.com${req.originalUrl}`; let sharePageUrl = `https://art.pcoloring.com/share/${id}`; let data = { id, uri: req.originalUrl, imageUrl: workUrl, applink, sharePageUrl, }; // 渲染EJS模板到内存中 res.render('v2/play', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } // 渲染成功,存redis, 发送数据到客户端 htmlData = html; // try { // await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES); // } catch (e) { // console.error(e); // } res.send(htmlData); }); } else { // 缓存命中, 直接发送缓存数据 res.set({ 'X-From-Cache': 'true' }); res.send(htmlData); } })().catch(next); }); // myworks router.get('/myworks', function (req, res, next) { (async function () { let data = { host: config.cdnHost ?? config.resHost, uri: `/myworks`, }; // 渲染EJS模板到内存中 res.render('v2/myworks', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } res.send(html); }); })().catch(next); }); // about router.get('/about', function (req, res, next) { (async function () { let cacheKey = `${CACHE_PREFIX}_/about`; let htmlData = await redis.getAsync(cacheKey); // htmlData = null; if (!htmlData) { let data = { uri: req.originalUrl, }; // 渲染EJS模板到内存中 res.render('v2/about', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } // 渲染成功,存redis, 发送数据到客户端 htmlData = html; try { await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES); } catch (e) { console.error(e); } res.send(htmlData); }); } else { // 缓存命中, 直接发送缓存数据 res.set({ 'X-From-Cache': 'true' }); res.send(htmlData); } })().catch(next); }); // app router.get('/app', function (req, res, next) { (async function () { let cacheKey = `${CACHE_PREFIX}_/app`; let htmlData = await redis.getAsync(cacheKey); // htmlData = null; if (!htmlData) { let data = { uri: req.originalUrl, }; // 渲染EJS模板到内存中 res.render('v2/app', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } // 渲染成功,存redis, 发送数据到客户端 htmlData = html; try { await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES); } catch (e) { console.error(e); } res.send(htmlData); }); } else { // 缓存命中, 直接发送缓存数据 res.set({ 'X-From-Cache': 'true' }); res.send(htmlData); } })().catch(next); }); // contact router.get('/contact', function (req, res, next) { (async function () { let cacheKey = `${CACHE_PREFIX}_/contact`; let htmlData = await redis.getAsync(cacheKey); // htmlData = null; if (!htmlData) { let data = { uri: req.originalUrl, }; // 渲染EJS模板到内存中 res.render('v2/contact', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } // 渲染成功,存redis, 发送数据到客户端 htmlData = html; try { await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES); } catch (e) { console.error(e); } res.send(htmlData); }); } else { // 缓存命中, 直接发送缓存数据 res.set({ 'X-From-Cache': 'true' }); res.send(htmlData); } })().catch(next); }); // faq router.get('/faq', function (req, res, next) { (async function () { let cacheKey = `${CACHE_PREFIX}_/faq`; let htmlData = await redis.getAsync(cacheKey); // htmlData = null; if (!htmlData) { let data = { uri: req.originalUrl, }; // 渲染EJS模板到内存中 res.render('v2/faq', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } // 渲染成功,存redis, 发送数据到客户端 htmlData = html; try { await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES); } catch (e) { console.error(e); } res.send(htmlData); }); } else { // 缓存命中, 直接发送缓存数据 res.set({ 'X-From-Cache': 'true' }); res.send(htmlData); } })().catch(next); }); // privacy-policy router.get('/privacy-policy', function (req, res, next) { (async function () { let cacheKey = `${CACHE_PREFIX}_/privacy`; let htmlData = await redis.getAsync(cacheKey); // htmlData = null; if (!htmlData) { let data = { uri: req.originalUrl, }; // 渲染EJS模板到内存中 res.render('v2/privacy', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } // 渲染成功,存redis, 发送数据到客户端 htmlData = html; try { await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES); } catch (e) { console.error(e); } res.send(htmlData); }); } else { // 缓存命中, 直接发送缓存数据 res.set({ 'X-From-Cache': 'true' }); res.send(htmlData); } })().catch(next); }); // terms-of-service router.get('/terms-of-service', function (req, res, next) { (async function () { let cacheKey = `${CACHE_PREFIX}_/terms-of-service`; let htmlData = await redis.getAsync(cacheKey); // htmlData = null; if (!htmlData) { let data = { uri: req.originalUrl, }; // 渲染EJS模板到内存中 res.render('v2/terms-of-service', data, async (err, html) => { if (err) { // 如果渲染出错,调用next()传递错误 return next(err); } // 渲染成功,存redis, 发送数据到客户端 htmlData = html; try { await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES); } catch (e) { console.error(e); } res.send(htmlData); }); } else { // 缓存命中, 直接发送缓存数据 res.set({ 'X-From-Cache': 'true' }); res.send(htmlData); } })().catch(next); }); module.exports = router;