index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. var express = require('express');
  2. var router = express.Router();
  3. const config = require('../../config/app');
  4. const models = require('../../models');
  5. const common = require('./common');
  6. const utils = require('../../libs/utils');
  7. const { getListBuilder } = require('../../libs/pager');
  8. const redis = require('../../libs/redis');
  9. const { coloringData } = require('./config');
  10. const CACHE_PREFIX = "art_v2";
  11. // const CACHE_EXPIRES = 60; // 60s刷新一次
  12. const CACHE_EXPIRES = 600;
  13. const artSelect = 'name title desc seoTitle seoDescription width height date publishTime tags lastMod mystery hasSpecial useSpecialThumb publishVersion totalStartCount totalDoneCount completionRate';
  14. // 首页路由
  15. router.get('/', function (req, res, next) { // 限制严格一点
  16. (async function () {
  17. let cacheKey = `${CACHE_PREFIX}_/`;
  18. let htmlData = await redis.getAsync(cacheKey);
  19. // htmlData = null;
  20. if (!htmlData) {
  21. // 最新上线
  22. let latest = await models.Art
  23. .find({ status: 9000 })
  24. .select(artSelect)
  25. .sort({ publishTime: 'desc' })
  26. .limit(12)
  27. .lean()
  28. .exec();
  29. common.organizeData(latest);
  30. let data = {
  31. latest,
  32. uri: req.originalUrl,
  33. };
  34. // 渲染EJS模板到内存中
  35. res.render('v2/index', data, async (err, html) => {
  36. if (err) {
  37. // 如果渲染出错,调用next()传递错误
  38. return next(err);
  39. }
  40. // 渲染成功,存redis, 发送数据到客户端
  41. htmlData = html;
  42. try {
  43. await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
  44. } catch (e) {
  45. console.error(e);
  46. }
  47. res.send(htmlData);
  48. });
  49. } else {
  50. // 缓存命中, 直接发送缓存数据
  51. res.set({ 'X-From-Cache': 'true' });
  52. res.send(htmlData);
  53. }
  54. })().catch(next);
  55. });
  56. // play页路由
  57. router.get('/play/:id', function (req, res, next) {
  58. (async function () {
  59. let id = req.params.id;
  60. utils.validators.validateId(id);
  61. let host = config.cdnHost ?? config.resHost;
  62. let workUrl = `${host}/thumbs/coloring-page/work/480/${id}.webp`;
  63. let cacheKey = `${CACHE_PREFIX}_play_${id}`;
  64. let htmlData = await redis.getAsync(cacheKey);
  65. if (!htmlData) {
  66. let doc = await models.Art.findById(id);
  67. if (!doc) throw createError(404, 'Art Not Found!');
  68. let data = {
  69. id,
  70. uri: req.originalUrl,
  71. imageUrl: workUrl,
  72. };
  73. // 渲染EJS模板到内存中
  74. res.render('v2/play', data, async (err, html) => {
  75. if (err) {
  76. // 如果渲染出错,调用next()传递错误
  77. return next(err);
  78. }
  79. // 渲染成功,存redis, 发送数据到客户端
  80. htmlData = html;
  81. try {
  82. await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
  83. } catch (e) {
  84. console.error(e);
  85. }
  86. res.send(htmlData);
  87. });
  88. } else {
  89. // 缓存命中, 直接发送缓存数据
  90. res.set({ 'X-From-Cache': 'true' });
  91. res.send(htmlData);
  92. }
  93. })().catch(next);
  94. });
  95. // myworks
  96. router.get('/myworks', function (req, res, next) {
  97. (async function () {
  98. let data = {
  99. host: config.cdnHost ?? config.resHost,
  100. uri: `/myworks`,
  101. };
  102. // 渲染EJS模板到内存中
  103. res.render('v2/myworks', data, async (err, html) => {
  104. if (err) {
  105. // 如果渲染出错,调用next()传递错误
  106. return next(err);
  107. }
  108. res.send(html);
  109. });
  110. })().catch(next);
  111. });
  112. // about
  113. router.get('/about', function (req, res, next) {
  114. (async function () {
  115. let cacheKey = `${CACHE_PREFIX}_/about`;
  116. let htmlData = await redis.getAsync(cacheKey);
  117. // htmlData = null;
  118. if (!htmlData) {
  119. let data = {
  120. uri: req.originalUrl,
  121. };
  122. // 渲染EJS模板到内存中
  123. res.render('v2/about', data, async (err, html) => {
  124. if (err) {
  125. // 如果渲染出错,调用next()传递错误
  126. return next(err);
  127. }
  128. // 渲染成功,存redis, 发送数据到客户端
  129. htmlData = html;
  130. try {
  131. await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
  132. } catch (e) {
  133. console.error(e);
  134. }
  135. res.send(htmlData);
  136. });
  137. } else {
  138. // 缓存命中, 直接发送缓存数据
  139. res.set({ 'X-From-Cache': 'true' });
  140. res.send(htmlData);
  141. }
  142. })().catch(next);
  143. });
  144. // app
  145. router.get('/app', function (req, res, next) {
  146. (async function () {
  147. let cacheKey = `${CACHE_PREFIX}_/app`;
  148. let htmlData = await redis.getAsync(cacheKey);
  149. // htmlData = null;
  150. if (!htmlData) {
  151. let data = {
  152. uri: req.originalUrl,
  153. };
  154. // 渲染EJS模板到内存中
  155. res.render('v2/app', data, async (err, html) => {
  156. if (err) {
  157. // 如果渲染出错,调用next()传递错误
  158. return next(err);
  159. }
  160. // 渲染成功,存redis, 发送数据到客户端
  161. htmlData = html;
  162. try {
  163. await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
  164. } catch (e) {
  165. console.error(e);
  166. }
  167. res.send(htmlData);
  168. });
  169. } else {
  170. // 缓存命中, 直接发送缓存数据
  171. res.set({ 'X-From-Cache': 'true' });
  172. res.send(htmlData);
  173. }
  174. })().catch(next);
  175. });
  176. // contact
  177. router.get('/contact', function (req, res, next) {
  178. (async function () {
  179. let cacheKey = `${CACHE_PREFIX}_/contact`;
  180. let htmlData = await redis.getAsync(cacheKey);
  181. // htmlData = null;
  182. if (!htmlData) {
  183. let data = {
  184. uri: req.originalUrl,
  185. };
  186. // 渲染EJS模板到内存中
  187. res.render('v2/contact', data, async (err, html) => {
  188. if (err) {
  189. // 如果渲染出错,调用next()传递错误
  190. return next(err);
  191. }
  192. // 渲染成功,存redis, 发送数据到客户端
  193. htmlData = html;
  194. try {
  195. await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
  196. } catch (e) {
  197. console.error(e);
  198. }
  199. res.send(htmlData);
  200. });
  201. } else {
  202. // 缓存命中, 直接发送缓存数据
  203. res.set({ 'X-From-Cache': 'true' });
  204. res.send(htmlData);
  205. }
  206. })().catch(next);
  207. });
  208. // faq
  209. router.get('/faq', function (req, res, next) {
  210. (async function () {
  211. let cacheKey = `${CACHE_PREFIX}_/faq`;
  212. let htmlData = await redis.getAsync(cacheKey);
  213. // htmlData = null;
  214. if (!htmlData) {
  215. let data = {
  216. uri: req.originalUrl,
  217. };
  218. // 渲染EJS模板到内存中
  219. res.render('v2/faq', data, async (err, html) => {
  220. if (err) {
  221. // 如果渲染出错,调用next()传递错误
  222. return next(err);
  223. }
  224. // 渲染成功,存redis, 发送数据到客户端
  225. htmlData = html;
  226. try {
  227. await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
  228. } catch (e) {
  229. console.error(e);
  230. }
  231. res.send(htmlData);
  232. });
  233. } else {
  234. // 缓存命中, 直接发送缓存数据
  235. res.set({ 'X-From-Cache': 'true' });
  236. res.send(htmlData);
  237. }
  238. })().catch(next);
  239. });
  240. // privacy-policy
  241. router.get('/privacy-policy', function (req, res, next) {
  242. (async function () {
  243. let cacheKey = `${CACHE_PREFIX}_/privacy`;
  244. let htmlData = await redis.getAsync(cacheKey);
  245. // htmlData = null;
  246. if (!htmlData) {
  247. let data = {
  248. uri: req.originalUrl,
  249. };
  250. // 渲染EJS模板到内存中
  251. res.render('v2/privacy', data, async (err, html) => {
  252. if (err) {
  253. // 如果渲染出错,调用next()传递错误
  254. return next(err);
  255. }
  256. // 渲染成功,存redis, 发送数据到客户端
  257. htmlData = html;
  258. try {
  259. await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
  260. } catch (e) {
  261. console.error(e);
  262. }
  263. res.send(htmlData);
  264. });
  265. } else {
  266. // 缓存命中, 直接发送缓存数据
  267. res.set({ 'X-From-Cache': 'true' });
  268. res.send(htmlData);
  269. }
  270. })().catch(next);
  271. });
  272. // terms-of-service
  273. router.get('/terms-of-service', function (req, res, next) {
  274. (async function () {
  275. let cacheKey = `${CACHE_PREFIX}_/terms-of-service`;
  276. let htmlData = await redis.getAsync(cacheKey);
  277. // htmlData = null;
  278. if (!htmlData) {
  279. let data = {
  280. uri: req.originalUrl,
  281. };
  282. // 渲染EJS模板到内存中
  283. res.render('v2/terms-of-service', data, async (err, html) => {
  284. if (err) {
  285. // 如果渲染出错,调用next()传递错误
  286. return next(err);
  287. }
  288. // 渲染成功,存redis, 发送数据到客户端
  289. htmlData = html;
  290. try {
  291. await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
  292. } catch (e) {
  293. console.error(e);
  294. }
  295. res.send(htmlData);
  296. });
  297. } else {
  298. // 缓存命中, 直接发送缓存数据
  299. res.set({ 'X-From-Cache': 'true' });
  300. res.send(htmlData);
  301. }
  302. })().catch(next);
  303. });
  304. module.exports = router;