| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- 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 data = {
- id,
- uri: req.originalUrl,
- imageUrl: workUrl,
- applink,
- };
- // 渲染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;
|