From 650400addd888e96a6111482e5bab3d1cd203f7b Mon Sep 17 00:00:00 2001 From: LoV432 Date: Thu, 9 Nov 2023 20:17:31 +0500 Subject: [PATCH] db: run creation code only once on start up --- Dockerfile | 7 ++++++- create-db.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ entrypoint.sh | 4 ++++ lib/db.ts | 41 ---------------------------------------- 4 files changed, 62 insertions(+), 42 deletions(-) create mode 100644 create-db.js create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index c7e4261..2075b97 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,6 +41,11 @@ RUN mkdir .next # https://nextjs.org/docs/advanced-features/output-file-tracing COPY --from=builder /app/.next/standalone ./ COPY --from=builder /app/.next/static ./.next/static +COPY ./create-db.js ./create-db.js + + +COPY ./entrypoint.sh ./entrypoint.sh +RUN chmod +x ./entrypoint.sh EXPOSE 3000 @@ -48,6 +53,6 @@ ENV PORT 3000 # set hostname to localhost ENV HOSTNAME "0.0.0.0" -CMD ["node", "server.js"] +ENTRYPOINT [ "/app/entrypoint.sh" ] # TODO: Run as non-root user \ No newline at end of file diff --git a/create-db.js b/create-db.js new file mode 100644 index 0000000..ba9e812 --- /dev/null +++ b/create-db.js @@ -0,0 +1,52 @@ +const Database = require('better-sqlite3'); +const fs = require('fs'); + +if (!fs.existsSync('./db')) { + fs.mkdirSync('./db'); +} +const db = new Database('./db/next-openwrt-stats.db'); + +// TODO: How to handle DB migrations? + +db.transaction(() => { + const createUserTable = db.prepare(`CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + index_number INTEGER, + name TEXT, + display_name TEXT, + ip TEXT, + mac_address TEXT, + last_updated INTEGER, + device_type TEXT, + last_event_type TEXT +)`); + createUserTable.run(); + + const createConnectionLogsTable = + db.prepare(`CREATE TABLE IF NOT EXISTS connectionlogs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + status TEXT, + time INTEGER +)`); + createConnectionLogsTable.run(); + + const checkForEmptyConnectionsTable = db + .prepare(`SELECT * FROM connectionlogs`) + .get(); + if (!checkForEmptyConnectionsTable) { + db.prepare( + 'INSERT INTO connectionlogs (id, status, time) VALUES (?, ?, ?)' + ).run(1, 'connected', Date.now()); + } + + const rpcdTokenTable = db.prepare(`CREATE TABLE IF NOT EXISTS rpcdtoken ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + token TEXT + )`); + rpcdTokenTable.run(); + + const checkToken = db.prepare(`SELECT * FROM rpcdtoken WHERE id = 1`).get(); + if (!checkToken) { + db.prepare('INSERT INTO rpcdtoken (token) VALUES (?)').run('0'); + } +})(); diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..bb6ed67 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +node create-db.js +node server.js diff --git a/lib/db.ts b/lib/db.ts index a205c23..f0a7387 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -5,44 +5,3 @@ if (!fs.existsSync('./db')) { fs.mkdirSync('./db'); } export const db = new Database('./db/next-openwrt-stats.db'); - -const createUserTable = db.prepare(`CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - index_number INTEGER, - name TEXT, - display_name TEXT, - ip TEXT, - mac_address TEXT, - last_updated INTEGER, - device_type TEXT, - last_event_type TEXT -)`); -createUserTable.run(); - -const createConnectionLogsTable = - db.prepare(`CREATE TABLE IF NOT EXISTS connectionlogs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - status TEXT, - time INTEGER -)`); -createConnectionLogsTable.run(); - -const checkForEmptyConnectionsTable = db - .prepare(`SELECT * FROM connectionlogs WHERE id = 1`) - .get(); -if (!checkForEmptyConnectionsTable) { - db.prepare( - 'INSERT INTO connectionlogs (id, status, time) VALUES (? ,?, ?)' - ).run(1, 'connected', Date.now()); -} - -const rpcdTokenTable = db.prepare(`CREATE TABLE IF NOT EXISTS rpcdtoken ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - token TEXT -)`); -rpcdTokenTable.run(); - -const checkToken = db.prepare(`SELECT * FROM rpcdtoken WHERE id = 1`).get(); -if (!checkToken) { - db.prepare('INSERT INTO rpcdtoken (token) VALUES (?)').run('0'); -}