feat: config merge

pull/21/merge
xiaojunnuo 2023-06-26 12:26:59 +08:00
parent 99522fb49a
commit fdc25dc0d7
11 changed files with 58 additions and 20 deletions

View File

@ -1 +0,0 @@
CERTD_AUTH_JWT_KEY=certd666

View File

@ -0,0 +1,2 @@
koa:
port: 7001

View File

@ -0,0 +1,2 @@
koa:
port: 7001

View File

@ -0,0 +1,2 @@
koa:
port: 7001

View File

@ -7,9 +7,6 @@ COPY . .
#RUN npm install --registry=https://registry.npmmirror.com
RUN npm install -g cnpm
RUN cnpm install
RUN npm run build:preview
RUN npm run build
# 如果端口更换,这边可以更新一下
EXPOSE 7001
CMD ["npm", "run", "online:preview"]
CMD ["npm", "run", "online"]

View File

@ -47,6 +47,7 @@
"dayjs": "^1.11.7",
"dotenv": "^16.0.3",
"glob": "^7.2.3",
"js-yaml": "^4.1.0",
"jsonwebtoken": "^8.5.1",
"kubernetes-client": "^9.0.0",
"lodash": "^4.17.21",

View File

@ -4,13 +4,13 @@ import { FlywayHistory } from 'midway-flyway-js/dist/entity';
import { MidwayConfig } from '@midwayjs/core';
import { UserEntity } from '../modules/authority/entity/user';
import { PipelineEntity } from '../modules/pipeline/entity/pipeline';
import * as dotenv from 'dotenv';
//import { logger } from '../utils/logger';
// load .env file in process.cwd
dotenv.config();
export default {
import _ from 'lodash';
import { load } from './loader';
const development = {
// use for cookie sign key, should change to your own and keep security
keys: process.env.CERTD_AUTH_JWT_KEY,
keys: 'certd666',
koa: {
port: 7001,
},
@ -68,7 +68,7 @@ export default {
biz: {
jwt: {
secret: process.env.CERTD_AUTH_JWT_KEYS,
secret: 'certd666',
expire: 7 * 24 * 60, //单位秒
},
auth: {
@ -76,3 +76,5 @@ export default {
},
},
} as MidwayConfig;
_.merge(development, load('development'));
export default development;

View File

@ -1,6 +1,8 @@
import { MidwayConfig } from '@midwayjs/core';
import { load } from './loader';
import _ from 'lodash';
export default {
const preview = {
/**
*
*/
@ -8,3 +10,7 @@ export default {
enabled: true,
},
} as MidwayConfig;
_.merge(preview, load('preview'));
export default preview;

View File

@ -1,10 +1,15 @@
import { MidwayConfig } from '@midwayjs/core';
export default {
import { load } from './loader';
import _ from 'lodash';
const production = {
/**
*
*/
preview: {
enabled: true,
enabled: false,
},
} as MidwayConfig;
_.merge(production, load('production'));
export default production;

View File

@ -0,0 +1,26 @@
import path from 'path';
import _ from 'lodash';
const yaml = require('js-yaml');
const fs = require('fs');
function parseEnv() {
const config = {};
for (const key in process.env) {
let keyName = key;
if (!keyName.startsWith('certd_')) {
continue;
}
keyName = keyName.replace('certd_', '');
const configKey = keyName.replace('_', '.');
_.set(config, configKey, process.env[key]);
}
return config;
}
export function load(env = '') {
// Get document, or throw exception on error
const yamlPath = path.join(process.cwd(), `.env.${env}.yaml`);
const doc = yaml.load(fs.readFileSync(yamlPath, 'utf8'));
_.merge(doc, parseEnv());
return doc;
}

View File

@ -1,9 +1,5 @@
import { Provide } from '@midwayjs/decorator';
import {
IWebMiddleware,
IMidwayKoaContext,
NextFunction,
} from '@midwayjs/koa';
import { IWebMiddleware, IMidwayKoaContext, NextFunction } from '@midwayjs/koa';
import { logger } from '../utils/logger';
@Provide()