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" : {
"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;

View file

@ -1,33 +1,29 @@
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);
}
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`;
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 {
} 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 { key, value, enable_reset, update_lowerbound, update_upperbound } =
opts;
const nsid = await get_namespace_id(opts.namespace);
console.log(nsid);
await sql`
@ -49,21 +45,28 @@ async function add_counter(opts) {
`;
return;
}
app.get("/create", async function(req, res) {
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;
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";
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});
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]=="/") {
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
View file

@ -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",

View file

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