aboutsummaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-06-04 08:51:01 +0200
committerMinteck <contact@minteck.org>2022-06-04 08:51:01 +0200
commit383285ecd5292bf9a825e05904955b937de84cc9 (patch)
tree0a53b6f02c1604b078044567c03dc1b6c944c8c2 /index.js
downloadequestriadb-383285ecd5292bf9a825e05904955b937de84cc9.tar.gz
equestriadb-383285ecd5292bf9a825e05904955b937de84cc9.tar.bz2
equestriadb-383285ecd5292bf9a825e05904955b937de84cc9.zip
Initial commit
Diffstat (limited to 'index.js')
-rw-r--r--index.js101
1 files changed, 101 insertions, 0 deletions
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..99a319f
--- /dev/null
+++ b/index.js
@@ -0,0 +1,101 @@
+const http = require('http');
+const fs = require("fs");
+const YAML = require('yaml');
+const crypto = require('crypto');
+
+global.variables = {
+ get version() {
+ const fs = require('fs');
+
+ if (fs.existsSync(".git/refs/heads/trunk")) {
+ return fs.readFileSync(".git/refs/heads/trunk").toString().substring(0, 8);
+ } else {
+ return "dev";
+ }
+ },
+ bodyOptions: {
+ limit: 10485760
+ }
+}
+
+console.log("Reading config...");
+global.databases = YAML.parse(fs.readFileSync('data/databases.yml').toString());
+
+console.log("Restoring databases...");
+global.heads = {};
+for (let entry of Object.keys(databases)) {
+ let database = databases[entry];
+ let dbid = crypto.createHash('md5').update(entry).digest('hex');
+
+ if (fs.existsSync("data/" + dbid)) {
+ console.log("[" + entry + "] Opening database...");
+ heads[entry] = JSON.parse(Buffer.from(fs.readFileSync("data/" + dbid + "/HEAD").toString(), "hex").toString("utf-8"));
+ } else {
+ console.log("[" + entry + "] Creating database...");
+ fs.mkdirSync("data/" + dbid);
+ fs.writeFileSync("data/" + dbid + "/HEAD", Buffer.from(JSON.stringify({})).toString("hex"));
+ let chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e" ,"f"];
+
+ for (let c1 of chars) {
+ for (let c2 of chars) {
+ fs.mkdirSync("data/" + dbid + "/" + c1 + c2);
+ }
+ }
+ }
+}
+
+console.log("Starting server...");
+
+let server = http.createServer(function (req, res) {
+ try {
+ parts = req.url.split("/").filter(i => i.trim() !== "");
+ console.log(req.socket.address(), parts);
+
+ if (parts.length === 0) {
+ require('./modules/_.js')(req, res);
+ } else if (!parts[0].includes("/") && parts[0].trim() !== "." && parts[0].trim() !== "..") {
+ if (fs.existsSync(__dirname + '/modules/' + parts[0].trim() + '.js')) {
+ if (typeof req.headers["x-pawerdb-token"] === "string") {
+ processed = false;
+
+ for (let entry of Object.keys(databases)) {
+ if (databases[entry].access_tokens.includes(Buffer.from(req.headers["x-pawerdb-token"], "base64").toString("utf-8").trim())) {
+ processed = true;
+ try {
+ require('./modules/' + parts[0].trim() + '.js')(req, res, entry, crypto.createHash('md5').update(entry).digest('hex'));
+ } catch (e) {
+ console.error(e);
+ res.writeHead(502, {'Content-Type':'application/json'});
+ res.write("{\"error\":502}");
+ res.end();
+ }
+ }
+ }
+
+ if (!processed) {
+ res.writeHead(401, {'Content-Type':'application/json'});
+ res.write("{\"error\":401}");
+ res.end();
+ }
+ } else {
+ res.writeHead(401, {'Content-Type':'application/json'});
+ res.write("{\"error\":401}");
+ res.end();
+ }
+ } else {
+ res.writeHead(404, {'Content-Type':'application/json'});
+ res.write("{\"error\":404}");
+ res.end();
+ }
+ }
+ } catch (e) {
+ console.error(e);
+ res.writeHead(500, {'Content-Type':'application/json'});
+ res.write("{\"error\":500}");
+ res.end();
+ }
+});
+
+server.listen(4459);
+
+console.log("pawerDB listening on port 4459") \ No newline at end of file