feat: 调整服务端环境变量导入机制 (#43)
1、为环境变量 process.env 增加 TypeScript 类型检测与感知 2、增加 dotenv & @types/node 处理支持 .env文件环境变量导入机制
This commit is contained in:
parent
f851b0df10
commit
5fff688612
@ -21,10 +21,6 @@ services:
|
||||
restart: always
|
||||
ports:
|
||||
- "8080:3000" # API端口
|
||||
environment:
|
||||
xiaojuSurveyMongoUrl: mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@xiaoju-survey-mongo:27017 # docker-compose 会根据容器名称自动处理
|
||||
xiaojuSurveyJwtSecret: surveyEngineJwtSecret
|
||||
xiaojuSurveyJwtExpiresIn: 8h
|
||||
links:
|
||||
- mongo:mongo
|
||||
depends_on:
|
||||
|
16
server/.env.example
Normal file
16
server/.env.example
Normal file
@ -0,0 +1,16 @@
|
||||
# mongo
|
||||
XIAOJU_SURVEY_MONGO_URL=mongodb://localhost:27017
|
||||
XIAOJU_SURVER_MONGO_DBNAME=xiaojuSurvey
|
||||
|
||||
# session
|
||||
# 8 * 3600 * 1000
|
||||
XIAOJU_SURVEY_SESSION_EXPIRE_TIME=28800000
|
||||
|
||||
# encrypt
|
||||
XIAOJU_SURVEY_ENCRYPT_TYPE=aes
|
||||
XIAOJU_SURVEY_ENCRYPT_SECRET_KEY=dataAesEncryptSecretKey
|
||||
XIAOJU_SURVEY_ENCRYPT_TYPE_LEN=10
|
||||
|
||||
# jwt
|
||||
XIAOJU_SURVEY_JWT_SECRET=xiaojuSurveyJwtSecret
|
||||
XIAOJU_SURVEY_JWT_EXPIRES_IN=8h
|
@ -19,6 +19,7 @@
|
||||
"@types/koa-bodyparser": "^4.3.10",
|
||||
"@types/koa-router": "^7.4.4",
|
||||
"@types/koa-static": "^4.0.4",
|
||||
"@types/node": "^20.10.8",
|
||||
"@typescript-eslint/eslint-plugin": "^6.15.0",
|
||||
"@typescript-eslint/parser": "^6.15.0",
|
||||
"cross-env": "^7.0.3",
|
||||
@ -32,6 +33,7 @@
|
||||
"dependencies": {
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
"crypto-js": "^4.2.0",
|
||||
"dotenv": "^16.3.1",
|
||||
"glob": "^10.3.10",
|
||||
"joi": "^17.9.2",
|
||||
"jsonwebtoken": "^9.0.1",
|
||||
|
@ -9,7 +9,7 @@ async function startServerAndRunScript() {
|
||||
console.log('MongoDB Memory Server started:', mongoUri);
|
||||
|
||||
// 通过 spawn 运行另一个脚本,并传递 MongoDB 连接 URL 作为环境变量
|
||||
const tsnode = spawn('cross-env', [`xiaojuSurveyMongoUrl="${mongoUri}"`, 'npx', 'ts-node-dev', './src/index.ts'], {
|
||||
const tsnode = spawn('cross-env', [`XIAOJU_SURVEY_MONGO_URL="${mongoUri}"`, 'npx', 'ts-node-dev', './src/index.ts'], {
|
||||
stdio: 'inherit',
|
||||
shell: process.platform === 'win32'
|
||||
});
|
||||
|
@ -1,13 +1,12 @@
|
||||
const config = {
|
||||
mongo: {
|
||||
url: process.env.xiaojuSurveyMongoUrl || 'mongodb://localhost:27017',
|
||||
dbName: 'xiaojuSurvey'
|
||||
},
|
||||
aesEncrypt: {
|
||||
key: process.env.xiaojuSurveyDataAesEncryptSecretKey || 'dataAesEncryptSecretKey'
|
||||
}
|
||||
import { mongo } from '../../../config';
|
||||
|
||||
const aesEncrypt = {
|
||||
key: process.env.XIAOJU_SURVEY_ENCRYPT_SECRET_KEY || 'dataAesEncryptSecretKey',
|
||||
};
|
||||
|
||||
export function getConfig() {
|
||||
return config;
|
||||
return {
|
||||
mongo,
|
||||
aesEncrypt,
|
||||
};
|
||||
}
|
@ -1,10 +1,7 @@
|
||||
const config = {
|
||||
mongo: {
|
||||
url: process.env.xiaojuSurveyMongoUrl || 'mongodb://localhost:27017',
|
||||
dbName: 'xiaojuSurvey',
|
||||
}
|
||||
};
|
||||
import { mongo } from '../../../config';
|
||||
|
||||
export function getConfig() {
|
||||
return config;
|
||||
return {
|
||||
mongo,
|
||||
};
|
||||
}
|
@ -1,17 +1,9 @@
|
||||
const config = {
|
||||
mongo: {
|
||||
url: process.env.xiaojuSurveyMongoUrl || 'mongodb://localhost:27017',
|
||||
dbName: 'xiaojuSurvey',
|
||||
},
|
||||
session: {
|
||||
expireTime: parseInt(process.env.xiaojuSurveySessionExpireTime) || 8 * 3600 * 1000
|
||||
},
|
||||
encrypt: {
|
||||
type: process.env.xiaojuSurveyEncryptType || 'aes',
|
||||
aesCodelength: parseInt(process.env.xiaojuSurveyAesCodelength) || 10 //aes密钥长度
|
||||
}
|
||||
};
|
||||
import { mongo, session, encrypt } from '../../../config';
|
||||
|
||||
export function getConfig() {
|
||||
return config;
|
||||
return {
|
||||
mongo,
|
||||
session,
|
||||
encrypt,
|
||||
};
|
||||
}
|
@ -1,14 +1,8 @@
|
||||
const config = {
|
||||
mongo: {
|
||||
url: process.env.xiaojuSurveyMongoUrl || 'mongodb://localhost:27017',
|
||||
dbName: 'xiaojuSurvey',
|
||||
},
|
||||
jwt: {
|
||||
secret: process.env.xiaojuSurveyJwtSecret || 'xiaojuSurveyJwtSecret',
|
||||
expiresIn: process.env.xiaojuSurveyJwtExpiresIn || '8h',
|
||||
}
|
||||
};
|
||||
import { mongo, jwt } from '../../../config';
|
||||
|
||||
export function getConfig() {
|
||||
return config;
|
||||
return {
|
||||
mongo,
|
||||
jwt,
|
||||
};
|
||||
}
|
26
server/src/config/index.ts
Normal file
26
server/src/config/index.ts
Normal file
@ -0,0 +1,26 @@
|
||||
const mongo = {
|
||||
url: process.env.XIAOJU_SURVEY_MONGO_URL || 'mongodb://localhost:27017',
|
||||
dbName: process.env.XIAOJU_SURVER_MONGO_DBNAME || 'xiaojuSurvey',
|
||||
}
|
||||
|
||||
const session = {
|
||||
expireTime: parseInt(process.env.XIAOJU_SURVEY_JWT_EXPIRES_IN) || 8 * 3600 * 1000
|
||||
}
|
||||
|
||||
const encrypt = {
|
||||
type: process.env.XIAOJU_SURVEY_ENCRYPT_TYPE || 'aes',
|
||||
aesCodelength: parseInt(process.env.XIAOJU_SURVEY_ENCRYPT_TYPE_LEN) || 10 //aes密钥长度
|
||||
}
|
||||
|
||||
const jwt = {
|
||||
secret: process.env.XIAOJU_SURVEY_JWT_SECRET || 'xiaojuSurveyJwtSecret',
|
||||
expiresIn: process.env.XIAOJU_SURVEY_JWT_EXPIRES_IN || '8h'
|
||||
}
|
||||
|
||||
|
||||
export{
|
||||
mongo,
|
||||
session,
|
||||
encrypt,
|
||||
jwt,
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
import 'dotenv/config';
|
||||
import * as os from 'os';
|
||||
import * as Koa from 'koa';
|
||||
import * as KoaBodyparser from 'koa-bodyparser';
|
||||
|
23
server/src/types/env.d.ts
vendored
Normal file
23
server/src/types/env.d.ts
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
export {};
|
||||
|
||||
declare global {
|
||||
namespace NodeJS {
|
||||
interface ProcessEnv {
|
||||
// mongo
|
||||
XIAOJU_SURVEY_MONGO_URL: string;
|
||||
XIAOJU_SURVER_MONGO_DBNAME: string;
|
||||
|
||||
// session
|
||||
XIAOJU_SURVEY_SESSION_EXPIRE_TIME: string;
|
||||
|
||||
// encrypt
|
||||
XIAOJU_SURVEY_ENCRYPT_TYPE: string;
|
||||
XIAOJU_SURVEY_ENCRYPT_SECRET_KEY: string;
|
||||
XIAOJU_SURVEY_ENCRYPT_TYPE_LEN: string;
|
||||
|
||||
// jwt
|
||||
XIAOJU_SURVEY_JWT_SECRET: string;
|
||||
XIAOJU_SURVEY_JWT_EXPIRES_IN: string;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user