Skip to content

Commit

Permalink
feat: opt config load and add husky (#12)
Browse files Browse the repository at this point in the history
* feat: opt config load and add husky

* fix: alter code comment
  • Loading branch information
lanz0519 authored Mar 21, 2024
1 parent b640358 commit c66bce8
Show file tree
Hide file tree
Showing 15 changed files with 505 additions and 201 deletions.
4 changes: 3 additions & 1 deletion .env.default
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
NODE_ENV=

REDIS_HOST=
# parseInt(process.env.REDIS_PORT, 10)
Expand All @@ -20,3 +19,6 @@ REDIS_HOST=

LOKALISE_TOKEN=
LOKALISE_PROJECT_ID=

PORT=
GLOBAL_PREFIX=
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jest-stare/
.dist/
_dist/
.vscode
.env
.history
.yarn/install-state.gz

.yarn/cache
.yarn/cache
.config*.json
7 changes: 7 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

echo "Running lint-staged..."
npx lint-staged
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
"@midwayjs/redis": "3",
"@midwayjs/validate": "3",
"@typegoose/typegoose": "^11.7.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"dotenv": "^16.3.1",
"glob": "^10.3.10",
"lint-staged": "^15.2.2",
"lodash": "^4.17.21",
"mongoose": "^7.0.0",
"mongoose-delete": "^1.0.1",
Expand All @@ -39,6 +38,7 @@
"@types/node": "14",
"cross-env": "^7.0.3",
"eslint-plugin-import": "^2.29.0",
"husky": "^9.0.11",
"jest": "^29.7.0",
"lokalise-client": "^1.1.9",
"mwts": "^1.3.0",
Expand All @@ -58,8 +58,8 @@
"lint:fix": "mwts fix",
"ci": "npm run cov",
"build": "midway-bin build -c",
"prepare-env": "yarn dlx ts-node ./src/scripts/prepare-env.ts",
"fetch:locale": "rimraf src/locales/*.json & npx lokalise-client fetch"
"fetch:locale": "rimraf src/locales/*.json & npx lokalise-client fetch",
"prepare": "husky"
},
"midway-bin-clean": [
".vscode/.tsbuildinfo",
Expand All @@ -71,5 +71,10 @@
},
"author": "anonymous",
"license": "MIT",
"packageManager": "yarn@4.0.2"
"packageManager": "yarn@4.0.2",
"lint-staged": {
"*{.ts,.tsx}": [
"yarn run lint:fix"
]
}
}
82 changes: 0 additions & 82 deletions src/config/base.ts

This file was deleted.

55 changes: 55 additions & 0 deletions src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-disable no-process-env */

import localeConfig from '../locales';

const getRedisConfig = (db = 0, options: Record<string, unknown> = {}) => ({
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT, 10),
password: process.env.REDIS_PWD,
db,
...options,
});

/*
default中的配置项,会被config.xxxx.ts中相同配置项覆盖
*/
export default {
koa: {
port: Number(process.env.PORT),
globalPrefix: process.env.GLOBAL_PREFIX,
},
midwayLogger: {
default: {
enableFile: false,
enableConsole: false,
},
},
redis: getRedisConfig(parseInt(process.env.REDIS_DB, 10)),
mongoose: {
dataSource: {
default: {
uri: process.env.MONGODB_URI,
options: {
user: process.env.MONGODB_USER,
pass: process.env.MONGODB_PASSWORD,
},
},
},
},
NODE_ENV: process.env.NODE_ENV,
aws: {
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
awsSecretKey: process.env.AWS_SECRET_KEY,
awsRegion: process.env.AWS_REGION,
},
keys: process.env.COOKIE_SIGN_KEY,
i18n: {
defaultLocale: 'en-us',
localeTable: localeConfig,
fallbacks: {
'*': 'en-us',
},
writeCookie: false,
resolver: false,
},
};
20 changes: 1 addition & 19 deletions src/config/config.local.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/* eslint-disable no-console */
/* eslint-disable no-process-env */

import { MidwayConfig } from '@midwayjs/core';
import { merge } from 'lodash';

import { baseConfig } from './base';

export default (): MidwayConfig => {
return merge(baseConfig, {
koa: {
port: 7001,
},
midwayLogger: {
default: {
console: false,
file: false,
},
},
}) as MidwayConfig;
};
export default {} as MidwayConfig;
6 changes: 2 additions & 4 deletions src/config/config.production.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { baseConfig } from './base';
import { MidwayConfig } from '@midwayjs/core';

import type { MidwayConfig } from '@midwayjs/core';

export default baseConfig as MidwayConfig;
export default {} as MidwayConfig;
6 changes: 2 additions & 4 deletions src/config/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { baseConfig } from './base';
import { MidwayConfig } from '@midwayjs/core';

import type { MidwayConfig } from '@midwayjs/core';

export default baseConfig as MidwayConfig;
export default {} as MidwayConfig;
14 changes: 13 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './init';

import { hostname } from 'os';
import path, { join } from 'path';

Expand All @@ -21,6 +23,8 @@ import { sync } from 'read-pkg';
import { DefaultErrorFilter } from './filter/default.filter';
import { LocaleMiddleware } from './middleware/locale.middleware';
import { ResponseWrapperMiddleware } from './middleware/response-wrapper.middleware';
import { RUNTIME_ENV_MAP, ServerEnv } from './types/config/config.dto';
import { validateBy } from './utils/common';
import { CloudwatchTransport } from './utils/logger';
import { registerModel } from './utils/register-model';
@Configuration({
Expand Down Expand Up @@ -57,11 +61,19 @@ export class MainConfiguration {
@Inject()
dataSourceManager: mongoose.MongooseDataSourceManager;

async onConfigLoad() {
const config = this.app.getConfig();

return validateBy(config, ServerEnv, {
allowUnknown: true,
});
}

async onReady(applicationContext: IMidwayContainer) {
this.app.useMiddleware([LocaleMiddleware, ResponseWrapperMiddleware]);
this.app.useFilter([DefaultErrorFilter]);

if (this.envConfig === 'production') {
if (this.envConfig === RUNTIME_ENV_MAP.PRODUCTION) {
const cloudwatchTransport = new CloudwatchTransport({
app: this.app.getProjectName(),
hostname: hostname(),
Expand Down
37 changes: 37 additions & 0 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable no-process-env */
import { join, resolve } from 'path';

import * as dotenv from 'dotenv';

export const ServerSecretsPath = resolve(
'/',
'mnt',
'secrets',
`onekey-eks-dashboard-${process.env.NODE_ENV}.json`
);
export const ServerConfigPath = resolve('/', 'mnt', 'config', 'config.json');

function loadJsonConfigFile(file: string, errorMessage: string) {
try {
dotenv.populate(process.env, require(file));
} catch (error) {
console.log(errorMessage, file);
}
}

/*
加载线上配置文件
*/
loadJsonConfigFile(ServerConfigPath, 'No config found path:');
loadJsonConfigFile(ServerSecretsPath, 'No secrets found path:');
loadJsonConfigFile(
join(__dirname, '..', '.config.json'),
'No local json config found'
);

/*
加载本地配置文件,注意.config.json和.env只需要一个即可,两个都存在的话,.config.json会覆盖.env
*/
dotenv.config({
path: join(__dirname, '..', '.env'),
});
Loading

0 comments on commit c66bce8

Please sign in to comment.