const express = require('express'); const {nanoid} = require('nanoid'); const { Pool } = require('pg'); const db = new Pool({ host: 'postgres', database: 'pastebin', user: 'pastebin', password: 'buginoo' }); // initialize app: const app = express(); const port = 80; db.query("CREATE TABLE IF NOT EXISTS bin (id VARCHAR PRIMARY KEY);" + "CREATE TABLE IF NOT EXISTS note (id VARCHAR PRIMARY KEY, text TEXT, modified TIMESTAMPTZ);" + "CREATE TABLE IF NOT EXISTS bin_note (bin_id VARCHAR REFERENCES bin (id), note_id VARCHAR REFERENCES note (id), PRIMARY KEY (bin_id, note_id));" + "CREATE TABLE IF NOT EXISTS user_ (id VARCHAR PRIMARY KEY, username TEXT, password TEXT);" // table 'user' is already taken ); const router = express.Router(); router.get('/', (req, res)=>{ res.send('Feh') }); const load_notes_stmt = "SELECT n.id, n.text, n.modified FROM bin_note AS bn" +" JOIN note AS n" +" ON n.id = bn.note_id" +" WHERE bn.bin_id = $1"; // {bin_id} router.post('/load-notes', (req, res)=>{ const bin_id = req.body.bin_id; db.query(load_notes_stmt, [bin_id]) .then(result => { res.json({status: 'ok', bin:{id:bin_id}, notes:result.rows}) }); // {status: 'ok', bin:{id:bin.id}, notes: bin.notes} }); const search_stmt = "SELECT n.id, n.text, n.modified FROM note AS n" +" WHERE n.text LIKE '%' || $1 || '%'" // need to use string concat otherwise the `$1` is viewed as part of the string instead of a placeholder +" ORDER BY n.modified DESC"; // {search_term, sorting, bin_id} router.post('/search', (req, res)=>{ const search_term = req.body.search_term; db.query(search_stmt, [search_term]) .then(result => { res.json({status:'ok', notes:result.rows}); }); // {status: 'ok', notes} }); const upsert_note_stmt = "INSERT INTO note (id, text, modified) VALUES ($1, $2, NOW())" +" ON CONFLICT (id)" +" DO UPDATE SET text = EXCLUDED.text, modified = EXCLUDED.modified"; const upsert_bin_stmt = "INSERT INTO bin (id) VALUES ($1)" +" ON CONFLICT (id)" +" DO NOTHING"; const upsert_bin_note_stmt = "INSERT INTO bin_note (bin_id, note_id) VALUES ($1, $2)" +" ON CONFLICT (bin_id, note_id)" +" DO NOTHING"; // {bin_id, note_id, text} router.post('/save', (req, res)=>{ const {bin_id, note_id, text} = req.body; db.connect().then(async (client)=>{ await client.query(upsert_note_stmt, [note_id, text]); await client.query(upsert_bin_stmt, [bin_id]); await client.query(upsert_bin_note_stmt, [bin_id, note_id]); // don't forget to release back into the Pool: client.release(); res.json({status: 'ok'}); }); // {status: 'ok'} }); const login_check_stmt = "SELECT id, password FROM user_ AS u" +" WHERE u.username = $1"; const register_stmt = "INSERT INTO user_ (id, username, password) VALUES ($1, $2, $3)"; router.post('/login', (req, res)=>{ const {username, password_from_client} = req.body; db.query(login_check_stmt, [username]) .then(result=>{ // if there is such a username: if(result.rows.length > 0){ const row = result.rows[0]; const id=row.id, password_from_db=row.password; // if the passwords match: if(password_from_client === password_from_db){ res.json({success:true, user:{id, username}, session_id:nanoid()}); } else{ res.json({success: false}); } } // if no such user exists: else{ const id = nanoid(); db.query(register_stmt, [id, username, password_from_client]) .then(r=>{ res.json({success: true, user:{id, username}, session_id:nanoid()}); }); } }); }); app.use('/', express.json(), router); // run app: const server = app.listen(port, () => { console.log(`Pastebin app listening at http://localhost:${port}`) }); // Ruthlessly copied from [https://medium.com/@becintec/building-graceful-node-applications-in-docker-4d2cd4d5d392]: // The signals we want to handle // NOTE: although it is tempting, the SIGKILL signal (9) cannot be intercepted and handled var signals = { 'SIGHUP': 1, 'SIGINT': 2, 'SIGTERM': 15 }; // Do any necessary shutdown logic for our application here const shutdown = (signal, value) => { console.log("shutdown!"); server.close(() => { console.log(`server stopped by ${signal} with value ${value}`); process.exit(128 + value); }); }; // Create a listener for each of the signals that we want to handle Object.keys(signals).forEach((signal) => { process.on(signal, () => { console.log(`process received a ${signal} signal`); shutdown(signal, signals[signal]); }); });