@myassis/gateway 1.0.87 → 1.0.88
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/index.js +5 -2
- package/dist/config/index.js +28 -3
- package/dist/services/ServiceManager.js +9 -3
- package/package.json +1 -1
package/dist/api/index.js
CHANGED
|
@@ -16,6 +16,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
16
16
|
exports.settingsApi = exports.tasksApi = exports.modelsApi = exports.skillHubApi = exports.skillsApi = exports.authApi = exports.ApiError = exports.getServerBaseUrl = exports.getRequestToken = exports.runWithToken = void 0;
|
|
17
17
|
const crypto_1 = __importDefault(require("crypto"));
|
|
18
18
|
const async_hooks_1 = require("async_hooks");
|
|
19
|
+
const index_js_1 = require("../config/index.js");
|
|
19
20
|
// 多用户模式请求上下文:存储当前请求的 token
|
|
20
21
|
const requestContext = new async_hooks_1.AsyncLocalStorage();
|
|
21
22
|
function runWithToken(token, fn) {
|
|
@@ -32,8 +33,10 @@ function getServerBaseUrl() {
|
|
|
32
33
|
return SERVER_BASE_URL;
|
|
33
34
|
}
|
|
34
35
|
exports.getServerBaseUrl = getServerBaseUrl;
|
|
35
|
-
// Server
|
|
36
|
-
|
|
36
|
+
// Server 服务地址。必须走 appConfig:这里曾经自己又写了一个
|
|
37
|
+
// `|| 'http://localhost:3000'` 的默认值,与 config 里的 9091 分裂成两个事实,
|
|
38
|
+
// 结果是同一个网关进程里业务 API 和其他模块连到不同地址。
|
|
39
|
+
const SERVER_BASE_URL = index_js_1.appConfig.serverBaseUrl;
|
|
37
40
|
// 签名密钥(用于请求签名)
|
|
38
41
|
const SIGN_KEY = process.env.API_SIGN_KEY || 'gateway-secret-key';
|
|
39
42
|
// API 响应错误类
|
package/dist/config/index.js
CHANGED
|
@@ -65,19 +65,31 @@ const envPath = getEnvPath();
|
|
|
65
65
|
if (envPath) {
|
|
66
66
|
(0, dotenv_1.config)({ path: envPath });
|
|
67
67
|
logger.info(`已加载环境变量文件: ${envPath}`);
|
|
68
|
+
logger.info(`当前工作目录: ${process.cwd()}`);
|
|
68
69
|
}
|
|
69
70
|
else {
|
|
70
|
-
logger.info(
|
|
71
|
+
logger.info(`未找到 .env 文件(工作目录: ${process.cwd()}),使用系统环境变量`);
|
|
71
72
|
}
|
|
72
73
|
// pkg 打包后自动设为 production
|
|
73
74
|
const isPackaged = !!process.pkg;
|
|
74
75
|
const defaultEnv = isPackaged ? 'production' : 'development';
|
|
76
|
+
/**
|
|
77
|
+
* Server 地址的环境默认值。
|
|
78
|
+
*
|
|
79
|
+
* 生产一律连云端域名,开发连本机。之前无论哪种环境都写死回落
|
|
80
|
+
* localhost,于是安装版一旦没注入 SERVER_BASE_URL 就默默去连本机,
|
|
81
|
+
* 表现为登录 500 「网络请求失败」—— 一个没有任何提示的错配。
|
|
82
|
+
*/
|
|
83
|
+
const PROD_SERVER_BASE_URL = 'https://api.my-assis.com';
|
|
84
|
+
const DEV_SERVER_BASE_URL = 'http://localhost:9091';
|
|
85
|
+
const resolvedNodeEnv = process.env.NODE_ENV || defaultEnv;
|
|
86
|
+
const defaultServerBaseUrl = resolvedNodeEnv === 'production' ? PROD_SERVER_BASE_URL : DEV_SERVER_BASE_URL;
|
|
75
87
|
// 环境变量配置
|
|
76
88
|
exports.appConfig = {
|
|
77
|
-
serverBaseUrl: process.env.SERVER_BASE_URL || '
|
|
89
|
+
serverBaseUrl: (process.env.SERVER_BASE_URL || defaultServerBaseUrl).replace(/\/+$/, ''),
|
|
78
90
|
port: parseInt(process.env.PORT || '3001', 10),
|
|
79
91
|
clientUrl: process.env.CLIENT_URL || 'http://localhost:3000',
|
|
80
|
-
nodeEnv:
|
|
92
|
+
nodeEnv: resolvedNodeEnv,
|
|
81
93
|
messageKeep: 4,
|
|
82
94
|
/** 摘要后保留的原始历史消息条数 */
|
|
83
95
|
historyKeep: parseInt(process.env.HISTORY_KEEP || '4', 10),
|
|
@@ -99,4 +111,17 @@ exports.appConfig = {
|
|
|
99
111
|
summaryTriggerChars: parseInt(process.env.SUMMARY_TRIGGER_CHARS || '60000', 10),
|
|
100
112
|
appName: '我的助手'
|
|
101
113
|
};
|
|
114
|
+
/**
|
|
115
|
+
* 启动时把实际生效的关键配置打出来。
|
|
116
|
+
*
|
|
117
|
+
* 之前只记了「已加载环境变量文件」,却不说最终跑的是哪个地址。
|
|
118
|
+
* 结果是生产服务误读开发 .env、连到局域网 IP 时,日志里毫无线索,
|
|
119
|
+
* 只能从一句登录 500「网络请求失败」倒推。
|
|
120
|
+
*
|
|
121
|
+
* 标注来源(env / 默认值)是故意的:「地址对不对」和
|
|
122
|
+
* 「地址是谁给的」是两个问题,后者才能区分「.env 没读到」与「.env 写错了」。
|
|
123
|
+
*/
|
|
124
|
+
logger.info(`生效配置: env=${exports.appConfig.nodeEnv}, port=${exports.appConfig.port}, ` +
|
|
125
|
+
`serverBaseUrl=${exports.appConfig.serverBaseUrl}` +
|
|
126
|
+
` (${process.env.SERVER_BASE_URL ? '来自 SERVER_BASE_URL' : '环境默认值'})`);
|
|
102
127
|
exports.default = exports.appConfig;
|
|
@@ -108,14 +108,20 @@ function getGatewayArgs() {
|
|
|
108
108
|
/**
|
|
109
109
|
* Gateway 运行的工作目录。
|
|
110
110
|
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
111
|
+
* - 打包为 exe:exe 所在目录;
|
|
112
|
+
* - node 启动:入口脚本(dist/index.js)所在目录,即 dist。
|
|
113
|
+
*
|
|
114
|
+
* 两者都是「产物所在目录」,于是 getEnvPath() 的第一条规则
|
|
115
|
+
* `process.cwd()/.env` 恰好命中随产物发布的 dist/.env(生产配置)。
|
|
116
|
+
*
|
|
117
|
+
* 不能再向上退一层到「包根」:包根下躺着开发用的 .env,
|
|
118
|
+
* 生产服务会因此读到 SERVER_BASE_URL=局域网 IP,且毫无提示(见 f126be961)。
|
|
113
119
|
*/
|
|
114
120
|
function getGatewayWorkDir() {
|
|
115
121
|
if (isPackagedExe())
|
|
116
122
|
return path_1.default.dirname(process.execPath);
|
|
117
123
|
const script = getServiceScript();
|
|
118
|
-
return script ? path_1.default.dirname(
|
|
124
|
+
return script ? path_1.default.dirname(script) : path_1.default.dirname(process.execPath);
|
|
119
125
|
}
|
|
120
126
|
/**
|
|
121
127
|
* 检查 Gateway 进程是否在运行
|