Merge pull request 'Add prettier' (#1) from add-prettier into main

Reviewed-on: 9pfs/countify#1
This commit is contained in:
9pfs 2024-05-24 14:45:54 +00:00
commit 9919da65d4
4 changed files with 76 additions and 48 deletions

16
db.js
View file

@ -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" : { const sql = postgres(
"host": "/var/run/postgresql", process.env.DB_CONNECTION_STRING || process.env.NODE_ENV == "production"
"database": "h_countify-dev", ? "postgres://h_countify-prod?host=%2Fvar%2Flib%2Fpostgresql"
"debug": true : {
}); host: "/var/run/postgresql",
database: "h_countify-dev",
debug: true,
},
);
export default sql; export default sql;

View file

@ -1,36 +1,32 @@
import sql from './db.js'; import sql from "./db.js";
import express from 'express'; import express from "express";
import {unlink} from 'fs/promises'; import { unlink } from "fs/promises";
import {randomUUID} from 'crypto'; import { randomUUID } from "crypto";
const app = express(); const app = express();
function notImplementedHandler(req, res, next) { function notImplementedHandler(req, res, next) {
res.sendStatus(418); res.sendStatus(418);
} }
async function get_namespace_id(name) { async function get_namespace_id(name) {
return await sql.begin(async sql=>{ return await sql.begin(async (sql) => {
var potential_res=await sql` var potential_res = await sql`
INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id; INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id;
`; `;
if(potential_res.length==0) { if (potential_res.length == 0) {
var e=await sql`SELECT id FROM namespaces WHERE name = ${name} LIMIT 1`; var e = await sql`SELECT id FROM namespaces WHERE name = ${name} LIMIT 1`;
console.log("e", e, e[0], e[0].id); console.log("e", e, e[0], e[0].id);
return e[0].id; return e[0].id;
} } else {
else { console.log("p", potential_res);
console.log("p", potential_res); return potential_res[0].id;
return potential_res[0].id; }
} });
});
} }
async function add_counter(opts) { async function add_counter(opts) {
const { key, const { key, value, enable_reset, update_lowerbound, update_upperbound } =
value, opts;
enable_reset, const nsid = await get_namespace_id(opts.namespace);
update_lowerbound, console.log(nsid);
update_upperbound} = opts; await sql`
const nsid = await get_namespace_id(opts.namespace);
console.log(nsid);
await sql`
INSERT INTO keys( INSERT INTO keys(
name, name,
value, value,
@ -47,23 +43,30 @@ async function add_counter(opts) {
${parseInt(update_upperbound)} ${parseInt(update_upperbound)}
) )
`; `;
return; return;
} }
app.get("/create", async function(req, res) { app.get("/create", async function (req, res) {
var sane_query; var sane_query;
sane_query = req.query; sane_query = req.query;
if(!sane_query?.namespace) sane_query.namespace = "default"; if (!sane_query?.namespace) sane_query.namespace = "default";
if(!sane_query?.key) sane_query.key = randomUUID(); if (!sane_query?.key) sane_query.key = randomUUID();
if(!sane_query?.enable_reset) sane_query.enable_reset = false; 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;
if(!sane_query?.update_lowerbound) sane_query.update_lowerbound = "-1"; if (!sane_query?.update_lowerbound) sane_query.update_lowerbound = "-1";
if(!sane_query?.update_upperbound) sane_query.update_upperbound = "1"; if (!sane_query?.update_upperbound) sane_query.update_upperbound = "1";
if(!sane_query?.value) sane_query.value = "0"; if (!sane_query?.value) sane_query.value = "0";
const add_counter_res = await add_counter(req.query); const add_counter_res = await add_counter(req.query);
return res.json({namespace: sane_query.namespace, key: sane_query.key, value: sane_query.value}); 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"; const listen_path =
if(listen_path[0]=="/") { process.env?.LISTEN_PATH || process.env?.NODE_ENV == "production"
await unlink(listen_path); ? "/run/user/2123/countify/webserver.sock"
: "/run/user/2123/countify-dev.sock";
if (listen_path[0] == "/") {
await unlink(listen_path);
} }
app.listen(listen_path); app.listen(listen_path);

18
package-lock.json generated
View file

@ -11,6 +11,9 @@
"dependencies": { "dependencies": {
"express": "^4.19.2", "express": "^4.19.2",
"postgres": "^3.4.4" "postgres": "^3.4.4"
},
"devDependencies": {
"prettier": "3.2.5"
} }
}, },
"node_modules/accepts": { "node_modules/accepts": {
@ -499,6 +502,21 @@
"url": "https://github.com/sponsors/porsager" "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": { "node_modules/proxy-addr": {
"version": "2.0.7", "version": "2.0.7",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",

View file

@ -16,5 +16,8 @@
"express": "^4.19.2", "express": "^4.19.2",
"postgres": "^3.4.4" "postgres": "^3.4.4"
}, },
"type": "module" "type": "module",
"devDependencies": {
"prettier": "3.2.5"
}
} }