Compare commits
2 commits
ece2c84a15
...
9919da65d4
Author | SHA1 | Date | |
---|---|---|---|
9919da65d4 | |||
79180080a2 |
4 changed files with 76 additions and 48 deletions
16
db.js
16
db.js
|
@ -1,8 +1,12 @@
|
|||
import postgres from 'postgres';
|
||||
import postgres from "postgres";
|
||||
|
||||
const sql = postgres(process.env.DB_CONNECTION_STRING||process.env.NODE_ENV=="production" ? "postgres://h_countify-prod?host=%2Fvar%2Flib%2Fpostgresql" : {
|
||||
"host": "/var/run/postgresql",
|
||||
"database": "h_countify-dev",
|
||||
"debug": true
|
||||
});
|
||||
const sql = postgres(
|
||||
process.env.DB_CONNECTION_STRING || process.env.NODE_ENV == "production"
|
||||
? "postgres://h_countify-prod?host=%2Fvar%2Flib%2Fpostgresql"
|
||||
: {
|
||||
host: "/var/run/postgresql",
|
||||
database: "h_countify-dev",
|
||||
debug: true,
|
||||
},
|
||||
);
|
||||
export default sql;
|
||||
|
|
85
index.js
85
index.js
|
@ -1,36 +1,32 @@
|
|||
import sql from './db.js';
|
||||
import express from 'express';
|
||||
import {unlink} from 'fs/promises';
|
||||
import {randomUUID} from 'crypto';
|
||||
import sql from "./db.js";
|
||||
import express from "express";
|
||||
import { unlink } from "fs/promises";
|
||||
import { randomUUID } from "crypto";
|
||||
const app = express();
|
||||
function notImplementedHandler(req, res, next) {
|
||||
res.sendStatus(418);
|
||||
res.sendStatus(418);
|
||||
}
|
||||
async function get_namespace_id(name) {
|
||||
return await sql.begin(async sql=>{
|
||||
var potential_res=await sql`
|
||||
return await sql.begin(async (sql) => {
|
||||
var potential_res = await sql`
|
||||
INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id;
|
||||
`;
|
||||
if(potential_res.length==0) {
|
||||
var e=await sql`SELECT id FROM namespaces WHERE name = ${name} LIMIT 1`;
|
||||
console.log("e", e, e[0], e[0].id);
|
||||
return e[0].id;
|
||||
}
|
||||
else {
|
||||
console.log("p", potential_res);
|
||||
return potential_res[0].id;
|
||||
}
|
||||
});
|
||||
if (potential_res.length == 0) {
|
||||
var e = await sql`SELECT id FROM namespaces WHERE name = ${name} LIMIT 1`;
|
||||
console.log("e", e, e[0], e[0].id);
|
||||
return e[0].id;
|
||||
} else {
|
||||
console.log("p", potential_res);
|
||||
return potential_res[0].id;
|
||||
}
|
||||
});
|
||||
}
|
||||
async function add_counter(opts) {
|
||||
const { key,
|
||||
value,
|
||||
enable_reset,
|
||||
update_lowerbound,
|
||||
update_upperbound} = opts;
|
||||
const nsid = await get_namespace_id(opts.namespace);
|
||||
console.log(nsid);
|
||||
await sql`
|
||||
const { key, value, enable_reset, update_lowerbound, update_upperbound } =
|
||||
opts;
|
||||
const nsid = await get_namespace_id(opts.namespace);
|
||||
console.log(nsid);
|
||||
await sql`
|
||||
INSERT INTO keys(
|
||||
name,
|
||||
value,
|
||||
|
@ -47,23 +43,30 @@ async function add_counter(opts) {
|
|||
${parseInt(update_upperbound)}
|
||||
)
|
||||
`;
|
||||
return;
|
||||
return;
|
||||
}
|
||||
app.get("/create", async function(req, res) {
|
||||
var sane_query;
|
||||
sane_query = req.query;
|
||||
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;
|
||||
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";
|
||||
const add_counter_res = await add_counter(req.query);
|
||||
return res.json({namespace: sane_query.namespace, key: sane_query.key, value: sane_query.value});
|
||||
app.get("/create", async function (req, res) {
|
||||
var sane_query;
|
||||
sane_query = req.query;
|
||||
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;
|
||||
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";
|
||||
const add_counter_res = await add_counter(req.query);
|
||||
return res.json({
|
||||
namespace: sane_query.namespace,
|
||||
key: sane_query.key,
|
||||
value: sane_query.value,
|
||||
});
|
||||
});
|
||||
const listen_path = process.env?.LISTEN_PATH||process.env?.NODE_ENV=="production"?"/run/user/2123/countify/webserver.sock":"/run/user/2123/countify-dev.sock";
|
||||
if(listen_path[0]=="/") {
|
||||
await unlink(listen_path);
|
||||
const listen_path =
|
||||
process.env?.LISTEN_PATH || process.env?.NODE_ENV == "production"
|
||||
? "/run/user/2123/countify/webserver.sock"
|
||||
: "/run/user/2123/countify-dev.sock";
|
||||
if (listen_path[0] == "/") {
|
||||
await unlink(listen_path);
|
||||
}
|
||||
app.listen(listen_path);
|
||||
|
|
18
package-lock.json
generated
18
package-lock.json
generated
|
@ -11,6 +11,9 @@
|
|||
"dependencies": {
|
||||
"express": "^4.19.2",
|
||||
"postgres": "^3.4.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "3.2.5"
|
||||
}
|
||||
},
|
||||
"node_modules/accepts": {
|
||||
|
@ -499,6 +502,21 @@
|
|||
"url": "https://github.com/sponsors/porsager"
|
||||
}
|
||||
},
|
||||
"node_modules/prettier": {
|
||||
"version": "3.2.5",
|
||||
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz",
|
||||
"integrity": "sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"prettier": "bin/prettier.cjs"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/prettier/prettier?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-addr": {
|
||||
"version": "2.0.7",
|
||||
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
||||
|
|
|
@ -16,5 +16,8 @@
|
|||
"express": "^4.19.2",
|
||||
"postgres": "^3.4.4"
|
||||
},
|
||||
"type": "module"
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"prettier": "3.2.5"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue