From a199f19ff4d62e6729111e6da73337d0625de03d Mon Sep 17 00:00:00 2001 From: Kai Ritthaler Date: Tue, 20 May 2025 15:54:34 +0200 Subject: [PATCH] changed installscript and docker-compose to install minIO --- code/backend/scripts/install.json | 27 +++++++++++- code/backend/scripts/install.ts | 69 +++++++++++++++++++------------ code/db/docker-compose.yaml | 14 +++++++ 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/code/backend/scripts/install.json b/code/backend/scripts/install.json index ef00102..ad5b3c1 100644 --- a/code/backend/scripts/install.json +++ b/code/backend/scripts/install.json @@ -8,5 +8,30 @@ "echo migrating database", "yarn prisma migrate dev --name init" ], - "requiredKeys": ["DATABASE_URL", "TOKEN_SECRET", "DB_USER", "DB_PASSWORD"] + "requiredKeys": [ + { "name": "DATABASE_URL", "generated": true }, + { "name": "TOKEN_SECRET", "generated": true }, + { + "name": "DB_USER", + "generated": false, + "default": "postgres", + "hide": false + }, + { + "name": "DB_PASSWORD", + "generated": false, + "hide": true + }, + { + "name": "MINIO_USER", + "generated": false, + "default": "minIO", + "hide": false + }, + { + "name": "MINIO_PASSWORD", + "generated": false, + "hide": true + } + ] } diff --git a/code/backend/scripts/install.ts b/code/backend/scripts/install.ts index 86d6df7..287e3de 100644 --- a/code/backend/scripts/install.ts +++ b/code/backend/scripts/install.ts @@ -6,55 +6,72 @@ import fs, { read } from "fs"; import readlineSync from "readline-sync"; import crypto from "crypto"; import dotenv from "dotenv"; -import { boolean } from "zod"; + +type env_config = { + name: string; + generated: boolean; + input?: string; + default?: string; + hide?: boolean; +}; +type json_config = { + commands: string[]; + requiredKeys: env_config[]; +}; const json_path: string = "scripts/install.json"; // Path to the JSON file const raw = fs.readFileSync(json_path, "utf8"); -const config = JSON.parse(raw); // Parse the JSON file +const config: json_config = JSON.parse(raw); // Parse the JSON file //check if there is a .env and if it has the correct settings -let allSet: boolean = true; +let missingConfigs: env_config[] = []; if (fs.existsSync(".env")) { dotenv.config(); - for (const key of config.requiredKeys) { - if (!process.env[key]) { - allSet = false; - break; + for (const setting of config.requiredKeys) { + if (!process.env[setting.name]) { + missingConfigs.push(setting); } } -} else { - allSet = false; } -if (allSet) { +if (missingConfigs.length < 1) { // if it`s all set abort the installation process.exit(0); } + // getting user input for the PostgreSQL username and password console.log("generrating .env file"); -const postgresUser: string = readlineSync.question( - "Enter the PostgreSQL username (default: postgres): ", - { defaultInput: "postgres" } -); -let postgresPassword: string = ""; -while (!postgresPassword) { - postgresPassword = readlineSync.question("Enter the PostgreSQL password: ", { - hideEchoBack: true, // Hides the characters - }); - !postgresPassword && console.log("Password cannot be empty"); +for (const setting of missingConfigs) { + if (!setting.generated) { + let input: string = ""; + do { + input = readlineSync.question( + `Enter the ${setting.name} ${setting.default ? `(${setting.default})` : ""}: `, + { defaultInput: setting.default, hideEchoBack: setting.hide } + ); + } while (!input); + process.env[setting.name] = input; + } else if (setting.name === "TOKEN_SECRET") { + // generating a random JWT secret + const jwtSecret: string = crypto.randomBytes(32).toString("hex"); // 64 Zeichen + process.env[setting.name] = jwtSecret; + } } -// generating a random JWT secret -const jwtSecret: string = crypto.randomBytes(32).toString("hex"); // 64 Zeichen -const env: string = `DATABASE_URL="postgresql://${postgresUser}:${postgresPassword}@localhost:5432/prisma" -TOKEN_SECRET="${jwtSecret}" -DB_USER="${postgresUser}" -DB_PASSWORD="${postgresPassword}"`; +let env: string = `DATABASE_URL="postgresql://${process.env["DB_USER"]}:${process.env["DB_PASSWORD"]}@localhost:5432/prisma"\n`; +for (const setting of config.requiredKeys) { + if (setting.name != "DATABASE_URL") { + env += `${setting.name}="${process.env[setting.name]}"\n`; + } +} + +// writing the .env file try { fs.writeFileSync(".env", env); console.log("File has been written successfully."); } catch (err) { console.error("Error writing to file:", err); } + // running the commands from the JSON file // The commands are executed in the order they are defined in the JSON file (async () => { diff --git a/code/db/docker-compose.yaml b/code/db/docker-compose.yaml index 37833cd..79d1b4e 100644 --- a/code/db/docker-compose.yaml +++ b/code/db/docker-compose.yaml @@ -11,5 +11,19 @@ services: volumes: - pgdata:/var/lib/postgresql/data + minio: + image: quay.io/minio/minio + container_name: minio + ports: + - "9000:9000" # API + - "9001:9001" # Web GUI + environment: + MINIO_ROOT_USER: ${MINIO_USER} + MINIO_ROOT_PASSWORD: ${MINIO_PASSWORD} + command: server /data --console-address ":9001" + volumes: + - minio-data:/data + volumes: pgdata: + minio-data: