Compare commits

..

10 commits

Author SHA1 Message Date
64affde372 Merge pull request 'implement /hit API endpoint' (#18) from hit-endpoint into main
All checks were successful
Run node --check on everything / node-check (push) Successful in 6s
Check project using prettier / prettier (push) Successful in 26s
Make sure the server is actually able to start / boot-test (push) Successful in 39s
Reviewed-on: 9pfs/countify#18
2024-05-25 04:53:33 +00:00
7151c77d15
Handle requests with no namespace correctly
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 7s
Check project using prettier / prettier (pull_request) Successful in 20s
Make sure the server is actually able to start / boot-test (pull_request) Successful in 34s
2024-05-25 04:45:13 +00:00
6dfb8ce705
Merge branch 'main' into hit-endpoint
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 8s
Check project using prettier / prettier (pull_request) Successful in 21s
Make sure the server is actually able to start / boot-test (pull_request) Successful in 27s
2024-05-25 04:16:16 +00:00
aaed9e854d
Run prettier
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 6s
Make sure the server is actually able to start / prettier (pull_request) Successful in 26s
Check project using prettier / prettier (pull_request) Successful in 31s
2024-05-25 01:55:14 +00:00
6de8f5bcdb
Hit is supposed to make keys, following the countapi spec.
Some checks failed
Run node --check on everything / node-check (pull_request) Successful in 8s
Make sure the server is actually able to start / prettier (pull_request) Successful in 22s
Check project using prettier / prettier (pull_request) Failing after 21s
2024-05-25 01:53:16 +00:00
2c42cf9dc8
Merge branch 'main' into hit-endpoint
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 7s
Make sure the server is actually able to start / prettier (pull_request) Successful in 24s
Check project using prettier / prettier (pull_request) Successful in 32s
2024-05-25 01:20:10 +00:00
146e05751d
Merge branch 'main' into hit-endpoint
All checks were successful
Run node --check on everything / node-check (push) Successful in 6s
Run node --check on everything / node-check (pull_request) Successful in 9s
Make sure the server is actually able to start / prettier (push) Successful in 24s
Make sure the server is actually able to start / prettier (pull_request) Successful in 27s
Check project using prettier / prettier (pull_request) Successful in 24s
Check project using prettier / prettier (push) Successful in 31s
2024-05-25 01:07:05 +00:00
1906580fb4
run prettier
All checks were successful
Run node --check on everything / node-check (push) Successful in 6s
Run node --check on everything / node-check (pull_request) Successful in 8s
Make sure the server is actually able to start / prettier (push) Successful in 30s
Make sure the server is actually able to start / prettier (pull_request) Successful in 27s
Check project using prettier / prettier (pull_request) Successful in 32s
Check project using prettier / prettier (push) Successful in 38s
2024-05-25 00:58:23 +00:00
37177e391d
Fixes to the /hit endpoint
Some checks failed
Run node --check on everything / node-check (push) Successful in 9s
Run node --check on everything / node-check (pull_request) Successful in 10s
Check project using prettier / prettier (push) Failing after 25s
Check project using prettier / prettier (pull_request) Failing after 28s
Make sure the server is actually able to start / prettier (pull_request) Successful in 32s
Make sure the server is actually able to start / prettier (push) Successful in 42s
2024-05-25 00:54:52 +00:00
361ab5e8f4
Partially implement /hit
All checks were successful
Run node --check on everything / node-check (push) Successful in 8s
Make sure the server is actually able to start / prettier (push) Successful in 26s
Check project using prettier / prettier (push) Successful in 34s
Run node --check on everything / node-check (pull_request) Successful in 6s
Make sure the server is actually able to start / prettier (pull_request) Successful in 22s
Check project using prettier / prettier (pull_request) Successful in 21s
2024-05-25 00:41:38 +00:00

View file

@ -68,6 +68,55 @@ async function get_counter_value(ns_name, key) {
}
}
async function counter_increment(ns_name, key, offset, create = false) {
var nsid = await get_namespace_id(ns_name);
return await sql.begin(async (sql) => {
if (offset == 0) return true;
if (create) {
var existence_res =
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
if (existence_res.length == 0) {
if (
!(await add_counter({
namespace: ns_name,
key: key,
value: 1,
enable_reset: false,
update_lowerbound: 0,
update_upperbound: 1,
}))
)
return null;
else return 1; // We know the key value will be 1 here, just return it explicitly (Ending early to save some SQL calls)
}
}
var sql_res = await sql`UPDATE keys SET value = value + ${offset} WHERE
namespace_id = ${nsid} AND
name = ${key} AND
${offset < 0 ? sql`update_lowerbound <= ${offset}` : sql`update_upperbound >= ${offset}`}
RETURNING value`;
if (sql_res.length == 1) return parseInt(sql_res[0].value);
else return null;
});
}
app.get("/hit/:ns/:key", async function (req, res) {
var counter_res = await counter_increment(
req.params.ns,
req.params.key,
1,
true,
);
return res
.status(counter_res == null ? 403 : 200)
.json({ value: counter_res });
});
app.get("/hit/:key", async function (req, res) {
var counter_res = await counter_increment("default", req.params.key, 1, true);
return res
.status(counter_res == null ? 403 : 200)
.json({ value: counter_res });
});
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 });