refactor: simplify db credential handling

This commit is contained in:
Dragory 2024-04-06 15:36:45 +00:00
parent 898cf18885
commit 1ef7ba8d0f
No known key found for this signature in database
6 changed files with 66 additions and 72 deletions

View file

@ -4,28 +4,28 @@
"description": "", "description": "",
"private": true, "private": true,
"scripts": { "scripts": {
"watch": "NODE_ENV=development HOST_MODE=development tsc-watch --build --onSuccess \"node start-dev.js\"", "watch": "tsc-watch --build --onSuccess \"node start-dev.js\"",
"watch-yaml-parse-test": "NODE_ENV=development HOST_MODE=development tsc-watch --build --onSuccess \"node dist/yamlParseTest.js\"", "watch-yaml-parse-test": "tsc-watch --build --onSuccess \"node dist/yamlParseTest.js\"",
"build": "tsc --build", "build": "tsc --build",
"start-bot-dev": "NODE_ENV=development HOST_MODE=development node --enable-source-maps --stack-trace-limit=30 --inspect=0.0.0.0:9229 dist/index.js", "start-bot-dev": "node --enable-source-maps --stack-trace-limit=30 --inspect=0.0.0.0:9229 dist/index.js",
"start-bot-dev-debug": "NODE_ENV=development DEBUG=true clinic heapprofiler --collect-only --dest .clinic-bot -- node --enable-source-maps --stack-trace-limit=30 --inspect=0.0.0.0:9229 dist/index.js", "start-bot-dev-debug": "DEBUG=true clinic heapprofiler --collect-only --dest .clinic-bot -- node --enable-source-maps --stack-trace-limit=30 --inspect=0.0.0.0:9229 dist/index.js",
"start-bot-prod": "cross-env NODE_ENV=production node --enable-source-maps --stack-trace-limit=30 dist/index.js", "start-bot-prod": "node --enable-source-maps --stack-trace-limit=30 dist/index.js",
"start-bot-prod-debug": "NODE_ENV=production DEBUG=true clinic heapprofiler --collect-only --dest .clinic-bot -- node --enable-source-maps --stack-trace-limit=30 dist/index.js", "start-bot-prod-debug": "DEBUG=true clinic heapprofiler --collect-only --dest .clinic-bot -- node --enable-source-maps --stack-trace-limit=30 dist/index.js",
"watch-bot": "NODE_ENV=development HOST_MODE=development tsc-watch --build --onSuccess \"npm run start-bot-dev\"", "watch-bot": "tsc-watch --build --onSuccess \"npm run start-bot-dev\"",
"start-api-dev": "NODE_ENV=development HOST_MODE=development node --enable-source-maps --stack-trace-limit=30 --inspect=0.0.0.0:9239 dist/api/index.js", "start-api-dev": "node --enable-source-maps --stack-trace-limit=30 --inspect=0.0.0.0:9239 dist/api/index.js",
"start-api-dev-debug": "NODE_ENV=development DEBUG=true clinic heapprofiler --collect-only --dest .clinic-api -- node --enable-source-maps --stack-trace-limit=30 --inspect=0.0.0.0:9239 dist/api/index.js", "start-api-dev-debug": "DEBUG=true clinic heapprofiler --collect-only --dest .clinic-api -- node --enable-source-maps --stack-trace-limit=30 --inspect=0.0.0.0:9239 dist/api/index.js",
"start-api-prod": "cross-env NODE_ENV=production node --enable-source-maps --stack-trace-limit=30 dist/api/index.js", "start-api-prod": "node --enable-source-maps --stack-trace-limit=30 dist/api/index.js",
"start-api-prod-debug": "NODE_ENV=production DEBUG=true clinic heapprofiler --collect-only --dest .clinic-api -- node --enable-source-maps --stack-trace-limit=30 dist/api/index.js", "start-api-prod-debug": "clinic heapprofiler --collect-only --dest .clinic-api -- node --enable-source-maps --stack-trace-limit=30 dist/api/index.js",
"watch-api": "NODE_ENV=development HOST_MODE=development tsc-watch --build --onSuccess \"npm run start-api-dev\"", "watch-api": "tsc-watch --build --onSuccess \"npm run start-api-dev\"",
"typeorm": "node ../node_modules/typeorm/cli.js", "typeorm": "node ../node_modules/typeorm/cli.js",
"migrate": "npm run typeorm -- migration:run -d dist/data/dataSource.js", "migrate": "npm run typeorm -- migration:run -d dist/data/dataSource.js",
"migrate-prod": "NODE_ENV=production npm run migrate", "migrate-prod": "npm run migrate",
"migrate-dev": "(export NODE_ENV=development; export HOST_MODE=development; npm run build && npm run migrate)", "migrate-dev": "npm run build && npm run migrate",
"migrate-rollback": "npm run typeorm -- migration:revert -d dist/data/dataSource.js", "migrate-rollback": "npm run typeorm -- migration:revert -d dist/data/dataSource.js",
"migrate-rollback-prod": "NODE_ENV=production npm run migrate-rollback", "migrate-rollback-prod": "npm run migrate-rollback",
"migrate-rollback-dev": "(export NODE_ENV=development; export HOST_MODE=development; npm run build && npm run migrate-rollback)", "migrate-rollback-dev": "npm run build && npm run migrate-rollback",
"validate-active-configs": "NODE_ENV=development HOST_MODE=development node --enable-source-maps dist/validateActiveConfigs.js > ../config-errors.txt", "validate-active-configs": "node --enable-source-maps dist/validateActiveConfigs.js > ../config-errors.txt",
"export-config-json-schema": "NODE_ENV=development HOST_MODE=development node --enable-source-maps dist/exportSchemas.js > ../config-schema.json", "export-config-json-schema": "node --enable-source-maps dist/exportSchemas.js > ../config-schema.json",
"test": "npm run build && npm run run-tests", "test": "npm run build && npm run run-tests",
"run-tests": "ava", "run-tests": "ava",
"test-watch": "tsc-watch --build --onSuccess \"npx ava\"" "test-watch": "tsc-watch --build --onSuccess \"npx ava\""

View file

@ -10,7 +10,7 @@ import { initGuildsAPI } from "./guilds/index";
import { clientError, error, notFound } from "./responses"; import { clientError, error, notFound } from "./responses";
import { startBackgroundTasks } from "./tasks"; import { startBackgroundTasks } from "./tasks";
const apiPathPrefix = env.HOST_MODE === "lightweight" ? env.LIGHTWEIGHT_API_PATH_PREFIX || "" : "/api"; const apiPathPrefix = env.NODE_ENV === "production" ? env.API_PATH_PREFIX ?? "" : "/api";
const app = express(); const app = express();

View file

@ -11,34 +11,22 @@ const entities = path.relative(process.cwd(), path.resolve(backendDir, "dist/dat
const migrations = path.relative(process.cwd(), path.resolve(backendDir, "dist/migrations/*.js")); const migrations = path.relative(process.cwd(), path.resolve(backendDir, "dist/migrations/*.js"));
type DbOpts = Pick<MysqlConnectionOptions, "host" | "port" | "username" | "password" | "database">; type DbOpts = Pick<MysqlConnectionOptions, "host" | "port" | "username" | "password" | "database">;
let dbOpts: DbOpts; const dbOpts: DbOpts =
if (env.HOST_MODE === "development") { env.NODE_ENV === "production"
dbOpts = { ? {
host: env.DB_HOST,
port: env.DB_PORT,
username: env.DB_USER,
password: env.DB_PASSWORD,
database: env.DB_DATABASE,
}
: {
host: "mysql", host: "mysql",
port: 3306, port: 3306,
username: "zeppelin", username: "zeppelin",
password: env.DEVELOPMENT_MYSQL_PASSWORD, password: env.DEVELOPMENT_MYSQL_PASSWORD,
database: "zeppelin", database: "zeppelin",
}; };
} else if (env.HOST_MODE === "standalone") {
dbOpts = {
host: "mysql",
port: 3306,
username: "zeppelin",
password: env.STANDALONE_MYSQL_PASSWORD,
database: "zeppelin",
};
} else if (env.HOST_MODE === "lightweight") {
dbOpts = {
host: env.LIGHTWEIGHT_DB_HOST,
port: env.LIGHTWEIGHT_DB_PORT,
username: env.LIGHTWEIGHT_DB_USER,
password: env.LIGHTWEIGHT_DB_PASSWORD,
database: env.LIGHTWEIGHT_DB_DATABASE,
};
} else {
throw new Error(`Unknown host mode: ${env.HOST_MODE}`);
}
export const dataSource = new DataSource({ export const dataSource = new DataSource({
type: "mysql", type: "mysql",

View file

@ -38,22 +38,22 @@ const envType = z.object({
PHISHERMAN_API_KEY: z.string().optional(), PHISHERMAN_API_KEY: z.string().optional(),
DEVELOPMENT_MYSQL_PASSWORD: z.string().optional(), // Included here for the DB_PASSWORD default in development DB_HOST: z.string().optional(),
STANDALONE_MYSQL_PASSWORD: z.string().optional(), // Included here for the DB_PASSWORD default in production DB_PORT: z.preprocess((v) => Number(v), z.number()).optional(),
DB_USER: z.string().optional(),
DB_PASSWORD: z.string().optional(),
DB_DATABASE: z.string().optional(),
LIGHTWEIGHT_DB_HOST: z.string().optional(), DEVELOPMENT_MYSQL_PASSWORD: z.string().optional(),
LIGHTWEIGHT_DB_PORT: z.preprocess((v) => Number(v), z.number()).optional(),
LIGHTWEIGHT_DB_USER: z.string().optional(),
LIGHTWEIGHT_DB_PASSWORD: z.string().optional(),
LIGHTWEIGHT_DB_DATABASE: z.string().optional(),
LIGHTWEIGHT_API_PATH_PREFIX: z.string().optional(), API_PATH_PREFIX: z.string().optional(),
HOST_MODE: z.enum(["development", "standalone", "lightweight"]).optional().default("lightweight"),
DEBUG: z DEBUG: z
.string() .string()
.optional() .optional()
.transform((str) => str === "true"), .transform((str) => str === "true"),
NODE_ENV: z.string().default("development"),
}); });
let toValidate = { ...process.env }; let toValidate = { ...process.env };

View file

@ -7,8 +7,14 @@ services:
args: args:
# Used at compile-time by dashboard # Used at compile-time by dashboard
API_URL: API_URL:
environment: environment: &env
HOST_MODE: lightweight - NODE_ENV=production
- DB_HOST=${LIGHTWEIGHT_DB_HOST}
- DB_PORT=${LIGHTWEIGHT_DB_PORT}
- DB_USER=${LIGHTWEIGHT_DB_USER}
- DB_PASSWORD=${LIGHTWEIGHT_DB_PASSWORD}
- DB_DATABASE=${LIGHTWEIGHT_DB_DATABASE}
- API_PATH_PREFIX=${LIGHTWEIGHT_API_PATH_PREFIX}
env_file: env_file:
- .env - .env
working_dir: /zeppelin/backend working_dir: /zeppelin/backend
@ -20,8 +26,7 @@ services:
condition: service_completed_successfully condition: service_completed_successfully
build: *build build: *build
restart: on-failure restart: on-failure
environment: environment: *env
HOST_MODE: lightweight
env_file: env_file:
- .env - .env
ports: ports:
@ -35,8 +40,7 @@ services:
condition: service_completed_successfully condition: service_completed_successfully
build: *build build: *build
restart: on-failure restart: on-failure
environment: environment: *env
HOST_MODE: lightweight
env_file: env_file:
- .env - .env
working_dir: /zeppelin/backend working_dir: /zeppelin/backend
@ -48,8 +52,7 @@ services:
condition: service_completed_successfully condition: service_completed_successfully
build: *build build: *build
restart: on-failure restart: on-failure
environment: environment: *env
HOST_MODE: lightweight
env_file: env_file:
- .env - .env
ports: ports:

View file

@ -42,8 +42,14 @@ services:
args: args:
# Used at compile-time by dashboard # Used at compile-time by dashboard
API_URL: API_URL:
environment: environment: &env
HOST_MODE: standalone - NODE_ENV=production
- DB_HOST=mysql
- DB_PORT=3306
- DB_USER=zeppelin
- DB_PASSWORD=${STANDALONE_MYSQL_PASSWORD}
- DB_DATABASE=zeppelin
- API_PATH_PREFIX=/api
env_file: env_file:
- .env - .env
working_dir: /zeppelin/backend working_dir: /zeppelin/backend
@ -55,8 +61,7 @@ services:
condition: service_completed_successfully condition: service_completed_successfully
build: *build build: *build
restart: on-failure restart: on-failure
environment: environment: *env
HOST_MODE: standalone
env_file: env_file:
- .env - .env
working_dir: /zeppelin/backend working_dir: /zeppelin/backend
@ -68,8 +73,7 @@ services:
condition: service_completed_successfully condition: service_completed_successfully
build: *build build: *build
restart: on-failure restart: on-failure
environment: environment: *env
HOST_MODE: standalone
env_file: env_file:
- .env - .env
working_dir: /zeppelin/backend working_dir: /zeppelin/backend
@ -81,8 +85,7 @@ services:
condition: service_completed_successfully condition: service_completed_successfully
build: *build build: *build
restart: on-failure restart: on-failure
environment: environment: *env
HOST_MODE: standalone
env_file: env_file:
- .env - .env
working_dir: /zeppelin/dashboard working_dir: /zeppelin/dashboard