basic version of forms
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
import chalk from 'chalk'
|
||||
import path from 'path';
|
||||
import fs from 'fs/promises';
|
||||
import { pathToFileURL } from 'url';
|
||||
|
||||
export default class Database {
|
||||
#nodes;
|
||||
#edges;
|
||||
#labels = {}
|
||||
|
||||
constructor() {
|
||||
this.getData()
|
||||
}
|
||||
|
||||
async getData() {
|
||||
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
|
||||
let dbJson;
|
||||
try {
|
||||
dbJson = JSON.parse(dbData);
|
||||
} catch {
|
||||
dbJson = []
|
||||
}
|
||||
this.#nodes = dbJson["nodes"];
|
||||
this.#edges = dbJson["edges"];
|
||||
|
||||
console.log(chalk.yellow("DB established."))
|
||||
Object.preventExtensions(this);
|
||||
}
|
||||
|
||||
// superKey = "nodes" || "edges"
|
||||
async writeData(superKey, key, value) {
|
||||
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
|
||||
let dbJson;
|
||||
try {
|
||||
dbJson = JSON.parse(dbData);
|
||||
} catch {
|
||||
dbJson = []
|
||||
}
|
||||
|
||||
dbJson[superKey][key] = value;
|
||||
|
||||
await fs.writeFile(path.join(process.cwd(), 'db/db.json'), JSON.stringify(dbJson, null, 2), 'utf8')
|
||||
}
|
||||
|
||||
async getLabelModels() {
|
||||
const labelHandlers = {};
|
||||
const labelDir = path.join(process.cwd(), 'src/model/labels');
|
||||
const files = await fs.readdir(labelDir);
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.endsWith('.js')) continue;
|
||||
|
||||
const label = path.basename(file, '.js');
|
||||
const modulePath = path.join(labelDir, file);
|
||||
const module = await import(pathToFileURL(modulePath).href);
|
||||
labelHandlers[label] = module.default;
|
||||
|
||||
if (!this.#labels[label]) {
|
||||
this.#labels[label] = [];
|
||||
}
|
||||
}
|
||||
return labelHandlers
|
||||
}
|
||||
|
||||
generateUserID() {
|
||||
let id = this.#labels["User"].length + 1;
|
||||
while (this.get.user(`user-${id}`)) {
|
||||
id++;
|
||||
}
|
||||
return `user-${id}`; // O(1) most of the time
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
return { nodes: this.#nodes }
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,14 @@ import cors from 'cors'
|
||||
import http from 'http'
|
||||
import chalk from 'chalk'
|
||||
import moment from 'moment'
|
||||
import * as z from "zod";
|
||||
|
||||
import { initWebSocket } from './ws.js'
|
||||
import Database from "./db/db.js"
|
||||
import getAllDownloadsFiles from "./db/getDownloads.js"
|
||||
import getAllDownloadsFiles from "./getDownloads.js"
|
||||
import forms from "forms"
|
||||
|
||||
class Server {
|
||||
db;
|
||||
export default class Server {
|
||||
store;
|
||||
UIPath = path.join(__dirname, '../ui')
|
||||
|
||||
registerRoutes(router) {
|
||||
@@ -81,8 +82,25 @@ class Server {
|
||||
next();
|
||||
}
|
||||
|
||||
async connectToForms() {
|
||||
await this.store.connect()
|
||||
this.store.register("log", z.toJSONSchema(z.object({
|
||||
time: z.string(),
|
||||
host: z.string(),
|
||||
ip: z.string(),
|
||||
path: z.string(),
|
||||
method: z.string()
|
||||
})))
|
||||
this.store.add("log", {
|
||||
time: "0100",
|
||||
host: "parchment.page",
|
||||
ip: "0.0.0.1",
|
||||
path: "/asd",
|
||||
method: "GET"
|
||||
})
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.db = new Database()
|
||||
const app = express();
|
||||
app.use(cors({ origin: '*' }));
|
||||
app.use(express.json());
|
||||
@@ -94,12 +112,15 @@ class Server {
|
||||
this.registerRoutes(router)
|
||||
app.use('/', router);
|
||||
|
||||
this.store = new forms.client()
|
||||
this.connectToForms()
|
||||
|
||||
const server = http.createServer(app);
|
||||
initWebSocket(server);
|
||||
const PORT = 80;
|
||||
server.listen(PORT, () => {
|
||||
console.log("\n")
|
||||
console.log(chalk.yellow("**************Downloads****************"))
|
||||
console.log(chalk.yellow("**************Parchment****************"))
|
||||
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
||||
console.log(chalk.yellow("***************************************"))
|
||||
console.log("\n")
|
||||
@@ -113,6 +134,4 @@ class Server {
|
||||
|
||||
Object.preventExtensions(this);
|
||||
}
|
||||
}
|
||||
|
||||
const server = new Server()
|
||||
}
|
||||
Reference in New Issue
Block a user