Compare commits
13 commits
3a9d172f79
...
4ace687121
Author | SHA1 | Date | |
---|---|---|---|
4ace687121 | |||
9f62ef78d3 | |||
d1aa74a14e | |||
fda599c32a | |||
51c684a416 | |||
8314e7fe6c | |||
eef5b28445 | |||
67a3fc87bf | |||
4157433533 | |||
9746b6b8a5 | |||
d5b5ea0485 | |||
7361c81fe9 | |||
98f00da7b0 |
1 changed files with 61 additions and 6 deletions
67
index.js
67
index.js
|
@ -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";
|
||||
|
|
Loading…
Reference in a new issue