Merge pull request 'Add prettier' (#1) from add-prettier into main
Reviewed-on: 9pfs/countify#1
This commit is contained in:
commit
9919da65d4
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" : {
|
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;
|
||||||
|
|
31
index.js
31
index.js
|
@ -1,13 +1,13 @@
|
||||||
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;
|
||||||
`;
|
`;
|
||||||
|
@ -15,19 +15,15 @@ async function get_namespace_id(name) {
|
||||||
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,
|
|
||||||
update_lowerbound,
|
|
||||||
update_upperbound} = opts;
|
|
||||||
const nsid = await get_namespace_id(opts.namespace);
|
const nsid = await get_namespace_id(opts.namespace);
|
||||||
console.log(nsid);
|
console.log(nsid);
|
||||||
await sql`
|
await sql`
|
||||||
|
@ -60,9 +56,16 @@ app.get("/create", async function(req, res) {
|
||||||
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 =
|
||||||
|
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] == "/") {
|
if (listen_path[0] == "/") {
|
||||||
await unlink(listen_path);
|
await unlink(listen_path);
|
||||||
}
|
}
|
||||||
|
|
18
package-lock.json
generated
18
package-lock.json
generated
|
@ -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",
|
||||||
|
|
|
@ -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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue