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 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-05 13:27:19 +02:00
|
|
|
/*
|
2022-07-04 19:37:50 +02:00
|
|
|
var fs = require('fs');
|
|
|
|
var data = fs.readFileSync('./data/database.json');
|
|
|
|
|
2022-07-05 13:27:19 +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:10:58 +02:00
|
|
|
async function authenticate(token, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All dates in the database
|
|
|
|
|
|
|
|
app.get('/api/:auth/database', alldata);
|
|
|
|
|
|
|
|
function alldata(request, response) {
|
|
|
|
var token = request.params.auth;
|
2022-07-05 15:10:58 +02:00
|
|
|
authenticate(token, '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
|
|
|
|
authenticate(token, search)
|
|
|
|
|
2022-07-04 19:37:50 +02:00
|
|
|
var elements = await db.get(word)
|
|
|
|
|
2022-07-05 14:58:49 +02:00
|
|
|
if(elements) {
|
|
|
|
var reply = elements;
|
2022-07-05 15:10:58 +02:00
|
|
|
} else {
|
2022-07-04 19:37:50 +02:00
|
|
|
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;
|
2022-07-05 15:10:58 +02:00
|
|
|
|
|
|
|
set = 'set '+element+' to '+data
|
|
|
|
authenticate(token, set)
|
|
|
|
|
2022-07-04 19:37:50 +02:00
|
|
|
await db.set(element, data)
|
|
|
|
const res = await db.get(element)
|
|
|
|
if(res) {
|
|
|
|
var reply = res;
|
2022-07-05 15:10:58 +02:00
|
|
|
} else {
|
2022-07-04 19:37:50 +02:00
|
|
|
var reply = {
|
|
|
|
status:"Not Found"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.send(reply);
|
2022-07-05 15:10:58 +02:00
|
|
|
}
|