Разделил на три файла: config, db, server

This commit is contained in:
Денис Буторин 2025-05-16 14:26:39 +03:00
parent d5638cd450
commit 3241efc495
5 changed files with 113 additions and 29 deletions

1
run
View file

@ -1 +0,0 @@
src/main.js

3
run Executable file
View file

@ -0,0 +1,3 @@
#!/bin/sh
exec node src/server.js

49
src/config.js Normal file
View file

@ -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
}

46
src/db.js Normal file
View file

@ -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
}

View file

@ -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))

15
src/server.js Normal file
View file

@ -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('Сервер запущен!'))