Добавил 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 { getConfig } from './config.js'
import fs from 'fs' import fs from 'fs'
import readline from 'readline'
import path from 'path'
import { createHash } from 'crypto' import { createHash } from 'crypto'
const DB = getConfig().DB const DB = getConfig().DB
@ -14,6 +16,20 @@ function getVerse() {
return Verse 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() { function getPeopleCount() {
return Object.keys(People).length return Object.keys(People).length
} }
@ -92,6 +108,7 @@ export default {
getVerse, getVerse,
getPeopleCount, getPeopleCount,
getVerseCount, getVerseCount,
getPreview,
peopleNumberByLogin, peopleNumberByLogin,
peopleByLogin, peopleByLogin,
checkPassword, checkPassword,

View file

@ -72,11 +72,32 @@ const server = createServer(async (req, res) => {
body += chunk.toString() body += chunk.toString()
}) })
req.on('end', () => { req.on('end', () => {
let index
let data
body = querystring.parse(body) body = querystring.parse(body)
switch (tokens[1]) { switch (tokens[1]) {
case 'verse': case 'people':
let data = {} index = Number(tokens[2])
let 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)) if (isNaN(index))
index = 0 index = 0
if (index < 0) { if (index < 0) {
@ -107,6 +128,16 @@ const server = createServer(async (req, res) => {
res.end(JSON.stringify(data)) res.end(JSON.stringify(data))
} }
break 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': case 'checklogin':
if (db.peopleByLogin(tokens[2])) if (db.peopleByLogin(tokens[2]))
sendText('Логин занят') sendText('Логин занят')

2
www

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