This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
denokv.ts
91 lines (71 loc) · 2.7 KB
/
denokv.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Hono } from "https://deno.land/x/[email protected]/mod.ts";
import { exec } from "https://deno.land/x/exec/mod.ts";
const app = new Hono();
// use default KV settings if using Deno Deploy, otherwise use a persistent sqlite database
const deployed = Deno.env.has("DENO_DEPLOYMENT_ID");
// use 7070 as default, support override as PORT envvar
const PORT = Deno.env.has("PORT") ? Deno.env.get("PORT") : "7070";
// restore if not using Deno Deploy
const restore = deployed ? null : new Deno.Command("litestream", {args: `restore -config /app/litestream.yaml`.split(" ")}).outputSync();
// use sqlite if not using Deno Deploy
const kv = deployed ? await Deno.openKv() : await Deno.openKv("/tmp/denokv.sqlite3");
// enable streaming replication if not using Deno Deploy
const backup = deployed ? null : new Deno.Command("litestream", {args: `replicate -config /app/litestream.yaml`.split(" ")}).spawn();
async function getCount() {
const all_known_keys = kv.list({prefix: []});
const output = [];
for await (const res of all_known_keys) {
output.push(res.value);
};
console.log("We have restored: " + output.length + " keys.");
return output.length
}
getCount();
const checkAuth = (c: Context) =>
c.req.headers.get('Authorization') === Deno.env.get("KVSTORE_SECRET");
const unauthorized = (c: Context) =>
c.json({ code: '401 Unauthorized', message: 'Unauthorized' }, 401)
app.use('*', async (c, next) => {
c.res.headers.set('Vary', 'Authorization')
if (!['GET', 'HEAD'].includes(c.req.method) && !checkAuth(c)) {
return unauthorized(c)
}
await next()
})
app.get("/headers", async (c) => {
return c.json(c.req.headers.entries().toArray())
});
app.get("/:key", async (c) => {
const key = c.req.param("key");
const result = await kv.get([key], { consistency: "strong" });
return c.text(result.value);
});
app.get("/count", async (c) => {
return c.text(await getCount());
});
// TODO: disable the next few methods in Prod
app.post("/eval", async (c) => {
const blurb = await c.req.text();
const result = eval(blurb);
return c.json(result);
});
async function executeAsyncEval(code) {
const func = eval('(async () => {' + code + '})');
return await func();
}
app.post("/eval_async", async (c) => {
const blurb = await c.req.text();
const result = await executeAsyncEval(blurb);
return c.json(result);
});
app.post("/exec", async (c) => {
const blurb = await c.req.text();
const result = await exec(blurb);
return c.json(result);
});
app.post("/:key", async (c) => {
const key = c.req.param("key");
const result = await kv.set([key], await c.req.text());
return c.json(result);
});
Deno.serve({ port: parseInt(PORT), hostname: "0.0.0.0"}, app.fetch);