diff --git a/docker-compose.yaml b/docker-compose.yaml index d3fcf301..00e22879 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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: diff --git a/server/.env.example b/server/.env.example new file mode 100644 index 00000000..4d51295f --- /dev/null +++ b/server/.env.example @@ -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 \ No newline at end of file diff --git a/server/package.json b/server/package.json index d0b5c031..50b166ee 100644 --- a/server/package.json +++ b/server/package.json @@ -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", diff --git a/server/scripts/run-local.ts b/server/scripts/run-local.ts index 7e954d55..3f715176 100644 --- a/server/scripts/run-local.ts +++ b/server/scripts/run-local.ts @@ -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' }); diff --git a/server/src/apps/security/config/index.ts b/server/src/apps/security/config/index.ts index 7c235f48..c06fa407 100644 --- a/server/src/apps/security/config/index.ts +++ b/server/src/apps/security/config/index.ts @@ -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, + }; } \ No newline at end of file diff --git a/server/src/apps/surveyManage/config/index.ts b/server/src/apps/surveyManage/config/index.ts index 41f62e8e..39c38c8d 100644 --- a/server/src/apps/surveyManage/config/index.ts +++ b/server/src/apps/surveyManage/config/index.ts @@ -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, + }; } \ No newline at end of file diff --git a/server/src/apps/surveyPublish/config/index.ts b/server/src/apps/surveyPublish/config/index.ts index 6353423e..1ae6c0a9 100644 --- a/server/src/apps/surveyPublish/config/index.ts +++ b/server/src/apps/surveyPublish/config/index.ts @@ -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, + }; } \ No newline at end of file diff --git a/server/src/apps/user/config/index.ts b/server/src/apps/user/config/index.ts index 5401164f..040c240b 100644 --- a/server/src/apps/user/config/index.ts +++ b/server/src/apps/user/config/index.ts @@ -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, + }; } \ No newline at end of file diff --git a/server/src/config/index.ts b/server/src/config/index.ts new file mode 100644 index 00000000..ccb267c8 --- /dev/null +++ b/server/src/config/index.ts @@ -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, +} diff --git a/server/src/index.ts b/server/src/index.ts index 0331d2cc..a3c505ca 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,3 +1,4 @@ +import 'dotenv/config'; import * as os from 'os'; import * as Koa from 'koa'; import * as KoaBodyparser from 'koa-bodyparser'; diff --git a/server/src/types/env.d.ts b/server/src/types/env.d.ts new file mode 100644 index 00000000..0d1a160d --- /dev/null +++ b/server/src/types/env.d.ts @@ -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; + } + } +}