Compare commits

...

5 commits

Author SHA1 Message Date
4157433533
Implement /set endpoint
All checks were successful
/ node-check (push) Successful in 2s
/ boot (push) Successful in 17s
/ prettier (push) Successful in 30s
/ tests (push) Successful in 32s
/ node-check (pull_request) Successful in 2s
/ prettier (pull_request) Successful in 17s
/ tests (pull_request) Successful in 21s
/ boot (pull_request) Successful in 16s
2024-05-26 02:49:37 +00:00
9746b6b8a5
(Unrelated) That was overcomplicated 2024-05-26 02:22:16 +00:00
d5b5ea0485
(Unrelated) Fix actually parsing enable_reset, instead of always being true if it's passed. 2024-05-26 02:06:10 +00:00
7361c81fe9
(Unrelated) Wrap in an sql.begin call. 2024-05-26 01:56:58 +00:00
98f00da7b0
Unrelated sanity change 2024-05-26 01:51:39 +00:00

View file

@ -6,6 +6,7 @@ const app = express();
const valid_regex = /^[A-Za-z0-9_\-\.]{3,64}$/; const valid_regex = /^[A-Za-z0-9_\-\.]{3,64}$/;
async function get_namespace_id(name, create = true) { async function get_namespace_id(name, create = true) {
return await sql.begin(async (sql) => { return await sql.begin(async (sql) => {
if (!valid_regex.test(name)) return null; // For sanity.
if (create) { if (create) {
var potential_res = await sql` var potential_res = await sql`
INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id; INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id;
@ -104,10 +105,27 @@ async function counter_update(ns_name, key, amount) {
var attempt = await counter_increment(ns_name, key, amount, false); var attempt = await counter_increment(ns_name, key, amount, false);
if (attempt) return attempt; if (attempt) return attempt;
var nsid = await get_namespace_id(ns_name); var nsid = await get_namespace_id(ns_name);
return await sql.begin(async (sql) => {
var res = var res =
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`; await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
if (res.length == 0) return null; if (res.length == 0) return null;
else return parseInt(res[0].value); else return parseInt(res[0].value);
});
}
async function counter_set(ns_name, key, value) {
var old_val = await counter_get(ns_name, key);
if (old_val == null) return { old_value: null, value: null, failed: null };
var nsid = await get_namespace_id(ns_name);
return await sql.begin(async (sql) => {
var changeable =
(await sql`SELECT enable_reset FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`) ==
"true";
if (!changeable)
return { old_value: old_val, value: old_val, failed: true };
await sql`UPDATE keys SET value = ${value} WHERE name = ${key} AND namespace_id = ${nsid}`;
return { old_value: old_val, value: value, failed: false };
});
} }
app.get("/hit/:ns/:key", async function (req, res) { app.get("/hit/:ns/:key", async function (req, res) {
@ -153,6 +171,34 @@ app.get("/update/:key", async function (req, res) {
.status(counter_res == null ? 404 : 200) .status(counter_res == null ? 404 : 200)
.json({ value: counter_res }); .json({ value: counter_res });
}); });
app.get("/set/:ns/:key", async function (req, res) {
var sane_query;
sane_query = req.query;
if (!sane_query?.value) sane_query.value = "0";
var { old_value, value, failed } = await counter_set(
req.params.ns,
req.params.key,
sane_query.value,
);
var status = 200;
if (old_value == null) status = 404;
if (failed) status = 403;
return res.status(status).json({ old_value: old_value, value: value });
});
app.get("/set/:key", async function (req, res) {
var sane_query;
sane_query = req.query;
if (!sane_query?.value) sane_query.value = "0";
var { old_value, value, failed } = await counter_set(
"default",
req.params.key,
sane_query.value,
);
var status = 200;
if (old_value == null) status = 404;
if (failed) status = 403;
return res.status(status).json({ old_value: old_value, value: value });
});
app.get("/get/:ns/:key", async function (req, res) { app.get("/get/:ns/:key", async function (req, res) {
var e = await get_counter_value(req.params.ns, req.params.key); var e = await get_counter_value(req.params.ns, req.params.key);
return res.status(e == null ? 404 : 200).json({ value: e }); return res.status(e == null ? 404 : 200).json({ value: e });
@ -168,7 +214,8 @@ app.get("/create", async function (req, res) {
if (!sane_query?.namespace) sane_query.namespace = "default"; if (!sane_query?.namespace) sane_query.namespace = "default";
if (!sane_query?.key) sane_query.key = randomUUID(); if (!sane_query?.key) sane_query.key = randomUUID();
if (!sane_query?.enable_reset) sane_query.enable_reset = false; if (!sane_query?.enable_reset) sane_query.enable_reset = false;
else sane_query.enable_reset = !!sane_query.enable_reset; else
sane_query.enable_reset = sane_query.enable_reset.toLowerCase() == "true";
if (!sane_query?.update_lowerbound) sane_query.update_lowerbound = "-1"; if (!sane_query?.update_lowerbound) sane_query.update_lowerbound = "-1";
if (!sane_query?.update_upperbound) sane_query.update_upperbound = "1"; if (!sane_query?.update_upperbound) sane_query.update_upperbound = "1";
if (!sane_query?.value) sane_query.value = "0"; if (!sane_query?.value) sane_query.value = "0";