From 6d36cb75eca945b92f0e3f217ebaa9511c659871 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Mon, 19 May 2025 02:05:54 -0400 Subject: [PATCH] basic stuff --- .gitignore | 3 +++ db.js | 25 +++++++++++++++++++++++++ index.js | 41 +++++++++++++++++++++++++++++++++++++++++ package.json | 9 +++++++++ 4 files changed, 78 insertions(+) create mode 100644 .gitignore create mode 100644 db.js create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9abc8b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/package-lock.json +/node_modules +/main.db \ No newline at end of file diff --git a/db.js b/db.js new file mode 100644 index 0000000..118b47d --- /dev/null +++ b/db.js @@ -0,0 +1,25 @@ +import sqlite3 from 'sqlite3' +import { open } from 'sqlite' + +let db; + +const sql = `CREATE TABLE IF NOT EXISTS action (addr TEXT, action TEXT)` + +async function initDb() { + if (db) return db; + + db = await open({ + filename: `${process.cwd()}/main.db`, + driver: sqlite3.Database + }); + + let sqlCmds = sql.split('\n'); + + for (let cmd of sqlCmds) { + await db.run(cmd); + } + + return db; +} + +export { initDb }; \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..7bff0fe --- /dev/null +++ b/index.js @@ -0,0 +1,41 @@ +import express from 'express'; +import { initDb } from './db.js'; +import bodyParser from 'body-parser'; + +const app = express() +const port = process.env.port || 5000 + +const jsonParser = bodyParser.json() + +app.use(jsonParser); + +app.post('/', async (req, res) => { + var db = await initDb(); + var ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress; + var { action } = req.body; + action = action + ''; + + await db.run('DELETE FROM action WHERE addr = ? AND action = ?', [ + ip, + action + ]); + await db.run('INSERT INTO action (addr, action) VALUES (?, ?)', [ + ip, + action + ]); + let output = await db.all('SELECT * FROM action WHERE action = ?', [ + action + ]) + + console.log(`${output.length} people triggered ${action}.`); + + res.send({ + ip, + action, + count: output.length + }); +}) + +app.listen(port, () => { + console.log(`Example app listening on port ${port}`) +}) diff --git a/package.json b/package.json new file mode 100644 index 0000000..286244f --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "dependencies": { + "body-parser": "^2.2.0", + "express": "^5.1.0", + "sqlite": "^5.1.1", + "sqlite3": "^5.1.7" + }, + "type": "module" +}