From 79180080a2ba347eecdf5f1666e5956ff52be71f Mon Sep 17 00:00:00 2001 From: 9pfs <9pfs@amcforum.wiki> Date: Fri, 24 May 2024 07:16:03 +0000 Subject: [PATCH] Add prettier --- db.js | 16 +++++---- index.js | 85 ++++++++++++++++++++++++----------------------- package-lock.json | 18 ++++++++++ package.json | 5 ++- 4 files changed, 76 insertions(+), 48 deletions(-) diff --git a/db.js b/db.js index 6c540cb..9e05d68 100644 --- a/db.js +++ b/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; diff --git a/index.js b/index.js index 5d67c5c..ecbe8cb 100644 --- a/index.js +++ b/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); diff --git a/package-lock.json b/package-lock.json index 7cb36c8..f98d30a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index a4e8bf6..a21d73f 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,8 @@ "express": "^4.19.2", "postgres": "^3.4.4" }, - "type": "module" + "type": "module", + "devDependencies": { + "prettier": "3.2.5" + } }