From c65ee62257710d64c0c8f6de3eb204aff401332f Mon Sep 17 00:00:00 2001 From: Killer Boss Original <73131550+killerbossoriginal@users.noreply.github.com> Date: Fri, 8 Jul 2022 12:36:41 +0200 Subject: [PATCH] d --- index.js | 17 +++--- index.js.old | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 index.js.old diff --git a/index.js b/index.js index 183cabc..0607a52 100644 --- a/index.js +++ b/index.js @@ -82,7 +82,7 @@ async function searchElement(request, response) { authenticate(token, response, search) var elements = await db.get(word) -// var elements = JSON.parse(elements); + var elements = JSON.parse(elements); if(elements) { var reply = elements @@ -108,10 +108,11 @@ async function set(request, response) { authenticate(token, response, set) await db.set(element, data) - var res = await db.get(element) // var res = JSON.parse(res); if(res) { - var reply = res; + var reply = { + status: 'Done' + }; } else { var reply = { status:"404-Not Found" @@ -128,19 +129,19 @@ app.get('/api/:auth/database/:element/remove', remove); async function remove(request, response) { var token = request.params.auth; var element = request.params.element; - var data = request.params.data; set = 'remove '+element authenticate(token, response, set) - await db.set(element, data) - var res = await db.delete(element) + await db.delete(element) // var res = JSON.parse(res); if(res) { - var reply = res + var reply = { + status: 'Done' + } } else { var reply = { - status:"Not Found" + status:"404 - Not Found" } } diff --git a/index.js.old b/index.js.old new file mode 100644 index 0000000..183cabc --- /dev/null +++ b/index.js.old @@ -0,0 +1,148 @@ +var client = []; +client.config = require('./config.js').settings; + +const MapDB = require('quickmap.db'); +const db = new MapDB('database.json'); + +const chalk = require('chalk') + +function catchError(message, err, origin, reason) { + console.log(chalk.gray('—————————————————————————————————')); + console.log( + chalk.white('['), + chalk.red.bold('AntiCrash'), + chalk.white(']'), + chalk.gray(' : '), + chalk.white.bold(message), + ); + console.log(chalk.gray('—————————————————————————————————')); + console.log(err, origin, reason); +} + +process.on('unhandledRejection', (err, origin) => { + catchError('Unhandled Rejection/Catch', err, origin); +}); +process.on('uncaughtException', (err, origin) => { + catchError('Uncaught Exception/Catch', err, origin); +}); +process.on('multipleResolves', (type, promise, reason) => { + catchError('Multiple Resolves', type, promise, reason); +}); + +async function setup() { + var isset = await db.get('client.issetupped') + if (!isset) { + await db.set('client.issetupped', true) + } +} + +setup() +var fs = require('fs'); +var data = fs.readFileSync('./data/database.json'); + +var elements = JSON.parse(data); +const express = require("express"); +const app = express(); + +const cors=require('cors'); + +app.listen(client.config.port, + () => console.log("Server Start at the port "+client.config.port)); + +app.use(express.static('public')); +app.use(cors()); + +async function authenticate(token, response, what) { + if (!client.config.auth.includes(token)) { + response.send('ERROR - Not auth') + } else { + console.log(token + ' do '+ what) + } +} + +// All dates in the database + +app.get('/api/:auth/database', alldata); + +function alldata(request, response) { + var token = request.params.auth; + authenticate(token, response, 'alldata') + response.send(elements); +} + +// One element in the database + +app.get('/api/:auth/database/:element/', searchElement); + +async function searchElement(request, response) { + var token = request.params.auth; + var word = request.params.element; + + search = 'search '+ word + authenticate(token, response, search) + + var elements = await db.get(word) +// var elements = JSON.parse(elements); + + if(elements) { + var reply = elements + } else { + var reply = { + status:"Not Found" + } + } + + response.send(reply); +} + +// Set a db variable + +app.get('/api/:auth/database/:element/set/:data', set); + +async function set(request, response) { + var token = request.params.auth; + var element = request.params.element; + var data = request.params.data; + + set = 'set '+element+' to '+data + authenticate(token, response, set) + + await db.set(element, data) + var res = await db.get(element) +// var res = JSON.parse(res); + if(res) { + var reply = res; + } else { + var reply = { + status:"404-Not Found" + } + } + + response.send(reply); +} + +// Set a db variable + +app.get('/api/:auth/database/:element/remove', remove); + +async function remove(request, response) { + var token = request.params.auth; + var element = request.params.element; + var data = request.params.data; + + set = 'remove '+element + authenticate(token, response, set) + + await db.set(element, data) + var res = await db.delete(element) +// var res = JSON.parse(res); + if(res) { + var reply = res + } else { + var reply = { + status:"Not Found" + } + } + + response.send(reply); +} \ No newline at end of file