Добавил возможностей, расширил api
This commit is contained in:
parent
9177e4bd65
commit
a4715325cc
3 changed files with 108 additions and 46 deletions
24
src/db.js
24
src/db.js
|
@ -1,5 +1,6 @@
|
|||
import { getConfig } from './config.js'
|
||||
import fs from 'fs'
|
||||
import { createHash } from 'crypto'
|
||||
|
||||
const DB = getConfig().DB
|
||||
|
||||
|
@ -20,6 +21,13 @@ function getVerseCount() {
|
|||
return Object.keys(Verse).length
|
||||
}
|
||||
|
||||
function peopleByLogin(login) {
|
||||
for (let p in People)
|
||||
if (People[p].LOGIN == login)
|
||||
return People[p]
|
||||
return false
|
||||
}
|
||||
|
||||
function Init() {
|
||||
const people = fs.readdirSync(DB + '/people')
|
||||
for (let p of people) {
|
||||
|
@ -34,15 +42,14 @@ function Init() {
|
|||
}
|
||||
}
|
||||
|
||||
function addPeople(name, subname, pseudonym, login, email) {
|
||||
function addPeople(name, subname, login, password) {
|
||||
const p = {
|
||||
NAME: name,
|
||||
SUBNAME: subname,
|
||||
PSEUDONYM: pseudonym,
|
||||
LOGIN: login,
|
||||
EMAIL: email
|
||||
HASH: createHash('sha256').update(password).digest('hex')
|
||||
}
|
||||
const index = Number(Object.keys(People)[getPeopleCount() - 1]) + 1
|
||||
const index = getPeopleCount() == 0 ? 1 : Number(Object.keys(People)[getPeopleCount() - 1]) + 1
|
||||
|
||||
People[index] = p
|
||||
|
||||
|
@ -73,5 +80,12 @@ function addVerse(title, author, genre, text) {
|
|||
}
|
||||
|
||||
export default {
|
||||
getPeople, getVerse, getPeopleCount, getVerseCount, Init, addPeople, addVerse
|
||||
getPeople,
|
||||
getVerse,
|
||||
getPeopleCount,
|
||||
getVerseCount,
|
||||
peopleByLogin,
|
||||
Init,
|
||||
addPeople,
|
||||
addVerse
|
||||
}
|
||||
|
|
128
src/server.js
128
src/server.js
|
@ -2,35 +2,46 @@ import { setConfig, getConfig } from './config.js'
|
|||
import db from './db.js'
|
||||
import fs from 'fs'
|
||||
import { createServer } from 'http'
|
||||
import path from 'path'
|
||||
import querystring from 'querystring'
|
||||
import { createHash } from 'crypto'
|
||||
|
||||
setConfig()
|
||||
const Config = getConfig()
|
||||
|
||||
const HEAD = fs.readFileSync(path.join(Config.WWW, 'head.html')).toString()
|
||||
const TAIL = fs.readFileSync(path.join(Config.WWW, 'tail.html')).toString()
|
||||
|
||||
const formats = {
|
||||
'.html': 'text/html',
|
||||
'.css': 'text/css',
|
||||
'.js': 'text/javascript'
|
||||
}
|
||||
|
||||
db.Init()
|
||||
|
||||
const Config = getConfig()
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
function sendText(text) {
|
||||
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
|
||||
res.end(text)
|
||||
}
|
||||
function sendFile(path) {
|
||||
fs.readFile(path, {}, (err, data) => {
|
||||
function sendFile(file) {
|
||||
fs.readFile(file, {}, (err, data) => {
|
||||
if (err) {
|
||||
sendText('Не удалось отправить Вам файл :(')
|
||||
}
|
||||
else {
|
||||
const lastDotIndex = path.lastIndexOf('.')
|
||||
if (lastDotIndex != -1) {
|
||||
const format = path.slice(lastDotIndex + 1)
|
||||
}
|
||||
let ContentType = formats[path.extname(file)]
|
||||
if (!ContentType)
|
||||
ContentType = 'text/plain'
|
||||
res.setHeader('Content-Type', ContentType)
|
||||
res.end(data)
|
||||
}
|
||||
})
|
||||
}
|
||||
function notFound() {
|
||||
res.statusCode = 404
|
||||
fs.readFile(Config.WWW + '/404.html', {}, (err, data) => {
|
||||
fs.readFile(path.join(Config.WWW, '404.html'), (err, data) => {
|
||||
if (err)
|
||||
sendText('Файл не найден!')
|
||||
else {
|
||||
|
@ -44,55 +55,92 @@ const server = createServer(async (req, res) => {
|
|||
|
||||
const tokens = pathname.split('/').filter(el => el != '')
|
||||
const People = db.getPeople()
|
||||
const Verse = db.getVerse()
|
||||
|
||||
if (tokens.length == 0)
|
||||
sendFile(Config.WWW + '/index.html')
|
||||
sendFile(path.join(Config.WWW, '/index.html'))
|
||||
else {
|
||||
switch (tokens[0]) {
|
||||
const command = tokens[0]
|
||||
switch (command) {
|
||||
case 'api':
|
||||
function error(text) {
|
||||
sendText(text)
|
||||
}
|
||||
let OK
|
||||
switch (tokens[1]) {
|
||||
case 'login':
|
||||
OK = true
|
||||
for (let p in People) {
|
||||
if (People[p].LOGIN == tokens[2]) {
|
||||
sendText('Пользователь с таким логином уже есть в системе!')
|
||||
OK = false
|
||||
break
|
||||
if (tokens.length == 1) {
|
||||
sendText('API сайта kompoet.ru')
|
||||
break
|
||||
}
|
||||
else if (req.method == 'GET')
|
||||
switch (tokens[1]) {
|
||||
case 'checklogin':
|
||||
if (db.peopleByLogin(tokens[2]))
|
||||
sendText('Логин занят')
|
||||
else {
|
||||
res.statusCode = 404
|
||||
sendText('Логин свободен')
|
||||
}
|
||||
}
|
||||
if (OK) {
|
||||
sendText('Логин свободен!')
|
||||
}
|
||||
break
|
||||
case 'email':
|
||||
OK = true
|
||||
for (let p in People) {
|
||||
if (People[p].EMAIL == tokens[2]) {
|
||||
sendText('Пользователь с такой почтой уже есть в системе!')
|
||||
OK = false
|
||||
break
|
||||
}
|
||||
else {
|
||||
let body = ''
|
||||
req.on('data', chunck => {
|
||||
body += chunck.toString()
|
||||
})
|
||||
req.on('end', () => {
|
||||
body = querystring.parse(body)
|
||||
switch (tokens[1]) {
|
||||
case 'signup':
|
||||
db.addPeople(body.name, body.subname, body.login, body.password)
|
||||
res.writeHead(301, {'location': '/'})
|
||||
res.end()
|
||||
break
|
||||
case 'login':
|
||||
const p = db.peopleByLogin(body.login)
|
||||
if (!p)
|
||||
res.writeHead(301, {'location': '/login?unknown'})
|
||||
else if (createHash('sha256').update(body.password).digest('hex') != p.HASH)
|
||||
res.writeHead(301, {'location': '/login?password'})
|
||||
res.end()
|
||||
break
|
||||
}
|
||||
}
|
||||
if (OK) {
|
||||
sendText('Почта свободна!')
|
||||
}
|
||||
break
|
||||
default:
|
||||
error(`ОШИБКА: в API нет метода '${tokens[1]}'`)
|
||||
})
|
||||
}
|
||||
break
|
||||
case 'signup':
|
||||
if (tokens.length == 1)
|
||||
sendFile(Config.WWW + '/signup.html')
|
||||
sendFile(path.join(Config.WWW, 'signup.html'))
|
||||
else
|
||||
notFound()
|
||||
break
|
||||
case 'login':
|
||||
if (tokens.length == 1)
|
||||
sendFile(path.join(Config.WWW, 'login.html'))
|
||||
else
|
||||
notFound()
|
||||
break
|
||||
default:
|
||||
const filePath = Config.WWW + pathname
|
||||
if (db.peopleByLogin(command)) {
|
||||
sendText('Есть такой пользователь!')
|
||||
}
|
||||
else if (/^\d+$/.test(command)) {
|
||||
const v = Verse[command]
|
||||
if (v) {
|
||||
const author = People[v.AUTHOR]
|
||||
const fullName = `${author.NAME} ${author.SUBNAME}`
|
||||
let ret = HEAD.replace('${TITLE}', `${v.TITLE} | ${fullName}`)
|
||||
ret += `<div id="fullverse"><h1>${v.TITLE}</h1><a href="${author.LOGIN}"><i>${fullName}</i></a>`
|
||||
fs.readFile(path.join(Config.DB, 'verse', command, 'text'), 'utf8', (err, data) => {
|
||||
if (!err) {
|
||||
ret += `<pre>${data}</pre>${TAIL}</div>`
|
||||
res.end(ret)
|
||||
}
|
||||
})
|
||||
}
|
||||
else
|
||||
notFound()
|
||||
break
|
||||
}
|
||||
const filePath = path.join(Config.WWW, pathname)
|
||||
fs.exists(filePath, (e) => {
|
||||
if (e)
|
||||
sendFile(filePath)
|
||||
|
|
2
www
2
www
|
@ -1 +1 @@
|
|||
Subproject commit e3f6c44f1ece1c74dd90c645695841058f019dea
|
||||
Subproject commit dde28033ce15c13f85a1613ff3fbaa045a0840df
|
Loading…
Add table
Add a link
Reference in a new issue