Compare commits
5 commits
3a9d172f79
...
4157433533
Author | SHA1 | Date | |
---|---|---|---|
4157433533 | |||
9746b6b8a5 | |||
d5b5ea0485 | |||
7361c81fe9 | |||
98f00da7b0 |
1 changed files with 52 additions and 5 deletions
57
index.js
57
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;
|
||||
|
@ -104,10 +105,27 @@ async function counter_update(ns_name, key, amount) {
|
|||
var attempt = await counter_increment(ns_name, key, amount, false);
|
||||
if (attempt) return attempt;
|
||||
var nsid = await get_namespace_id(ns_name);
|
||||
var res =
|
||||
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
|
||||
if (res.length == 0) return null;
|
||||
else return parseInt(res[0].value);
|
||||
return await sql.begin(async (sql) => {
|
||||
var res =
|
||||
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
|
||||
if (res.length == 0) return null;
|
||||
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) {
|
||||
|
@ -153,6 +171,34 @@ 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 (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) {
|
||||
var e = await get_counter_value(req.params.ns, req.params.key);
|
||||
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?.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