· 7 years ago · Feb 24, 2019, 07:58 PM
1const databaseFile = './.data/rbx-sqlite3-db003.db'; // If you need to reset EVERYTHING, you can change this.
2
3const tables = [ // These are created once if alwaysCreateTables is false, however if it is set to true these all will be made each time (will not reset data, but will create any new table entries)
4 "table1", // All tables have two rows, key and value
5 "table2",
6 "table3"
7];
8
9const alwaysCreateTables = true;
10
11const ApiToken = "USE A PASSWORD GENERATOR"; // Highly recommended: https://www.grc.com/passwords.htm
12
13const tableKeyLength = 150;
14const tableValueLength = 3500;
15
16const getAsyncAllowStar = true; // Allow "*" to be sent to the server to return all data from a table.
17
18/*
19 SETTINGS ABOVE
20*/
21var $stmt;
22var fs = require('fs');
23
24const sqlite3 = require('better-sqlite3');
25
26var dbExists = fs.existsSync(databaseFile);
27const Database = new sqlite3(databaseFile);
28const json = JSON.stringify;
29
30var express = require('express');
31var bodyParser = require('body-parser');
32var app = express();
33app.use(bodyParser.json());
34if (!dbExists || alwaysCreateTables) {
35 /* fill database with tables */
36 tables.forEach(function(v, i) {
37 Database.prepare("CREATE TABLE IF NOT EXISTS "+v+" (key VARCHAR("+tableKeyLength+") PRIMARY KEY, value VARCHAR("+tableValueLength+") NOT NULL)").run();
38 });
39}
40function isValidTable(tab) {
41 return (tables.indexOf(tab) !== -1); // prepared statements don't allow you to specify a table, so we have to whitelist tables specified above.
42}
43
44
45app.delete("/deleteAsync", function(req, res) {
46 if (req.headers["apitoken"] == ApiToken) {
47 if (req.body.Key != null && req.body.Table != null) {
48 if (isValidTable(req.body.Table)) {
49 $stmt = Database.prepare("DELETE FROM `"+ req.body.Table +"` WHERE `key`=?");
50 var data = $stmt.run(req.body.Key);
51 res.status(200);
52 res.send(json({
53 Success: true,
54 KeyDeleted: data.changes > 0
55 }));
56 } else {
57 res.status(404);
58 res.send(json({
59 Success: false,
60 Message: "Table doesn't exist"
61 }));
62 }
63 } else {
64 res.status(400);
65 res.send(json({
66 Success: false,
67 Message: "Invalid request"
68 }));
69 }
70 } else {
71 res.status(403);
72 res.send(json({
73 Success: false,
74 Message: "You are unauthorized to make requests to this host."
75 }));
76 }
77});
78
79app.post("/postAsync", function(req, res) {
80 if (req.headers["apitoken"] == ApiToken) {
81 if (req.body.Key != null && req.body.Value != null && req.body.Table != null) {
82 if (isValidTable(req.body.Table)) {
83 $stmt = Database.prepare("REPLACE INTO `"+ req.body.Table +"` (key, value) VALUES (?, ?)"); // Create value if not exist, change value if exist.
84 var changes = $stmt.run(req.body.Key, req.body.Value).changes;
85 res.status(200);
86 res.send(json({
87 Success: true,
88 Changes: changes
89 }));
90 } else {
91 res.status(404);
92 res.send(json({
93 Success: false,
94 Message: "Table doesn't exist"
95 }));
96 }
97 } else {
98 res.status(400);
99 res.send(json({
100 Success: false,
101 Message: "Invalid request"
102 }));
103 }
104 } else {
105 res.status(403);
106 res.send(json({
107 Success: false,
108 Message: "You are unauthorized to make requests to this host."
109 }));
110 }
111});
112
113app.post("/getAsync", function(req, res) {
114 if (req.headers["apitoken"] == ApiToken) {
115 if (req.body.Key != null && req.body.Table != null) {
116 if (isValidTable(req.body.Table)) {
117 if (req.body.Key == "*" && getAsyncAllowStar) {
118 $stmt = Database.prepare("SELECT * FROM `" + req.body.Table +"`");
119
120 var data = $stmt.all();
121 res.status(200);
122 res.send(json({
123 Success: true,
124 ValueExists: data.length > 0,
125 Value: data
126 }));
127 } else {
128 $stmt = Database.prepare("SELECT * FROM `" + req.body.Table + "` WHERE `key`=?");
129 var data = $stmt.get(req.body.Key);
130 res.status(200);
131 res.send(json({
132 Success: true,
133 ValueExists: (!data == null),
134 Value: data
135 }));
136 }
137 } else {
138 res.status(404);
139 res.send(json({
140 Success: false,
141 Message: "Table doesn't exist"
142 }));
143 }
144 } else {
145 res.status(400);
146 res.send(json({
147 Success: false,
148 Message: "Invalid request"
149 }));
150 }
151 } else {
152 res.status(403);
153 res.send(json({
154 Success: false,
155 Message: "You are unauthorized to make requests to this host."
156 }));
157 }
158});
159
160app.all("/", function(req, res) {
161 res.send("Well, hello there Wanderer!");
162});
163
164var list = app.listen(process.env.PORT, function() {
165 console.log('Server Online, Port ' + list.address().port);
166});