feat: support load yaml config
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@mx-space/core",
|
||||
"version": "3.38.1",
|
||||
"version": "3.38.2",
|
||||
"author": "Innei <https://innei.ren>",
|
||||
"private": true,
|
||||
"license": "AGPLv3",
|
||||
@@ -92,6 +92,7 @@
|
||||
"class-transformer": "0.5.1",
|
||||
"class-validator": "0.13.2",
|
||||
"class-validator-jsonschema": "npm:@innei/class-validator-jsonschema@3.1.2",
|
||||
"commander": "9.4.1",
|
||||
"consola": "*",
|
||||
"dayjs": "1.11.5",
|
||||
"ejs": "3.1.8",
|
||||
@@ -192,4 +193,4 @@
|
||||
"pino": "./external/pino",
|
||||
"get-pixels@^3>request": "./external/request"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
pnpm-lock.yaml
generated
9
pnpm-lock.yaml
generated
@@ -67,6 +67,7 @@ specifiers:
|
||||
class-transformer: 0.5.1
|
||||
class-validator: 0.13.2
|
||||
class-validator-jsonschema: npm:@innei/class-validator-jsonschema@3.1.2
|
||||
commander: 9.4.1
|
||||
consola: '*'
|
||||
cron: '*'
|
||||
cross-env: 7.0.3
|
||||
@@ -164,6 +165,7 @@ dependencies:
|
||||
class-transformer: 0.5.1
|
||||
class-validator: 0.13.2
|
||||
class-validator-jsonschema: /@innei/class-validator-jsonschema/3.1.2_e6kgdsnyya5caxg3ysdyxrqm7a
|
||||
commander: 9.4.1
|
||||
consola: 2.15.3
|
||||
dayjs: 1.11.5
|
||||
ejs: 3.1.8
|
||||
@@ -2711,10 +2713,9 @@ packages:
|
||||
engines: {node: '>= 6'}
|
||||
dev: true
|
||||
|
||||
/commander/9.3.0:
|
||||
resolution: {integrity: sha512-hv95iU5uXPbK83mjrJKuZyFM/LBAoCV/XhVGkS5Je6tl7sxr6A0ITMw5WoRV46/UaJ46Nllm3Xt7IaJhXTIkzw==}
|
||||
/commander/9.4.1:
|
||||
resolution: {integrity: sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==}
|
||||
engines: {node: ^12.20.0 || >=14}
|
||||
dev: true
|
||||
|
||||
/commondir/1.0.1:
|
||||
resolution: {integrity: sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==}
|
||||
@@ -4814,7 +4815,7 @@ packages:
|
||||
dependencies:
|
||||
cli-truncate: 3.1.0
|
||||
colorette: 2.0.19
|
||||
commander: 9.3.0
|
||||
commander: 9.4.1
|
||||
debug: 4.3.4
|
||||
execa: 6.1.0
|
||||
lilconfig: 2.0.5
|
||||
|
||||
@@ -1,10 +1,57 @@
|
||||
/* eslint-disable @typescript-eslint/consistent-type-imports */
|
||||
import type { AxiosRequestConfig } from 'axios'
|
||||
import cluster from 'cluster'
|
||||
import { argv } from 'zx-cjs'
|
||||
import { program } from 'commander'
|
||||
import { readFileSync } from 'fs'
|
||||
import { load as yamlLoad } from 'js-yaml'
|
||||
import path from 'path'
|
||||
|
||||
import { cwd, isDev, isMainCluster, isTest } from './global/env.global'
|
||||
|
||||
const commander = program
|
||||
.option('-p, --port <number>', 'server port')
|
||||
.option('--demo', 'enable demo mode')
|
||||
.option(
|
||||
'--allowed_origins <string>',
|
||||
'allowed origins, e.g. innei.ren,*.innei.ren',
|
||||
)
|
||||
.option('-c, --config <path>', 'load yaml config from file')
|
||||
|
||||
// db
|
||||
.option('--collection_name <string>', 'mongodb collection name')
|
||||
.option('--db_host <string>', 'mongodb database host')
|
||||
.option('--db_port <number>', 'mongodb database port')
|
||||
.option('--db_user <string>', 'mongodb database user')
|
||||
.option('--db_password <string>', 'mongodb database password')
|
||||
|
||||
// redis
|
||||
.option('--redis_host <string>', 'redis host')
|
||||
.option('--redis_port <number>', 'redis port')
|
||||
.option('--redis_password <string>', 'redis password')
|
||||
.option('--disable_cache', 'disable redis cache')
|
||||
|
||||
// jwt
|
||||
.option('--jwt_secret <string>', 'custom jwt secret')
|
||||
.option('--jwt_expire <number>', 'custom jwt expire time(d)')
|
||||
|
||||
// cluster
|
||||
.option('--cluster', 'enable cluster mode')
|
||||
.option('--cluster_workers <number>', 'cluster worker count')
|
||||
|
||||
// debug
|
||||
.option('--http_request_verbose', 'enable http request verbose')
|
||||
|
||||
commander.parse()
|
||||
|
||||
const argv = commander.opts()
|
||||
|
||||
if (argv.config) {
|
||||
const config = yamlLoad(
|
||||
readFileSync(path.join(String(process.cwd()), argv.config), 'utf8'),
|
||||
)
|
||||
Object.assign(argv, config)
|
||||
}
|
||||
|
||||
export const PORT = argv.port || process.env.PORT || 2333
|
||||
export const API_VERSION = 2
|
||||
|
||||
@@ -36,8 +83,8 @@ export const MONGO_DB = {
|
||||
user: argv.db_user || '',
|
||||
password: argv.db_password || '',
|
||||
get uri() {
|
||||
let userPassword =
|
||||
this.user && this.password ? this.user + ':' + this.password + '@' : ''
|
||||
const userPassword =
|
||||
this.user && this.password ? `${this.user}:${this.password}@` : ''
|
||||
return `mongodb://${userPassword}${this.host}:${this.port}/${
|
||||
isTest ? 'mx-space_unitest' : this.dbName
|
||||
}`
|
||||
|
||||
Reference in New Issue
Block a user