Добавил api/verse

This commit is contained in:
Денис Буторин 2025-07-05 05:37:45 +03:00
parent c6d4f68aa4
commit 9b50f9e353
Signed by: dekabu
GPG key ID: 867F0C8041D69B4C

View file

@ -4,7 +4,6 @@ import fs from 'fs'
import { createServer } from 'http'
import path from 'path'
import querystring from 'querystring'
import { createHash } from 'crypto'
setConfig()
const Config = getConfig()
@ -63,9 +62,6 @@ const server = createServer(async (req, res) => {
const command = tokens[0]
switch (command) {
case 'api':
function error(text) {
sendText(text)
}
if (tokens.length == 1) {
sendText('API сайта kompoet.ru')
break
@ -78,6 +74,39 @@ const server = createServer(async (req, res) => {
req.on('end', () => {
body = querystring.parse(body)
switch (tokens[1]) {
case 'verse':
let data = {}
let index = Number(tokens[2])
if (isNaN(index))
index = 0
if (index < 0) {
res.statusCode = 404
sendText('Отрицательный индекс!')
break
}
let countVerse = db.getVerseCount()
let countBlock
let ostatok = countVerse % 20
if (ostatok == 0)
countBlock = countVerse / 20
else
countBlock = (countVerse - ostatok) / 20 + 1
if (index > countBlock - 1) {
res.statusCode = 404
sendText('Слишком большой индекс! Стихи закончились')
}
else {
let maxIndex = countVerse - index * 20
if (maxIndex >= 20)
for (let i = maxIndex; i > maxIndex - 20; --i)
data[i] = Verse[i]
else
for (let i = maxIndex; i > 0; --i)
data[i] = Verse[i]
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data))
}
break
case 'checklogin':
if (db.peopleByLogin(tokens[2]))
sendText('Логин занят')
@ -87,7 +116,7 @@ const server = createServer(async (req, res) => {
}
break
case 'checkpassword':
if (db.peopleByLogin(body.login).HASH == createHash('sha256').update(body.password).digest('hex'))
if (db.checkPassword(body.login, body.password))
sendText('Пароль верный')
else {
res.statusCode = 403
@ -110,6 +139,9 @@ const server = createServer(async (req, res) => {
res.sendText('Нет прав! Перезайдите')
}
break
default:
res.statusCode = 404
res.sendText(`API не имеет команды '${command}'`)
}
})
}