From 0a2c06f55b23f83cd9961959d773148b2d7a99f0 Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 8 Mar 2024 17:05:27 +0100 Subject: [PATCH 01/12] added config file and refactored code --- comprl-web/app/db/sqlite.data.tsx | 23 ++++++++++++----------- comprl-web/app/routes/_auth.register.tsx | 3 ++- comprl-web/config.ts | 5 +++++ 3 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 comprl-web/config.ts diff --git a/comprl-web/app/db/sqlite.data.tsx b/comprl-web/app/db/sqlite.data.tsx index e687159d..35f93041 100644 --- a/comprl-web/app/db/sqlite.data.tsx +++ b/comprl-web/app/db/sqlite.data.tsx @@ -1,10 +1,11 @@ import Database from 'better-sqlite3'; import User from './types'; +import { config } from "config"; -console.log('Creating users.db'); -const db = new Database('users.db', { verbose: console.log }); +console.log(`Creating ${config.user_db_path}`); +const db = new Database(config.user_db_path, { verbose: console.log }); db.prepare(` - CREATE TABLE IF NOT EXISTS users( + CREATE TABLE IF NOT EXISTS ${config.user_db_name}( user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, password TEXT NOT NULL, @@ -16,18 +17,18 @@ db.prepare(` db.close(); export async function addUser(username: string, password: string, role: string = 'user') { - const db = new Database('users.db', { verbose: console.log }); - const stmt = db.prepare('INSERT INTO users(username, password, role) VALUES (?, ?, ?)'); + const db = new Database(config.user_db_path, { verbose: console.log }); + const stmt = db.prepare(`INSERT INTO ${config.user_db_name}(username, password, role) VALUES (?, ?, ?)`); stmt.run(username, password, role); db.close(); } -export async function getUser(username: string, password : string) { - const db = new Database('users.db', { verbose: console.log }); - const stmt = db.prepare('SELECT * FROM users WHERE username = ?'); +export async function getUser(username: string, password: string) { + const db = new Database(config.user_db_path, { verbose: console.log }); + const stmt = db.prepare(`SELECT * FROM ${config.user_db_name} WHERE username = ?`); const res = stmt.get(username); db.close(); - if(!res) {return undefined} - if(res.password != password) {return undefined} - return {id: res.user_id, name: res.username, role: res.role} as User; + if (!res) { return undefined } + if (res.password != password) { return undefined } + return { id: res.user_id, name: res.username, role: res.role } as User; } \ No newline at end of file diff --git a/comprl-web/app/routes/_auth.register.tsx b/comprl-web/app/routes/_auth.register.tsx index 7e2c946c..425df80b 100644 --- a/comprl-web/app/routes/_auth.register.tsx +++ b/comprl-web/app/routes/_auth.register.tsx @@ -3,6 +3,7 @@ import { ActionFunctionArgs, redirect } from "@remix-run/node"; import { Form, useActionData } from "@remix-run/react"; import { useState } from "react"; import { addUser } from "~/db/sqlite.data"; +import { config } from "config"; export async function action({ request }: ActionFunctionArgs) { @@ -11,7 +12,7 @@ export async function action({ request }: ActionFunctionArgs) { const username = formData.get("username") as string; const password = formData.get("password") as string; - if (key !== "1234") { + if (key !== config.key) { return { alerts: [{ severity: "error", message: "Invalid key" }] } diff --git a/comprl-web/config.ts b/comprl-web/config.ts new file mode 100644 index 00000000..043fb107 --- /dev/null +++ b/comprl-web/config.ts @@ -0,0 +1,5 @@ +export const config = { + user_db_path: "user.db", + user_db_name: "data", + key: "1234" +}; From 81303cc94399f14503f41387d3b228adf17554b6 Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 8 Mar 2024 17:20:10 +0100 Subject: [PATCH 02/12] changed README --- comprl-web/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/comprl-web/README.md b/comprl-web/README.md index 911fc5a4..5a452b02 100644 --- a/comprl-web/README.md +++ b/comprl-web/README.md @@ -28,6 +28,6 @@ npm start Now you'll need to pick a host to deploy it to. -## Key -The current key is `1234`. -You can change the key in the file `comprl-web/app/routes/_dashboard.register.tsx`. \ No newline at end of file +## Config File +The `user_db_path`, the `user_db_name` and the `key` are defined in the config file `config.ts`. +The current key is `1234`. \ No newline at end of file From b4dd80fdaac2af43e93b8f359a7c2bf383981b4e Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 15 Mar 2024 10:06:15 +0100 Subject: [PATCH 03/12] added toml file --- comprl-web/config.toml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 comprl-web/config.toml diff --git a/comprl-web/config.toml b/comprl-web/config.toml new file mode 100644 index 00000000..badc566e --- /dev/null +++ b/comprl-web/config.toml @@ -0,0 +1,6 @@ +[Web] + user_db_path = "user.db", + user_db_name = "data", + game_db_path = "game.db" + game_db_name = "data" + key = "1234" \ No newline at end of file From 99edebfb255caae710e442bae3009aa7b64fbe95 Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 15 Mar 2024 10:21:50 +0100 Subject: [PATCH 04/12] updated sqlite.data with config file --- comprl-web/app/db/sqlite.data.tsx | 40 ++++++++++++++++++------------- comprl-web/config.toml | 4 ++-- comprl-web/package-lock.json | 6 +++++ comprl-web/package.json | 1 + 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/comprl-web/app/db/sqlite.data.tsx b/comprl-web/app/db/sqlite.data.tsx index 8cb41c78..ee30e70f 100644 --- a/comprl-web/app/db/sqlite.data.tsx +++ b/comprl-web/app/db/sqlite.data.tsx @@ -1,12 +1,18 @@ import Database from 'better-sqlite3'; import { User, Statistics } from './types'; import { v4 as uuidv4 } from 'uuid'; -import { config } from "config"; +import { readFileSync } from 'fs'; +import { parse } from '@iarna/toml'; -console.log(`Creating ${config.user_db_path}`); -const userDB = new Database(config.user_db_path, { verbose: console.log }); +const configFilePath = 'config.toml'; +const tomlData = readFileSync(configFilePath, 'utf8'); +const config = parse(tomlData); +console.log(config); + +console.log(`Creating ${config.Web.user_db_path}`); +const userDB = new Database(config.Web.user_db_path, { verbose: console.log }); userDB.prepare(` - CREATE TABLE IF NOT EXISTS ${config.user_db_name}( + CREATE TABLE IF NOT EXISTS ${config.Web.user_db_name}( user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, password TEXT NOT NULL, @@ -17,10 +23,10 @@ userDB.prepare(` `).run(); userDB.close(); -console.log('Creating game.db'); -const gameDB = new Database('game.db', { verbose: console.log }); +console.log(`Creating ${config.Web.game_db_path}`); +const gameDB = new Database(config.Web.game_db_path, { verbose: console.log }); gameDB.prepare(` - CREATE TABLE IF NOT EXISTS data ( + CREATE TABLE IF NOT EXISTS ${config.Web.game_db_name} ( game_id TEXT NOT NULL PRIMARY KEY, user1 INTEGER NOT NULL, user2 INTEGER NOT NULL, @@ -34,16 +40,16 @@ gameDB.prepare(` gameDB.close(); export async function addUser(username: string, password: string, role: string = 'user') { - const userDB = new Database(config.user_db_path, { verbose: console.log }); + const userDB = new Database(config.Web.user_db_path, { verbose: console.log }); const token = uuidv4(); - const stmt = userDB.prepare(`INSERT INTO ${config.user_db_name}(username, password, role, token) VALUES (?, ?, ?, ?)`); + const stmt = userDB.prepare(`INSERT INTO ${config.Web.user_db_name}(username, password, role, token) VALUES (?, ?, ?, ?)`); stmt.run(username, password, role, token); userDB.close(); } export async function getUser(username: string, password: string) { - const userDB = new Database(config.user_db_path, { verbose: console.log }); - const stmt = userDB.prepare(`SELECT * FROM ${config.user_db_name} WHERE username = ?`); + const userDB = new Database(config.Web.user_db_path, { verbose: console.log }); + const stmt = userDB.prepare(`SELECT * FROM ${config.Web.user_db_name} WHERE username = ?`); const res = stmt.get(username); userDB.close(); if (!res) { return undefined } @@ -52,8 +58,8 @@ export async function getUser(username: string, password: string) { } export async function getAllUsers() { - const db = new Database('users.db', { verbose: console.log }); - const query = 'SELECT * FROM users'; + const db = new Database(config.Web.user_db_path, { verbose: console.log }); + const query = `SELECT * FROM ${config.Web.user_db_name}`; const users = db.prepare(query).all(); db.close(); return users; @@ -73,15 +79,15 @@ export async function getRankedUsers() { export async function getStatistics(user_id: number) { - const gameDB = new Database('game.db', { verbose: console.log }); + const gameDB = new Database(config.Web.game_db_path, { verbose: console.log }); - const stmt_played = gameDB.prepare('SELECT COUNT(*) FROM data WHERE user1 = ? OR user2 = ?'); + const stmt_played = gameDB.prepare(`SELECT COUNT(*) FROM ${config.Web.game_db_name} WHERE user1 = ? OR user2 = ?`); const playedGames = stmt_played.get(user_id, user_id)['COUNT(*)']; - const stmt_won = gameDB.prepare('SELECT COUNT(winner) FROM data WHERE winner = ?'); + const stmt_won = gameDB.prepare(`SELECT COUNT(winner) FROM ${config.Web.game_db_name} WHERE winner = ?`); const wonGames = stmt_won.get(user_id)['COUNT(winner)']; - const stmt_disconnect = gameDB.prepare('SELECT COUNT(disconnected) FROM data WHERE disconnected = ?'); + const stmt_disconnect = gameDB.prepare(`SELECT COUNT(disconnected) FROM ${config.Web.game_db_name} WHERE disconnected = ?`); const disconnectedGames = stmt_disconnect.get(user_id)['COUNT(disconnected)']; gameDB.close(); diff --git a/comprl-web/config.toml b/comprl-web/config.toml index badc566e..1d1e1a56 100644 --- a/comprl-web/config.toml +++ b/comprl-web/config.toml @@ -1,6 +1,6 @@ [Web] - user_db_path = "user.db", - user_db_name = "data", + user_db_path = "user.db" + user_db_name = "data" game_db_path = "game.db" game_db_name = "data" key = "1234" \ No newline at end of file diff --git a/comprl-web/package-lock.json b/comprl-web/package-lock.json index d8b5cb3b..cde107e4 100644 --- a/comprl-web/package-lock.json +++ b/comprl-web/package-lock.json @@ -8,6 +8,7 @@ "dependencies": { "@emotion/react": "^11.11.3", "@emotion/styled": "^11.11.0", + "@iarna/toml": "^2.2.5", "@mui/icons-material": "^5.15.12", "@mui/material": "^5.15.10", "@remix-run/css-bundle": "^2.6.0", @@ -1408,6 +1409,11 @@ "integrity": "sha512-6EwiSjwWYP7pTckG6I5eyFANjPhmPjUX9JRLUSfNPC7FX7zK9gyZAfUEaECL6ALTpGX5AjnBq3C9XmVWPitNpw==", "dev": true }, + "node_modules/@iarna/toml": { + "version": "2.2.5", + "resolved": "https://registry.npmjs.org/@iarna/toml/-/toml-2.2.5.tgz", + "integrity": "sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==" + }, "node_modules/@isaacs/cliui": { "version": "8.0.2", "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz", diff --git a/comprl-web/package.json b/comprl-web/package.json index 54a118bb..f180e07b 100644 --- a/comprl-web/package.json +++ b/comprl-web/package.json @@ -13,6 +13,7 @@ "dependencies": { "@emotion/react": "^11.11.3", "@emotion/styled": "^11.11.0", + "@iarna/toml": "^2.2.5", "@mui/icons-material": "^5.15.12", "@mui/material": "^5.15.10", "@remix-run/css-bundle": "^2.6.0", From 58c8c8ebeab32381195ba3e42f11b02a8f784efb Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 15 Mar 2024 10:41:06 +0100 Subject: [PATCH 05/12] updated auth.resgister with config file --- comprl-web/app/routes/_auth.register.tsx | 8 ++++++-- comprl-web/package-lock.json | 4 ++-- comprl-web/package.json | 1 + comprl-web/remix.config.js | 5 +++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/comprl-web/app/routes/_auth.register.tsx b/comprl-web/app/routes/_auth.register.tsx index 425df80b..a79ac09c 100644 --- a/comprl-web/app/routes/_auth.register.tsx +++ b/comprl-web/app/routes/_auth.register.tsx @@ -3,8 +3,12 @@ import { ActionFunctionArgs, redirect } from "@remix-run/node"; import { Form, useActionData } from "@remix-run/react"; import { useState } from "react"; import { addUser } from "~/db/sqlite.data"; -import { config } from "config"; +import { readFileSync } from 'fs'; +import { parse } from 'toml'; +const configFilePath = 'config.toml'; +const tomlData = readFileSync(configFilePath, 'utf8'); +const config = parse(tomlData); export async function action({ request }: ActionFunctionArgs) { const formData = await request.formData() @@ -12,7 +16,7 @@ export async function action({ request }: ActionFunctionArgs) { const username = formData.get("username") as string; const password = formData.get("password") as string; - if (key !== config.key) { + if (key !== config.Web.key) { return { alerts: [{ severity: "error", message: "Invalid key" }] } diff --git a/comprl-web/package-lock.json b/comprl-web/package-lock.json index cde107e4..3c99a9b9 100644 --- a/comprl-web/package-lock.json +++ b/comprl-web/package-lock.json @@ -22,6 +22,7 @@ "react-dom": "^18.2.0", "remix-auth": "^3.6.0", "remix-auth-form": "^1.4.0", + "toml": "^3.0.0", "uuid": "^9.0.1" }, "devDependencies": { @@ -10315,8 +10316,7 @@ "node_modules/toml": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz", - "integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==", - "dev": true + "integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==" }, "node_modules/trim-lines": { "version": "3.0.1", diff --git a/comprl-web/package.json b/comprl-web/package.json index f180e07b..8d61726e 100644 --- a/comprl-web/package.json +++ b/comprl-web/package.json @@ -27,6 +27,7 @@ "react-dom": "^18.2.0", "remix-auth": "^3.6.0", "remix-auth-form": "^1.4.0", + "toml": "^3.0.0", "uuid": "^9.0.1" }, "devDependencies": { diff --git a/comprl-web/remix.config.js b/comprl-web/remix.config.js index 7fac2d30..2d7f615f 100644 --- a/comprl-web/remix.config.js +++ b/comprl-web/remix.config.js @@ -5,4 +5,9 @@ export default { // assetsBuildDirectory: "public/build", // publicPath: "/build/", // serverBuildPath: "build/index.js", + browserNodeBuiltinsPolyfill: { + modules: { + fs: true + } + } }; From 8364be232a416879c2b43ce3e4e0fe44c3b44438 Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 15 Mar 2024 10:43:04 +0100 Subject: [PATCH 06/12] deleted ts config file --- comprl-web/config.ts | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 comprl-web/config.ts diff --git a/comprl-web/config.ts b/comprl-web/config.ts deleted file mode 100644 index 043fb107..00000000 --- a/comprl-web/config.ts +++ /dev/null @@ -1,5 +0,0 @@ -export const config = { - user_db_path: "user.db", - user_db_name: "data", - key: "1234" -}; From e2150b5194942ca9788865c4c6787b63d3dff61b Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 15 Mar 2024 22:14:33 +0100 Subject: [PATCH 07/12] Added ConfigProvider --- comprl-web/app/ConfigProvider.js | 15 ++++++++ comprl-web/app/db/sqlite.data.tsx | 45 ++++++++++++------------ comprl-web/app/routes/_auth.register.tsx | 11 +++--- 3 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 comprl-web/app/ConfigProvider.js diff --git a/comprl-web/app/ConfigProvider.js b/comprl-web/app/ConfigProvider.js new file mode 100644 index 00000000..0d40b1fc --- /dev/null +++ b/comprl-web/app/ConfigProvider.js @@ -0,0 +1,15 @@ +import { readFileSync } from 'fs'; +import { parse } from 'toml'; +const configFilePath = 'config.toml'; +let configData; +try { + const tomlData = readFileSync(configFilePath, 'utf8'); + const parsedData = parse(tomlData); + configData = JSON.stringify(parsedData); +} catch (error) { + console.error('Fehler beim Lesen der Konfigurationsdatei:', error); + process.exit(1); +} + + +export const config = configData; \ No newline at end of file diff --git a/comprl-web/app/db/sqlite.data.tsx b/comprl-web/app/db/sqlite.data.tsx index ee30e70f..1ad4dbe5 100644 --- a/comprl-web/app/db/sqlite.data.tsx +++ b/comprl-web/app/db/sqlite.data.tsx @@ -1,18 +1,19 @@ import Database from 'better-sqlite3'; import { User, Statistics } from './types'; import { v4 as uuidv4 } from 'uuid'; -import { readFileSync } from 'fs'; -import { parse } from '@iarna/toml'; +import { config } from "~/ConfigProvider"; -const configFilePath = 'config.toml'; -const tomlData = readFileSync(configFilePath, 'utf8'); -const config = parse(tomlData); -console.log(config); +const configObject = JSON.parse(config); +console.log(configObject); +const user_db_path = configObject.Web.user_db_path; +const user_db_name = configObject.Web.user_db_name; +const game_db_path = configObject.Web.game_db_path; +const game_db_name = configObject.Web.game_db_name; -console.log(`Creating ${config.Web.user_db_path}`); -const userDB = new Database(config.Web.user_db_path, { verbose: console.log }); +console.log(`Creating ${user_db_path}`); +const userDB = new Database(user_db_path, { verbose: console.log }); userDB.prepare(` - CREATE TABLE IF NOT EXISTS ${config.Web.user_db_name}( + CREATE TABLE IF NOT EXISTS ${user_db_name}( user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, password TEXT NOT NULL, @@ -23,10 +24,10 @@ userDB.prepare(` `).run(); userDB.close(); -console.log(`Creating ${config.Web.game_db_path}`); -const gameDB = new Database(config.Web.game_db_path, { verbose: console.log }); +console.log(`Creating ${game_db_path}`); +const gameDB = new Database(game_db_path, { verbose: console.log }); gameDB.prepare(` - CREATE TABLE IF NOT EXISTS ${config.Web.game_db_name} ( + CREATE TABLE IF NOT EXISTS ${game_db_name} ( game_id TEXT NOT NULL PRIMARY KEY, user1 INTEGER NOT NULL, user2 INTEGER NOT NULL, @@ -40,16 +41,16 @@ gameDB.prepare(` gameDB.close(); export async function addUser(username: string, password: string, role: string = 'user') { - const userDB = new Database(config.Web.user_db_path, { verbose: console.log }); + const userDB = new Database(user_db_path, { verbose: console.log }); const token = uuidv4(); - const stmt = userDB.prepare(`INSERT INTO ${config.Web.user_db_name}(username, password, role, token) VALUES (?, ?, ?, ?)`); + const stmt = userDB.prepare(`INSERT INTO ${user_db_name}(username, password, role, token) VALUES (?, ?, ?, ?)`); stmt.run(username, password, role, token); userDB.close(); } export async function getUser(username: string, password: string) { - const userDB = new Database(config.Web.user_db_path, { verbose: console.log }); - const stmt = userDB.prepare(`SELECT * FROM ${config.Web.user_db_name} WHERE username = ?`); + const userDB = new Database(user_db_path, { verbose: console.log }); + const stmt = userDB.prepare(`SELECT * FROM ${user_db_name} WHERE username = ?`); const res = stmt.get(username); userDB.close(); if (!res) { return undefined } @@ -58,8 +59,8 @@ export async function getUser(username: string, password: string) { } export async function getAllUsers() { - const db = new Database(config.Web.user_db_path, { verbose: console.log }); - const query = `SELECT * FROM ${config.Web.user_db_name}`; + const db = new Database(user_db_path, { verbose: console.log }); + const query = `SELECT * FROM ${user_db_name}`; const users = db.prepare(query).all(); db.close(); return users; @@ -79,15 +80,15 @@ export async function getRankedUsers() { export async function getStatistics(user_id: number) { - const gameDB = new Database(config.Web.game_db_path, { verbose: console.log }); + const gameDB = new Database(game_db_path, { verbose: console.log }); - const stmt_played = gameDB.prepare(`SELECT COUNT(*) FROM ${config.Web.game_db_name} WHERE user1 = ? OR user2 = ?`); + const stmt_played = gameDB.prepare(`SELECT COUNT(*) FROM ${game_db_name} WHERE user1 = ? OR user2 = ?`); const playedGames = stmt_played.get(user_id, user_id)['COUNT(*)']; - const stmt_won = gameDB.prepare(`SELECT COUNT(winner) FROM ${config.Web.game_db_name} WHERE winner = ?`); + const stmt_won = gameDB.prepare(`SELECT COUNT(winner) FROM ${game_db_name} WHERE winner = ?`); const wonGames = stmt_won.get(user_id)['COUNT(winner)']; - const stmt_disconnect = gameDB.prepare(`SELECT COUNT(disconnected) FROM ${config.Web.game_db_name} WHERE disconnected = ?`); + const stmt_disconnect = gameDB.prepare(`SELECT COUNT(disconnected) FROM ${game_db_name} WHERE disconnected = ?`); const disconnectedGames = stmt_disconnect.get(user_id)['COUNT(disconnected)']; gameDB.close(); diff --git a/comprl-web/app/routes/_auth.register.tsx b/comprl-web/app/routes/_auth.register.tsx index a79ac09c..b73d091d 100644 --- a/comprl-web/app/routes/_auth.register.tsx +++ b/comprl-web/app/routes/_auth.register.tsx @@ -3,12 +3,7 @@ import { ActionFunctionArgs, redirect } from "@remix-run/node"; import { Form, useActionData } from "@remix-run/react"; import { useState } from "react"; import { addUser } from "~/db/sqlite.data"; -import { readFileSync } from 'fs'; -import { parse } from 'toml'; - -const configFilePath = 'config.toml'; -const tomlData = readFileSync(configFilePath, 'utf8'); -const config = parse(tomlData); +import { config } from "~/ConfigProvider"; export async function action({ request }: ActionFunctionArgs) { const formData = await request.formData() @@ -16,7 +11,9 @@ export async function action({ request }: ActionFunctionArgs) { const username = formData.get("username") as string; const password = formData.get("password") as string; - if (key !== config.Web.key) { + const configObject = JSON.parse(config); + + if (key !== configObject.Web.key) { return { alerts: [{ severity: "error", message: "Invalid key" }] } From bcd4769a9a6fe1e7e83652913387b78cf5b13b40 Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 15 Mar 2024 22:34:17 +0100 Subject: [PATCH 08/12] updated new functions --- comprl-web/app/ConfigProvider.js | 2 +- comprl-web/app/db/sqlite.data.tsx | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/comprl-web/app/ConfigProvider.js b/comprl-web/app/ConfigProvider.js index 0d40b1fc..c420983f 100644 --- a/comprl-web/app/ConfigProvider.js +++ b/comprl-web/app/ConfigProvider.js @@ -7,7 +7,7 @@ try { const parsedData = parse(tomlData); configData = JSON.stringify(parsedData); } catch (error) { - console.error('Fehler beim Lesen der Konfigurationsdatei:', error); + console.error('Error reading the configuration file:', error); process.exit(1); } diff --git a/comprl-web/app/db/sqlite.data.tsx b/comprl-web/app/db/sqlite.data.tsx index d7e09936..16ceeb92 100644 --- a/comprl-web/app/db/sqlite.data.tsx +++ b/comprl-web/app/db/sqlite.data.tsx @@ -59,8 +59,8 @@ export async function getUser(username: string, password: string) { } export async function getUsername(user_id: number) { - const userDB = new Database('users.db', { verbose: console.log }); - const stmt = userDB.prepare('SELECT username FROM users WHERE user_id = ?'); + const userDB = new Database(user_db_path, { verbose: console.log }); + const stmt = userDB.prepare(`SELECT username FROM ${user_db_name} WHERE user_id = ?`); const res = stmt.get(user_id); userDB.close(); return res.username; @@ -133,8 +133,8 @@ export async function composeGame(game: Game) { } export async function getGame(game_id: string) { - const gameDB = new Database('game.db', { verbose: console.log }); - const stmt = gameDB.prepare('SELECT * FROM data WHERE game_id=?'); + const gameDB = new Database(game_db_path, { verbose: console.log }); + const stmt = gameDB.prepare(`SELECT * FROM ${game_db_name} WHERE game_id=?`); const game = stmt.get(game_id) gameDB.close(); From 8333c3daac3f8d56f0dcb14d61b9e657c1ef17b0 Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 15 Mar 2024 23:08:56 +0100 Subject: [PATCH 09/12] changed ConfigProvider --- comprl-web/app/ConfigProvider.js | 1 + comprl-web/app/db/sqlite.data.tsx | 11 +++++------ comprl-web/app/routes/_auth.register.tsx | 4 +--- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/comprl-web/app/ConfigProvider.js b/comprl-web/app/ConfigProvider.js index c420983f..e2ac3695 100644 --- a/comprl-web/app/ConfigProvider.js +++ b/comprl-web/app/ConfigProvider.js @@ -6,6 +6,7 @@ try { const tomlData = readFileSync(configFilePath, 'utf8'); const parsedData = parse(tomlData); configData = JSON.stringify(parsedData); + configData = JSON.parse(configData); } catch (error) { console.error('Error reading the configuration file:', error); process.exit(1); diff --git a/comprl-web/app/db/sqlite.data.tsx b/comprl-web/app/db/sqlite.data.tsx index 16ceeb92..c6ed16c0 100644 --- a/comprl-web/app/db/sqlite.data.tsx +++ b/comprl-web/app/db/sqlite.data.tsx @@ -3,12 +3,11 @@ import { User, Statistics, Game } from './types'; import { v4 as uuidv4 } from 'uuid'; import { config } from "~/ConfigProvider"; -const configObject = JSON.parse(config); -console.log(configObject); -const user_db_path = configObject.Web.user_db_path; -const user_db_name = configObject.Web.user_db_name; -const game_db_path = configObject.Web.game_db_path; -const game_db_name = configObject.Web.game_db_name; +console.log(config); +const user_db_path = config.Web.user_db_path; +const user_db_name = config.Web.user_db_name; +const game_db_path = config.Web.game_db_path; +const game_db_name = config.Web.game_db_name; console.log(`Creating ${user_db_path}`); const userDB = new Database(user_db_path, { verbose: console.log }); diff --git a/comprl-web/app/routes/_auth.register.tsx b/comprl-web/app/routes/_auth.register.tsx index b73d091d..1dd355d1 100644 --- a/comprl-web/app/routes/_auth.register.tsx +++ b/comprl-web/app/routes/_auth.register.tsx @@ -11,9 +11,7 @@ export async function action({ request }: ActionFunctionArgs) { const username = formData.get("username") as string; const password = formData.get("password") as string; - const configObject = JSON.parse(config); - - if (key !== configObject.Web.key) { + if (key !== config.Web.key) { return { alerts: [{ severity: "error", message: "Invalid key" }] } From 23f464feb215af1e49b84ea625fc817d199b2ba6 Mon Sep 17 00:00:00 2001 From: Svea Date: Fri, 15 Mar 2024 23:24:23 +0100 Subject: [PATCH 10/12] changed README --- comprl-web/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comprl-web/README.md b/comprl-web/README.md index 5a452b02..02575f13 100644 --- a/comprl-web/README.md +++ b/comprl-web/README.md @@ -29,5 +29,5 @@ npm start Now you'll need to pick a host to deploy it to. ## Config File -The `user_db_path`, the `user_db_name` and the `key` are defined in the config file `config.ts`. +The `user_db_path`, the `user_db_name` and the `key` are defined in the config file `config.toml`. The current key is `1234`. \ No newline at end of file From 2373942d51caab81bc5607d812b23b438c729933 Mon Sep 17 00:00:00 2001 From: Paul Masan Date: Fri, 15 Mar 2024 23:40:17 +0100 Subject: [PATCH 11/12] config now supports cmd overwrite --- comprl-web/app/ConfigProvider.js | 16 ------------ comprl-web/app/config.tsx | 31 ++++++++++++++++++++++++ comprl-web/app/db/sqlite.data.tsx | 2 +- comprl-web/app/routes/_auth.register.tsx | 2 +- 4 files changed, 33 insertions(+), 18 deletions(-) delete mode 100644 comprl-web/app/ConfigProvider.js create mode 100644 comprl-web/app/config.tsx diff --git a/comprl-web/app/ConfigProvider.js b/comprl-web/app/ConfigProvider.js deleted file mode 100644 index e2ac3695..00000000 --- a/comprl-web/app/ConfigProvider.js +++ /dev/null @@ -1,16 +0,0 @@ -import { readFileSync } from 'fs'; -import { parse } from 'toml'; -const configFilePath = 'config.toml'; -let configData; -try { - const tomlData = readFileSync(configFilePath, 'utf8'); - const parsedData = parse(tomlData); - configData = JSON.stringify(parsedData); - configData = JSON.parse(configData); -} catch (error) { - console.error('Error reading the configuration file:', error); - process.exit(1); -} - - -export const config = configData; \ No newline at end of file diff --git a/comprl-web/app/config.tsx b/comprl-web/app/config.tsx new file mode 100644 index 00000000..0a0ea746 --- /dev/null +++ b/comprl-web/app/config.tsx @@ -0,0 +1,31 @@ +import { readFileSync } from 'fs'; +import { parse } from 'toml'; + +export function parseArgs() { + const args = {};; + process.argv + .slice(2, process.argv.length) + .forEach(arg => { + const longArg = arg.split('='); + args[longArg[0]] = longArg[1]; + }); + return args; +} + +export function parseConfig() { + const args = parseArgs(); + console.log(args); + const configFilePath = args['config'] || 'config.toml'; + try { + console.log('Reading configuration file:', configFilePath); + const tomlData = readFileSync(configFilePath, 'utf8'); + const parsedData = parse(tomlData); + const str = JSON.stringify(parsedData) + return JSON.parse(str); + } catch (error) { + console.error('Error reading the configuration file:', error); + return null; + } +} + +export const config = parseConfig(); \ No newline at end of file diff --git a/comprl-web/app/db/sqlite.data.tsx b/comprl-web/app/db/sqlite.data.tsx index c6ed16c0..37ce5279 100644 --- a/comprl-web/app/db/sqlite.data.tsx +++ b/comprl-web/app/db/sqlite.data.tsx @@ -1,7 +1,7 @@ import Database from 'better-sqlite3'; import { User, Statistics, Game } from './types'; import { v4 as uuidv4 } from 'uuid'; -import { config } from "~/ConfigProvider"; +import { config } from "~/config"; console.log(config); const user_db_path = config.Web.user_db_path; diff --git a/comprl-web/app/routes/_auth.register.tsx b/comprl-web/app/routes/_auth.register.tsx index 1dd355d1..1a5a8f16 100644 --- a/comprl-web/app/routes/_auth.register.tsx +++ b/comprl-web/app/routes/_auth.register.tsx @@ -3,7 +3,7 @@ import { ActionFunctionArgs, redirect } from "@remix-run/node"; import { Form, useActionData } from "@remix-run/react"; import { useState } from "react"; import { addUser } from "~/db/sqlite.data"; -import { config } from "~/ConfigProvider"; +import { config } from "~/config"; export async function action({ request }: ActionFunctionArgs) { const formData = await request.formData() From d2010721a65b86dce96befcc4a2590634a001c4a Mon Sep 17 00:00:00 2001 From: Paul Masan Date: Fri, 15 Mar 2024 23:44:11 +0100 Subject: [PATCH 12/12] fixed typing and linting errors --- comprl-web/app/config.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comprl-web/app/config.tsx b/comprl-web/app/config.tsx index 0a0ea746..9807d7f6 100644 --- a/comprl-web/app/config.tsx +++ b/comprl-web/app/config.tsx @@ -2,7 +2,7 @@ import { readFileSync } from 'fs'; import { parse } from 'toml'; export function parseArgs() { - const args = {};; + const args: { [key: string]: string } = {}; // Add index signature process.argv .slice(2, process.argv.length) .forEach(arg => {