basic stuff

This commit is contained in:
biglyderv 2025-05-19 02:05:54 -04:00
commit 6d36cb75ec
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
4 changed files with 78 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/package-lock.json
/node_modules
/main.db

25
db.js Normal file
View file

@ -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 };

41
index.js Normal file
View file

@ -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}`)
})

9
package.json Normal file
View file

@ -0,0 +1,9 @@
{
"dependencies": {
"body-parser": "^2.2.0",
"express": "^5.1.0",
"sqlite": "^5.1.1",
"sqlite3": "^5.1.7"
},
"type": "module"
}