mit.db/index.js

186 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-07-05 01:30:38 +02:00
var client = [];
2022-07-04 19:37:50 +02:00
client.config = require('./config.js').settings;
const MapDB = require('quickmap.db');
const db = new MapDB('database.json');
2022-07-05 16:09:45 +02:00
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);
});
2022-07-05 13:26:02 +02:00
async function setup() {
var isset = await db.get('client.issetupped')
if (!isset) {
await db.set('client.issetupped', true)
}
}
setup()
2022-07-04 19:37:50 +02:00
var fs = require('fs');
var data = fs.readFileSync('./data/database.json');
2022-07-05 16:19:38 +02:00
var elements = JSON.parse(data);
2022-07-04 19:37:50 +02:00
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());
2022-07-05 15:12:14 +02:00
async function authenticate(token, response, what) {
2022-07-04 19:37:50 +02:00
if (!client.config.auth.includes(token)) {
2022-07-05 15:10:58 +02:00
response.send('ERROR - Not auth')
} else {
console.log(token + ' do '+ what)
2022-07-04 19:37:50 +02:00
}
}
2022-07-09 23:15:48 +02:00
/*
2022-07-04 19:37:50 +02:00
// All dates in the database
app.get('/api/:auth/database', alldata);
function alldata(request, response) {
var token = request.params.auth;
2022-07-05 15:12:14 +02:00
authenticate(token, response, 'alldata')
2022-07-04 19:37:50 +02:00
response.send(elements);
}
// One element in the database
app.get('/api/:auth/database/:element/', searchElement);
2022-07-05 01:29:50 +02:00
async function searchElement(request, response) {
2022-07-04 19:37:50 +02:00
var token = request.params.auth;
var word = request.params.element;
2022-07-05 15:10:58 +02:00
search = 'search '+ word
2022-07-05 15:12:14 +02:00
authenticate(token, response, search)
2022-07-05 15:10:58 +02:00
2022-07-04 19:37:50 +02:00
var elements = await db.get(word)
2022-07-08 12:43:07 +02:00
var elements = {
name: word,
value: elements
}
// console.log(elements)
// var elements = JSON.parse(elements);
// console.log(elements)
2022-07-04 19:37:50 +02:00
2022-07-05 14:58:49 +02:00
if(elements) {
2022-07-05 16:27:01 +02:00
var reply = elements
2022-07-05 15:10:58 +02:00
} else {
2022-07-04 19:37:50 +02:00
var reply = {
2022-07-08 12:46:31 +02:00
status:"404 - Not Found"
2022-07-04 19:37:50 +02:00
}
}
response.send(reply);
}
// Set a db variable
2022-07-09 23:15:48 +02:00
*/
app.post('/api/database/set', async (request, response) => {
2022-07-09 23:16:45 +02:00
console.log(request)
reply = 'Ok'
2022-07-09 23:21:38 +02:00
response.send(request);
2022-07-09 23:15:48 +02:00
});
2022-07-04 19:37:50 +02:00
2022-07-09 23:15:48 +02:00
/*
2022-07-09 19:59:57 +02:00
app.post('/api/:auth/database/:element/set/:data', async (request, response) => {
2022-07-08 20:02:32 +02:00
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 = {
status: 'Done'
};
} else {
var reply = {
status:"404 - Not Found"
}
}
response.send(reply);
});
2022-07-04 19:37:50 +02:00
async function set(request, response) {
var token = request.params.auth;
var element = request.params.element;
var data = request.params.data;
2022-07-05 15:10:58 +02:00
set = 'set '+element+' to '+data
2022-07-05 16:17:08 +02:00
authenticate(token, response, set)
2022-07-05 15:10:58 +02:00
2022-07-05 16:17:08 +02:00
await db.set(element, data)
2022-07-08 12:40:48 +02:00
var res = await db.get(element)
2022-07-05 16:28:42 +02:00
// var res = JSON.parse(res);
2022-07-04 19:37:50 +02:00
if(res) {
2022-07-08 12:36:41 +02:00
var reply = {
status: 'Done'
};
2022-07-05 15:10:58 +02:00
} else {
2022-07-04 19:37:50 +02:00
var reply = {
2022-07-08 12:46:31 +02:00
status:"404 - Not Found"
2022-07-04 19:37:50 +02:00
}
}
2022-07-05 15:20:23 +02:00
response.send(reply);
}
2022-07-08 19:57:54 +02:00
// Remove a db variable
2022-07-05 15:20:23 +02:00
app.get('/api/:auth/database/:element/remove', remove);
async function remove(request, response) {
var token = request.params.auth;
var element = request.params.element;
set = 'remove '+element
2022-07-05 16:15:30 +02:00
authenticate(token, response, set)
2022-07-05 15:20:23 +02:00
2022-07-08 12:36:41 +02:00
await db.delete(element)
2022-07-05 16:28:42 +02:00
// var res = JSON.parse(res);
2022-07-05 15:20:23 +02:00
if(res) {
2022-07-08 12:36:41 +02:00
var reply = {
status: 'Done'
}
2022-07-05 15:20:23 +02:00
} else {
var reply = {
2022-07-08 12:36:41 +02:00
status:"404 - Not Found"
2022-07-05 15:20:23 +02:00
}
}
2022-07-04 19:37:50 +02:00
response.send(reply);
2022-07-08 19:57:54 +02:00
}
2022-07-09 23:15:48 +02:00
*/