From 45e22b320ec530a8ebdf848d2dba375ad4f85a9d Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Thu, 23 May 2024 09:46:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8env-var=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 14 ++++++++++++ package.json | 1 + src/config.ts | 58 +++++++++++------------------------------------ 3 files changed, 28 insertions(+), 45 deletions(-) diff --git a/package-lock.json b/package-lock.json index ce3a54b..cca6715 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "cli-progress": "^3.12.0", "colors": "^1.3.3", "dotenv": "^16.4.1", + "env-var": "^7.5.0", "express": "^4.18.1", "fs-extra": "^8.1.0", "got": "^14.2.0", @@ -2420,6 +2421,14 @@ "node": ">=10.0.0" } }, + "node_modules/env-var": { + "version": "7.5.0", + "resolved": "https://registry.npmjs.org/env-var/-/env-var-7.5.0.tgz", + "integrity": "sha512-mKZOzLRN0ETzau2W2QXefbFjo5EF4yWq28OyKb9ICdeNhHJlOE/pHHnz4hdYJ9cNZXcJHo5xN4OT4pzuSHSNvA==", + "engines": { + "node": ">=10" + } + }, "node_modules/esbuild": { "version": "0.17.18", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.18.tgz", @@ -8827,6 +8836,11 @@ "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.2.1.tgz", "integrity": "sha512-9JktcM3u18nU9N2Lz3bWeBgxVgOKpw7yhRaoxQA3FUDZzzw+9WlA6p4G4u0RixNkg14fH7EfEc/RhpurtiROTQ==" }, + "env-var": { + "version": "7.5.0", + "resolved": "https://registry.npmjs.org/env-var/-/env-var-7.5.0.tgz", + "integrity": "sha512-mKZOzLRN0ETzau2W2QXefbFjo5EF4yWq28OyKb9ICdeNhHJlOE/pHHnz4hdYJ9cNZXcJHo5xN4OT4pzuSHSNvA==" + }, "esbuild": { "version": "0.17.18", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.18.tgz", diff --git a/package.json b/package.json index ccd963c..77ae5c3 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "cli-progress": "^3.12.0", "colors": "^1.3.3", "dotenv": "^16.4.1", + "env-var": "^7.5.0", "express": "^4.18.1", "fs-extra": "^8.1.0", "got": "^14.2.0", diff --git a/src/config.ts b/src/config.ts index 0a1c2b4..710e449 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,6 @@ import dotenv from 'dotenv' import {z} from 'zod' +import env from 'env-var' export interface IConfigFlavor { runtime: string @@ -9,54 +10,21 @@ export interface IConfigFlavor { export class Config { public static instance: Config - public readonly clusterId: string - public readonly clusterSecret: string - public readonly clusterIp?: string - public readonly port: number = 4000 - public readonly clusterPublicPort?: number - public readonly byoc: boolean = false - public readonly disableAccessLog: boolean = false - - public readonly enableNginx: boolean = false - public readonly enableUpnp: boolean = false - public readonly storage: string = 'file' - public readonly storageOpts: unknown + public readonly clusterId = env.get('CLUSTER_ID').required().asString() + public readonly clusterSecret = env.get('CLUSTER_SECRET').required().asString() + public readonly clusterIp? = env.get('CLUSTER_IP').asString() + public readonly port: number = env.get('CLUSTER_PORT').default(4000).asPortNumber() + public readonly clusterPublicPort = env.get('CLUSTER_PUBLIC_PORT').default(this.port).asPortNumber() + public readonly byoc = env.get('CLUSTER_BYOC').asBool() + public readonly disableAccessLog = env.get('DISABLE_ACCESS_LOG').asBool() + + public readonly enableNginx = env.get('ENABLE_NGINX').asBool() + public readonly enableUpnp = env.get('ENABLE_UPNP').asBool() + public readonly storage = env.get('CLUSTER_STORAGE').default('file').asString() + public readonly storageOpts = env.get('CLUSTER_STORAGE_OPTIONS').asJsonObject() public readonly flavor: IConfigFlavor private constructor() { - if (!process.env.CLUSTER_ID) { - throw new Error('CLUSTER_ID is not set') - } - this.clusterId = process.env.CLUSTER_ID - if (!process.env.CLUSTER_SECRET) { - throw new Error('CLUSTER_SECRET is not set') - } - this.clusterSecret = process.env.CLUSTER_SECRET - this.clusterIp = process.env.CLUSTER_IP - if (process.env.CLUSTER_PORT) { - this.port = parseInt(process.env.CLUSTER_PORT, 10) - if (isNaN(this.port)) { - throw new Error('CLUSTER_PORT is not a number') - } - } - this.clusterPublicPort = process.env.CLUSTER_PUBLIC_PORT ? parseInt(process.env.CLUSTER_PUBLIC_PORT, 10) : undefined - if (typeof this.clusterPublicPort === 'number' && isNaN(this.clusterPublicPort)) { - throw new Error('CLUSTER_PUBLIC_PORT is not a number') - } - this.byoc = process.env.CLUSTER_BYOC === 'true' - this.enableNginx = process.env.ENABLE_NGINX === 'true' - if (process.env.CLUSTER_STORAGE) { - this.storage = process.env.CLUSTER_STORAGE - } - if (process.env.CLUSTER_STORAGE_OPTIONS) { - this.storageOpts = JSON.parse(process.env.CLUSTER_STORAGE_OPTIONS) - } - if (process.env.DISABLE_ACCESS_LOG) { - this.disableAccessLog = true - } - if (process.env.ENABLE_UPNP) { - this.enableUpnp = true - } this.flavor = { runtime: `Node.js/${process.version}`, storage: this.storage,