FemScoreboard/server.ts

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-10-07 22:46:02 -07:00
/**
* server.ts
* Web server for the scoreboard page.
*/
import { Database as Database3 } from 'sqlite3';
import { Database, open } from 'sqlite';
import express = require('express');
import 'dotenv/config';
import { ScoreboardMessageRow, ScoreboardUserRow } from './models';
const app = express();
app.use(express.static('public'));
app.set('view engine', 'pug');
const port = process.env.PORT;
let db: Database = null;
async function openDb() {
return open({
filename: 'discord/db.sqlite',
driver: Database3
})
}
app.get('/', async (req, res) => {
const users = await db.all<[ScoreboardUserRow]>('SELECT * FROM users');
//const msgs = await db.all<[ScoreboardMessageRow]>('SELECT * FROM messages');
const funniest = [...users].sort((a, b) => a.reaction_1_total - b.reaction_1_total);
const realest = [...users].sort((a, b) => a.reaction_2_total - b.reaction_2_total);
const cunniest = [...users].sort((a, b) => a.reaction_3_total - b.reaction_3_total);
res.render('index', { funniest, realest, cunniest });
});
app.listen(port, async () => {
console.log('[web] Opening database...');
db = await openDb();
console.log('[web] Database ready.');
console.log(`[web] Listening on port ${port}`);
});