chore: 增加在运行时通过环境变量修改参数的方法

pull/81/head v0.0.8
moonrailgun 3 years ago
parent ac694080b5
commit 0188cf034c

@ -12,4 +12,4 @@ EXPOSE 11011
RUN cd web && pnpm build
CMD http-server-spa ./web/dist index.html 11011
CMD node scripts/sync-config-from-env.js && http-server-spa ./web/dist index.html 11011

@ -0,0 +1,3 @@
{
"serviceUrl": null
}

@ -79,6 +79,10 @@ const plugins: Configuration['plugins'] = [
from: path.resolve(ROOT_PATH, './assets/pwa.webmanifest'),
to: 'pwa.webmanifest',
},
{
from: path.resolve(ROOT_PATH, './assets/config.json'),
to: 'config.json',
},
{
from: path.resolve(ROOT_PATH, './assets/images/logo/'),
to: 'images/logo/',

@ -0,0 +1,20 @@
/**
* 用于 dockerfile 启动命令
* 将环境变量的信息同步到 dist/config.json 文件中
*/
const fs = require('fs-extra');
const path = require('path');
const envConfig = require('../assets/config.json'); // 获取默认配置
if (process.env.SERVICE_URL) {
envConfig.serviceUrl = process.env.SERVICE_URL;
}
if (envConfig) {
fs.writeJsonSync(path.resolve(__dirname, '../dist/config.json'), envConfig, {
spaces: 2,
});
console.log('从环境变量更新config完毕:', envConfig);
}

@ -11,9 +11,12 @@ import {
showErrorToasts,
t,
fetchGlobalConfig,
request,
isValidStr,
} from 'tailchat-shared';
import { getPopupContainer } from './utils/dom-helper';
import { getUserJWT } from './utils/jwt-helper';
import _get from 'lodash/get';
const webStorage = buildStorage(window.localStorage);
setStorage(() => webStorage);
@ -54,6 +57,20 @@ setGlobalLoading((text) => {
return hide;
});
/**
*
*/
request
.get('/config.json', {
baseURL: '',
})
.then(({ data: config }) => {
if (isValidStr(_get(config, 'serviceUrl'))) {
setServiceUrl(() => _get(config, 'serviceUrl'));
}
})
.catch(() => {});
/**
*
*/

Loading…
Cancel
Save