server.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const fs = require('fs')
  2. const path = require('path')
  3. const Koa = require('koa')
  4. const Router = require('koa-router')
  5. const bodyParser = require('koa-bodyparser')
  6. const LRU = require('lru-cache')
  7. const WebSocket = require('ws')
  8. const koaStatic = require('koa-static')
  9. const routerOptions = {
  10. prefix: '/api'
  11. }
  12. const lruOptions = {
  13. max: 50,
  14. maxAge: 1000 * 60 * 60 * 24
  15. }
  16. const dataPath = './data/data.json'
  17. const staticPath = './dist'
  18. const router = new Router(routerOptions)
  19. const cache = new LRU(lruOptions)
  20. let STATIC_DATA = ''
  21. fs.readFile(dataPath, (err, buf) => {
  22. if (err) {
  23. console.error(err)
  24. return
  25. }
  26. STATIC_DATA = buf.toString()
  27. })
  28. const app = new Koa()
  29. const wss = new WebSocket.Server({ port: 5556 })
  30. app.use(koaStatic(path.join(__dirname, staticPath)))
  31. wss.on('connection', ws => {
  32. ws.on('message', msg => {
  33. const res = JSON.parse(msg)
  34. console.log('msg: ', res)
  35. if (res.code && res.status === 'update') {
  36. const list = cache.get(res.code)
  37. const data = JSON.stringify({ data: list })
  38. wss.clients.forEach(client => {
  39. if (client.readyState === WebSocket.OPEN) {
  40. client.send(data)
  41. }
  42. })
  43. }
  44. })
  45. })
  46. app.use(bodyParser())
  47. // 判断是不是api
  48. // app.use((ctx, next) => {
  49. // const url = ctx.url
  50. // const isApi = /^\/api/.test(url)
  51. // ctx.$isApi = isApi
  52. // next()
  53. // })
  54. router.get('/list', (ctx, next) => {
  55. const { code } = ctx.query
  56. let data
  57. if (cache.has(code)) {
  58. data = cache.get(code)
  59. } else {
  60. const jsonData = JSON.parse(STATIC_DATA).data || []
  61. const len = 25
  62. const sum = jsonData.length - len
  63. const arr = new Array(25).fill(1).map(el => Math.floor(Math.random() * sum))
  64. const groupList = [1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0]
  65. data = new Array(25).fill(1).map((el, i) => {
  66. const del = groupList.length ? Math.floor(Math.random() * groupList.length) : 0
  67. const group = groupList.splice(del, 1)[0]
  68. return {
  69. text: jsonData.splice(arr[i], 1)[0],
  70. status: 0,
  71. group
  72. }
  73. })
  74. cache.set(code, data)
  75. }
  76. ctx.body = {
  77. code,
  78. data
  79. }
  80. })
  81. router.put('/status/:code', ctx => {
  82. const { params, request } = ctx
  83. const { code } = params
  84. const { index } = request.body
  85. const list = cache.get(code) || []
  86. if (!index) {
  87. throw new Error('index is not find')
  88. }
  89. if (!list || !list.length) {
  90. throw new Error('cache list is not find')
  91. }
  92. list[index].status = list[index].status ? 0 : 1
  93. cache.set(code, list)
  94. ctx.body = { success: true, msg: '更新成功', data: list[index] }
  95. })
  96. app.use(router.routes()).use(router.allowedMethods())
  97. app.on('error', err => console.error(err))
  98. app.listen(5555)
  99. console.log('http://127.0.0.1:5555')