From ae459eabfea997dd7d746406f39a37bf89e747ff Mon Sep 17 00:00:00 2001 From: xion Date: Tue, 20 May 2025 00:29:52 +0800 Subject: [PATCH] fix: update for init --- .env.example | 1 - package.json | 4 ++-- src/module/config.ts | 10 ++++++++++ src/module/get-user-app.ts | 5 ++++- src/module/index.ts | 9 ++++----- src/module/query/get-router.ts | 1 + src/module/redis/get-app-status.ts | 4 ++-- src/module/redis/redis.ts | 8 ++++---- 8 files changed, 27 insertions(+), 15 deletions(-) diff --git a/.env.example b/.env.example index b9e1c55..23a1c62 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,6 @@ PROXY_PORT=3005 PROXY_DOMAIN=localhost PROXY_RESOURCES=http://localhost:9000/resources PROXY_ALLOWED_ORIGINS=localhost,xiongxiao.me,zxj.im -PROXY_HOME=/ai/chat # 后台代理 API_HOST=http://localhost:4005 diff --git a/package.json b/package.json index b31a485..df88da7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "page-proxy", - "version": "0.0.5", + "version": "0.0.6", "description": "", "main": "index.js", "type": "module", @@ -18,7 +18,7 @@ ], "scripts": { "watch": "rollup -c --watch", - "dev": "cross-env NODE_ENV=development nodemon --ignore upload --exec tsx src/index.ts", + "dev": "cross-env NODE_ENV=development nodemon --ignore proxy-upload --exec tsx src/index.ts", "dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ", "build": "rimraf dist && rollup -c", "start": "pm2 start dist/app.mjs --name page-proxy", diff --git a/src/module/config.ts b/src/module/config.ts index 902b84e..d36d17c 100644 --- a/src/module/config.ts +++ b/src/module/config.ts @@ -45,6 +45,11 @@ type ConfigType = { */ websiteId: string; }; + redis?: { + host: string; + port: number; + password?: string; + }; }; // export const config = useConfig(); @@ -67,6 +72,11 @@ export const config: ConfigType = { resources: envConfig.PROXY_RESOURCES, allowedOrigin: (envConfig.PROXY_ALLOWED_ORIGINS as string)?.split(',') || [], }, + redis: { + host: envConfig.REDIS_HOST, + port: envConfig.REDIS_PORT, + password: envConfig.REDIS_PASSWORD, + }, stat: { websiteId: envConfig.DATA_WEBSITE_ID, }, diff --git a/src/module/get-user-app.ts b/src/module/get-user-app.ts index 5a5ae1c..9049396 100644 --- a/src/module/get-user-app.ts +++ b/src/module/get-user-app.ts @@ -10,6 +10,7 @@ import { fetchApp, fetchDomain, fetchTest } from './query/get-router.ts'; import { getAppLoadStatus, setAppLoadStatus } from './redis/get-app-status.ts'; import { minioResources } from './minio.ts'; import { downloadFileFromMinio } from './proxy/http-proxy.ts'; +import { logger } from './logger.ts'; const pipelineAsync = promisify(pipeline); @@ -194,6 +195,7 @@ export class UserApp { } const loadStatus = await getAppLoadStatus(user, app); + logger.debug('loadStatus', loadStatus); if (loadStatus.status === 'loading') { // 其他情况,error或者running都可以重新加载 return { @@ -212,7 +214,7 @@ export class UserApp { return { code: 500, message: 'app status is not running' }; } // console.log('fetchData', JSON.stringify(fetchData.data.files, null, 2)); - + // const getFileSize this.setLoaded('loading', 'loading'); const loadProxy = async () => { const value = fetchData; @@ -272,6 +274,7 @@ export class UserApp { await redis.hset('user:app:set:' + app + ':' + user, data); this.setLoaded('running', 'loaded'); }; + logger.debug('loadFilesFn', fetchData.proxy); try { if (fetchData.proxy === true) { await loadProxy(); diff --git a/src/module/index.ts b/src/module/index.ts index f5127bd..49aa310 100644 --- a/src/module/index.ts +++ b/src/module/index.ts @@ -12,8 +12,8 @@ import { UserPermission } from '@kevisual/permission'; import { getLoginUser } from '@/middleware/auth.ts'; import { rediretHome } from './user-home/index.ts'; import { aiProxy } from './proxy/ai-proxy.ts'; -const api = config?.api || { host: 'kevisual.xiongxiao.me', path: '/api/router' }; -const domain = config?.proxy?.domain || 'kevisual.xiongxiao.me'; +import { logger } from './logger.ts'; +const domain = config?.proxy?.domain; const allowedOrigins = config?.proxy?.allowedOrigin || []; const noProxyUrl = ['/', '/favicon.ico']; @@ -56,7 +56,6 @@ const checkNotAuthPath = (user, app) => { export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => { const querySearch = new URL(req.url, `http://${req.headers.host}`).searchParams; const password = querySearch.get('p'); - const loginUser = await getLoginUser(req); if (req.url === '/favicon.ico') { res.writeHead(200, { 'Content-Type': 'image/x-icon' }); res.end('proxy no favicon.ico\n'); @@ -153,7 +152,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR return res.end(); } // 验证域名 - if (dns.hostName !== domain) { + if (domain && dns.hostName !== domain) { // redis获取域名对应的用户和应用 domainApp = true; const data = await UserApp.getDomainApp(dns.hostName); @@ -235,7 +234,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR const userApp = new UserApp({ user, app }); let isExist = await userApp.getExist(); - + logger.debug('userApp', userApp, isExist); if (!isExist) { try { const { code, loading, message } = await userApp.setCacheData(); diff --git a/src/module/query/get-router.ts b/src/module/query/get-router.ts index 6709796..6118652 100644 --- a/src/module/query/get-router.ts +++ b/src/module/query/get-router.ts @@ -1,4 +1,5 @@ import { config } from '../config.ts'; +import { logger } from '../logger.ts'; const api = config?.api || { host: 'https://kevisual.cn', path: '/api/router' }; const apiPath = api.path || '/api/router'; diff --git a/src/module/redis/get-app-status.ts b/src/module/redis/get-app-status.ts index 04cbe62..83f01cb 100644 --- a/src/module/redis/get-app-status.ts +++ b/src/module/redis/get-app-status.ts @@ -23,8 +23,8 @@ export const getAppLoadStatus = async (user: string, app: string): Promise { +export const setAppLoadStatus = async (user: string, app: string, status: AppLoadStatus, exp = 3 * 60) => { const key = 'user:app:status:' + app + ':' + user; const value = JSON.stringify(status); - await redis.set(key, value); + await redis.set(key, value, 'EX', exp); // 5分钟过期 }; diff --git a/src/module/redis/redis.ts b/src/module/redis/redis.ts index 893e937..e3b0bbb 100644 --- a/src/module/redis/redis.ts +++ b/src/module/redis/redis.ts @@ -1,11 +1,11 @@ import { Redis } from 'ioredis'; import { useContextKey } from '@kevisual/use-config/context'; console.log(process.env.REDIS_HOST); - +import { config } from '../config.ts'; const redisConfig = { - host: 'localhost', // Redis 服务器的主机名或 IP 地址 - port: 6379, // Redis 服务器的端口号 - // password: 'your_password', // Redis 的密码 (如果有) + host: config?.redis?.host || 'localhost', // Redis 服务器的主机名或 IP 地址 + port: config?.redis?.port || 6379, // Redis 服务器的端口号 + password: config?.redis?.password, // Redis 的密码 (如果有) }; const init = () => { return new Redis({