Добавил API для списка стихов VersePart

This commit is contained in:
Денис Буторин 2025-07-06 08:38:19 +03:00
parent 77cf021b54
commit 247dfd2ea1
Signed by: dekabu
GPG key ID: 867F0C8041D69B4C
3 changed files with 52 additions and 4 deletions

View file

@ -1,5 +1,7 @@
import { getConfig } from './config.js'
import fs from 'fs'
import readline from 'readline'
import path from 'path'
import { createHash } from 'crypto'
const DB = getConfig().DB
@ -14,6 +16,20 @@ function getVerse() {
return Verse
}
async function getPreview(number) {
const rl = readline.createInterface({
input: fs.createReadStream(path.join(DB, 'verse', String(number), 'text')),
crlfDelay: Infinity
})
const lines = []
for await (const line of rl) {
lines.push(line)
if (lines.length >= 4)
break
}
return lines.join('\n')
}
function getPeopleCount() {
return Object.keys(People).length
}
@ -92,6 +108,7 @@ export default {
getVerse,
getPeopleCount,
getVerseCount,
getPreview,
peopleNumberByLogin,
peopleByLogin,
checkPassword,

View file

@ -72,11 +72,32 @@ const server = createServer(async (req, res) => {
body += chunk.toString()
})
req.on('end', () => {
let index
let data
body = querystring.parse(body)
switch (tokens[1]) {
case 'verse':
let data = {}
let index = Number(tokens[2])
case 'people':
index = Number(tokens[2])
if (isNaN(index)) {
data = People
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data))
}
else {
if (People[index]) {
data = People[index]
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data))
}
else {
res.statusCode = 404
res.sendText('Нет человека с таким индексом!')
}
}
break
case 'versepart':
data = {}
index = Number(tokens[2])
if (isNaN(index))
index = 0
if (index < 0) {
@ -107,6 +128,16 @@ const server = createServer(async (req, res) => {
res.end(JSON.stringify(data))
}
break
case 'pre':
index = tokens[2]
if (Verse[index]) {
db.getPreview(tokens[2]).then(text => sendText(text))
}
else {
res.statusCode = 404
sendText('Не найдено!')
}
break
case 'checklogin':
if (db.peopleByLogin(tokens[2]))
sendText('Логин занят')

2
www

@ -1 +1 @@
Subproject commit c6cbc86b8a361f59d5f06dc2eb6581c21da51d4f
Subproject commit b7d40913c60c43e216f8d1f65f7ba6b64e215324