forked from fkpc/kanirobocon-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkanirobo-server.ts
34 lines (32 loc) · 994 Bytes
/
kanirobo-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { serveAPI } from "https://js.sabae.cc/wsutil.js";
import {minidb, MiniDB, NanoDB} from "./minidb.ts";
const password = (await Deno.readTextFile("password.txt")).trim();
// picoDB.deleteAll()
// picoDB.setDefault();
const fromURLSearchParams = (param) => {
const p = new URLSearchParams(param);
const res = {};
for (const key of p.keys()) {
res[key] = p.get(key);
}
return res;
};
const retJSONP = (callback, obj) => {
const s = `${callback}(${JSON.stringify(obj)});`;
return new Response(s, { headers: { "Content-Type": "application/json" } });
};
serveAPI("/api", async (param, req, path, conninfo) => {
param = fromURLSearchParams(param);
// if (param.key !== password) {
// return null;
// }
console.log(param)
const fn = "data/" + path.substring(5);
if (fn.indexOf("..") >= 0) {
return null;
}
const cmd = param.cmd;
const res = await minidb(fn, cmd, param, new MiniDB());
console.log(res)
return retJSONP(param.callback, res);
});