d
This commit is contained in:
parent
c0b49e0bdd
commit
c65ee62257
2 changed files with 157 additions and 8 deletions
17
index.js
17
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"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
148
index.js.old
Normal file
148
index.js.old
Normal file
|
@ -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);
|
||||
}
|
Loading…
Reference in a new issue