Compare commits

...

13 commits

Author SHA1 Message Date
4ace687121
Change failed to be true regardless of when it fails, asjust logic accordingly
All checks were successful
/ node-check (pull_request) Successful in 3s
/ prettier (pull_request) Successful in 14s
/ tests (pull_request) Successful in 19s
/ boot (pull_request) Successful in 14s
/ node-check (push) Successful in 3s
/ boot (push) Successful in 14s
/ prettier (push) Successful in 20s
/ tests (push) Successful in 24s
2024-05-26 15:41:45 +00:00
9f62ef78d3
Slight optimization
All checks were successful
/ prettier (pull_request) Successful in 4s
/ node-check (pull_request) Successful in 3s
/ tests (pull_request) Successful in 7s
/ boot (pull_request) Successful in 4s
/ prettier (push) Successful in 4s
/ tests (push) Successful in 9s
/ node-check (push) Successful in 3s
/ boot (push) Successful in 4s
2024-05-26 15:26:59 +00:00
d1aa74a14e
Make get_counter_value optionally return the nsid.
All checks were successful
/ node-check (pull_request) Successful in 3s
/ prettier (pull_request) Successful in 16s
/ tests (pull_request) Successful in 21s
/ boot (pull_request) Successful in 16s
/ node-check (push) Successful in 2s
/ prettier (push) Successful in 22s
/ boot (push) Successful in 21s
/ tests (push) Successful in 31s
2024-05-26 15:22:07 +00:00
fda599c32a
Un-wrap this sql call
All checks were successful
/ node-check (pull_request) Successful in 3s
/ tests (pull_request) Successful in 19s
/ boot (pull_request) Successful in 13s
/ prettier (pull_request) Successful in 20s
/ node-check (push) Successful in 3s
/ tests (push) Successful in 18s
/ prettier (push) Successful in 20s
/ boot (push) Successful in 30s
2024-05-26 15:07:01 +00:00
51c684a416
Revert "Un-wrap this sql call"
This reverts commit 8314e7fe6c.
2024-05-26 15:06:07 +00:00
8314e7fe6c
Un-wrap this sql call
All checks were successful
/ boot (pull_request) Successful in 15s
/ prettier (pull_request) Successful in 9s
/ tests (pull_request) Successful in 21s
/ node-check (pull_request) Successful in 3s
/ boot (push) Successful in 20s
/ tests (push) Successful in 27s
/ node-check (push) Successful in 2s
/ prettier (push) Successful in 28s
2024-05-26 15:04:23 +00:00
eef5b28445
Run prettier
All checks were successful
/ prettier (pull_request) Successful in 15s
/ tests (pull_request) Successful in 19s
/ node-check (pull_request) Successful in 3s
/ boot (pull_request) Successful in 15s
/ node-check (push) Successful in 2s
/ prettier (push) Successful in 21s
/ boot (push) Successful in 26s
/ tests (push) Successful in 31s
2024-05-26 14:58:49 +00:00
67a3fc87bf
Small Fixes
Some checks failed
/ node-check (pull_request) Successful in 3s
/ boot (pull_request) Successful in 14s
/ tests (pull_request) Successful in 26s
/ prettier (pull_request) Failing after 25s
/ node-check (push) Successful in 3s
/ tests (push) Successful in 24s
/ boot (push) Successful in 18s
/ prettier (push) Failing after 31s
2024-05-26 14:53:01 +00:00
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}$/;
async function get_namespace_id(name, create = true) {
return await sql.begin(async (sql) => {
if (!valid_regex.test(name)) return null; // For sanity.
if (create) {
var potential_res = await sql`
INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id;
@ -55,16 +56,23 @@ async function add_counter(opts) {
}
}
async function get_counter_value(ns_name, key) {
async function get_counter_value(ns_name, key, return_id = false) {
const nsid = await get_namespace_id(ns_name, false);
if (nsid == null) return null;
else {
if (nsid == null) {
if (!return_id) return null;
return { value: null, nsid: null };
} else {
var sql_res = await sql`
SELECT value FROM keys WHERE
namespace_id = ${nsid} AND
name = ${key}`;
if (sql_res.length == 0) return null;
else return parseInt(sql_res[0].value);
if (sql_res.length == 0) {
if (!return_id) return null;
return { value: null, nsid: null };
} else {
if (!return_id) return parseInt(sql_res[0].value);
return { value: parseInt(sql_res[0].value), nsid: nsid };
}
}
}
@ -110,6 +118,20 @@ async function counter_update(ns_name, key, amount) {
else return parseInt(res[0].value);
}
async function counter_set(ns_name, key, value) {
var { old_val: value, nsid } = await get_counter_value(ns_name, key, true);
if (old_val == null) return { old_value: null, value: null, failed: true };
return await sql.begin(async (sql) => {
var changeable = (
await sql`SELECT enable_reset FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`
)[0].enable_reset;
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) {
var counter_res = await counter_increment(
req.params.ns,
@ -153,6 +175,38 @@ app.get("/update/:key", async function (req, res) {
.status(counter_res == null ? 404 : 200)
.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 (failed) {
if (old_value == null) status = 404;
else 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 (failed) {
if (old_value == null) status = 404;
else status = 403;
}
return res.status(status).json({ old_value: old_value, value: value });
});
app.get("/get/:ns/:key", async function (req, res) {
var e = await get_counter_value(req.params.ns, req.params.key);
return res.status(e == null ? 404 : 200).json({ value: e });
@ -168,7 +222,8 @@ app.get("/create", async function (req, res) {
if (!sane_query?.namespace) sane_query.namespace = "default";
if (!sane_query?.key) sane_query.key = randomUUID();
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_upperbound) sane_query.update_upperbound = "1";
if (!sane_query?.value) sane_query.value = "0";