|
@@ -14,7 +14,11 @@ const lruOptions = {
|
|
|
max: 50,
|
|
|
maxAge: 1000 * 60 * 60 * 24
|
|
|
}
|
|
|
-const dataPath = './data/data.json'
|
|
|
+const watchFileOptions = {
|
|
|
+ interval: 60 * 10e2
|
|
|
+}
|
|
|
+const dataPath = './data/data.txt'
|
|
|
+const reviewDataPath = './data/reviewData.txt'
|
|
|
const staticPath = './dist'
|
|
|
|
|
|
const router = new Router(routerOptions)
|
|
@@ -26,7 +30,20 @@ fs.readFile(dataPath, (err, buf) => {
|
|
|
console.error(err)
|
|
|
return
|
|
|
}
|
|
|
- STATIC_DATA = buf.toString()
|
|
|
+ STATIC_DATA = buf.toString().replace('\n', '')
|
|
|
+})
|
|
|
+
|
|
|
+fs.watchFile(dataPath, watchFileOptions, (curr, prev) => {
|
|
|
+ if (curr.mtime !== prev.mtime) {
|
|
|
+ STATIC_DATA = ''
|
|
|
+ fs.readFile(dataPath, (err, buf) => {
|
|
|
+ if (err) {
|
|
|
+ console.error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ STATIC_DATA = buf.toString().split(',')
|
|
|
+ })
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
const app = new Koa()
|
|
@@ -78,7 +95,7 @@ router.get('/list', (ctx, next) => {
|
|
|
if (cache.has(code)) {
|
|
|
data = cache.get(code)
|
|
|
} else {
|
|
|
- const jsonData = JSON.parse(STATIC_DATA).data || []
|
|
|
+ const jsonData = STATIC_DATA || []
|
|
|
const len = 25
|
|
|
const sum = jsonData.length - len
|
|
|
const arr = new Array(25).fill(1).map(el => Math.floor(Math.random() * sum))
|
|
@@ -117,6 +134,68 @@ router.put('/status/:code', ctx => {
|
|
|
ctx.body = { success: true, msg: '更新成功', data: list[index] }
|
|
|
})
|
|
|
|
|
|
+router.post('/words', ctx => {
|
|
|
+ const { request } = ctx
|
|
|
+ const { words = '' } = request.body
|
|
|
+ const wordsList = words.split(',')
|
|
|
+ const noExist = []
|
|
|
+ const exist = wordsList.filter(el => {
|
|
|
+ if (STATIC_DATA.indexOf(el) !== -1) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ noExist.push(el)
|
|
|
+ return false
|
|
|
+ })
|
|
|
+ const pushNewWords = noExist.join(',')
|
|
|
+ fs.appendFile(reviewDataPath, pushNewWords, (err) => {
|
|
|
+ if (err) {
|
|
|
+ console.error('appendFile error:', err)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ let body = {}
|
|
|
+ if (exist.length) {
|
|
|
+ body = { success: false, exist }
|
|
|
+ } else {
|
|
|
+ body = { success: true }
|
|
|
+ }
|
|
|
+ ctx.body = body
|
|
|
+})
|
|
|
+router.get('/reviews', ctx => {
|
|
|
+ console.log(ctx.url)
|
|
|
+ const buf = fs.readFileSync(reviewDataPath)
|
|
|
+ const list = buf.toString().split(',')
|
|
|
+ ctx.body = { success: true, list }
|
|
|
+})
|
|
|
+router.post('/reviews', ctx => {
|
|
|
+ const { request } = ctx
|
|
|
+ const { words = '' } = request.body
|
|
|
+ const wordsList = words.split(',')
|
|
|
+ const noExist = []
|
|
|
+ const exist = wordsList.filter(el => {
|
|
|
+ if (STATIC_DATA.indexOf(el) !== -1) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ noExist.push(el)
|
|
|
+ return false
|
|
|
+ })
|
|
|
+ try {
|
|
|
+ fs.appendFileSync(dataPath, `,${noExist.join(',')}`)
|
|
|
+ fs.writeFile(reviewDataPath, '', err => {
|
|
|
+ if (err) {
|
|
|
+ return console.error('clear reviewData error:', err)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if (exist.length) {
|
|
|
+ ctx.body = { success: true, msg: `${exist.join(',')} 添加失败,词已存在` }
|
|
|
+ } else {
|
|
|
+ ctx.body = { success: true, msg: '添加成功' }
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('appendFile error:', error)
|
|
|
+ ctx.body = { success: false, msg: JSON.stringify(error) }
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
app.use(router.routes()).use(router.allowedMethods())
|
|
|
|
|
|
app.on('error', err => console.error(err))
|