diff --git a/run b/run deleted file mode 120000 index fbe6a39..0000000 --- a/run +++ /dev/null @@ -1 +0,0 @@ -src/main.js \ No newline at end of file diff --git a/run b/run new file mode 100755 index 0000000..6b9f7e6 --- /dev/null +++ b/run @@ -0,0 +1,3 @@ +#!/bin/sh + +exec node src/server.js diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..eba12b1 --- /dev/null +++ b/src/config.js @@ -0,0 +1,49 @@ +import { existsSync, readFileSync, writeFileSync } from 'fs' +import { cwd } from 'process' +import { join } from 'path' + +const CWD = cwd() + +var PORT = 8000 +var DB = join(CWD, 'db') +var WWW = join(CWD, 'www') + +function set() { + if (existsSync('config.json')) { + console.log('Найден файл config.json') + let json = JSON.parse(readFileSync('config.json')) + PORT = json.PORT || PORT + DB = json.DB || DB + WWW = json.WWW || WWW + } + else { + console.log('Не найден файл config.json. Конфигурация по умолчанию') + } + console.log(`Порт: ${PORT}`) + console.log(`База данных: ${DB}`) + console.log(`Статичные файлы: ${WWW}`) + + let json = { + PORT: PORT, + DB: DB, + WWW: WWW + } + writeFileSync('config.json', JSON.stringify(json, null, 2)) + console.log('Конфигурация записана в config.json') +} + +function getPort() { + return PORT +} + +function getDB() { + return DB +} + +function getWWW() { + return WWW +} + +export default { + set, getPort, getDB, getWWW +} diff --git a/src/db.js b/src/db.js new file mode 100644 index 0000000..70d58cd --- /dev/null +++ b/src/db.js @@ -0,0 +1,46 @@ +import fs from 'fs' +import { join } from 'path' + +let Path +let Persons = {} +let Poetry = {} + +function setPath(path) { + Path = path +} + +function init() { + + // Создаём файловую структуру + ['', 'persons', 'poetry'].map(sub => { + let dir = join(Path, sub) + if (! fs.existsSync(dir)) + fs.mkdirSync(dir) + }) + + const personpath = join(Path, 'persons') + for (let id of fs.readdirSync(personpath)) { + Persons[id] = JSON.parse(fs.readFileSync(join(personpath, id, 'info.json'))) + } + + const poetrypath = join(Path, 'poetry') + for (let id of fs.readdirSync(poetrypath)) { + Poetry[id] = JSON.parse(fs.readFileSync(join(poetrypath, id, 'info.json'))) + } + + console.log('База данных инициализированна!') + console.log(`Людей: ${Object.keys(Persons).length}`) + console.log(`Стихов: ${Object.keys(Poetry).length}`) +} + +function getPersons() { + return Persons +} + +function getPoetry() { + return Poetry +} + +export default { + setPath, init, getPersons, getPoetry +} diff --git a/src/main.js b/src/main.js deleted file mode 100755 index 4a91865..0000000 --- a/src/main.js +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/node - -import fs from 'fs' -import path from 'path' -import process from 'process' - -const Config = { - PORT: 8000, - DB: path.resolve(process.cwd(), 'db'), - WWW: path.resolve(process.cwd(), 'www') -} - -if (fs.existsSync('config.json')) { - console.log('Найден файл config.json') - let json = JSON.parse(fs.readFileSync('config.json')); - - Config.PORT = json.PORT || Config.PORT - Config.DB = json.DB || Config.DB - Config.WWW = json.WWW || Config.WWW -} -else - console.log('Файл config.json не найден. Конфигурация по умолчанию.') - -console.log(`Порт: ${Config.PORT}`) -console.log(`База данных: ${Config.DB}`) -console.log(`Веб-контент: ${Config.WWW}`) - -fs.writeFileSync('config.json', JSON.stringify(Config, null, 2)) diff --git a/src/server.js b/src/server.js new file mode 100644 index 0000000..8ffee45 --- /dev/null +++ b/src/server.js @@ -0,0 +1,15 @@ +import config from './config.js' +import db from './db.js' +import { createServer } from 'http' + +config.set() + +db.setPath(config.getDB()) + +db.init() + +const server = createServer((req, res) => { + res.end('Hello world!') +}) + +server.listen(config.getPort(), console.log('Сервер запущен!'))