fix: update for init

This commit is contained in:
熊潇 2025-05-20 00:29:52 +08:00
parent 46cb3d4e75
commit ae459eabfe
8 changed files with 27 additions and 15 deletions

View File

@ -3,7 +3,6 @@ PROXY_PORT=3005
PROXY_DOMAIN=localhost PROXY_DOMAIN=localhost
PROXY_RESOURCES=http://localhost:9000/resources PROXY_RESOURCES=http://localhost:9000/resources
PROXY_ALLOWED_ORIGINS=localhost,xiongxiao.me,zxj.im PROXY_ALLOWED_ORIGINS=localhost,xiongxiao.me,zxj.im
PROXY_HOME=/ai/chat
# 后台代理 # 后台代理
API_HOST=http://localhost:4005 API_HOST=http://localhost:4005

View File

@ -1,6 +1,6 @@
{ {
"name": "page-proxy", "name": "page-proxy",
"version": "0.0.5", "version": "0.0.6",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"type": "module", "type": "module",
@ -18,7 +18,7 @@
], ],
"scripts": { "scripts": {
"watch": "rollup -c --watch", "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\" ", "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", "build": "rimraf dist && rollup -c",
"start": "pm2 start dist/app.mjs --name page-proxy", "start": "pm2 start dist/app.mjs --name page-proxy",

View File

@ -45,6 +45,11 @@ type ConfigType = {
*/ */
websiteId: string; websiteId: string;
}; };
redis?: {
host: string;
port: number;
password?: string;
};
}; };
// export const config = useConfig(); // export const config = useConfig();
@ -67,6 +72,11 @@ export const config: ConfigType = {
resources: envConfig.PROXY_RESOURCES, resources: envConfig.PROXY_RESOURCES,
allowedOrigin: (envConfig.PROXY_ALLOWED_ORIGINS as string)?.split(',') || [], allowedOrigin: (envConfig.PROXY_ALLOWED_ORIGINS as string)?.split(',') || [],
}, },
redis: {
host: envConfig.REDIS_HOST,
port: envConfig.REDIS_PORT,
password: envConfig.REDIS_PASSWORD,
},
stat: { stat: {
websiteId: envConfig.DATA_WEBSITE_ID, websiteId: envConfig.DATA_WEBSITE_ID,
}, },

View File

@ -10,6 +10,7 @@ import { fetchApp, fetchDomain, fetchTest } from './query/get-router.ts';
import { getAppLoadStatus, setAppLoadStatus } from './redis/get-app-status.ts'; import { getAppLoadStatus, setAppLoadStatus } from './redis/get-app-status.ts';
import { minioResources } from './minio.ts'; import { minioResources } from './minio.ts';
import { downloadFileFromMinio } from './proxy/http-proxy.ts'; import { downloadFileFromMinio } from './proxy/http-proxy.ts';
import { logger } from './logger.ts';
const pipelineAsync = promisify(pipeline); const pipelineAsync = promisify(pipeline);
@ -194,6 +195,7 @@ export class UserApp {
} }
const loadStatus = await getAppLoadStatus(user, app); const loadStatus = await getAppLoadStatus(user, app);
logger.debug('loadStatus', loadStatus);
if (loadStatus.status === 'loading') { if (loadStatus.status === 'loading') {
// 其他情况error或者running都可以重新加载 // 其他情况error或者running都可以重新加载
return { return {
@ -212,7 +214,7 @@ export class UserApp {
return { code: 500, message: 'app status is not running' }; return { code: 500, message: 'app status is not running' };
} }
// console.log('fetchData', JSON.stringify(fetchData.data.files, null, 2)); // console.log('fetchData', JSON.stringify(fetchData.data.files, null, 2));
// const getFileSize
this.setLoaded('loading', 'loading'); this.setLoaded('loading', 'loading');
const loadProxy = async () => { const loadProxy = async () => {
const value = fetchData; const value = fetchData;
@ -272,6 +274,7 @@ export class UserApp {
await redis.hset('user:app:set:' + app + ':' + user, data); await redis.hset('user:app:set:' + app + ':' + user, data);
this.setLoaded('running', 'loaded'); this.setLoaded('running', 'loaded');
}; };
logger.debug('loadFilesFn', fetchData.proxy);
try { try {
if (fetchData.proxy === true) { if (fetchData.proxy === true) {
await loadProxy(); await loadProxy();

View File

@ -12,8 +12,8 @@ import { UserPermission } from '@kevisual/permission';
import { getLoginUser } from '@/middleware/auth.ts'; import { getLoginUser } from '@/middleware/auth.ts';
import { rediretHome } from './user-home/index.ts'; import { rediretHome } from './user-home/index.ts';
import { aiProxy } from './proxy/ai-proxy.ts'; import { aiProxy } from './proxy/ai-proxy.ts';
const api = config?.api || { host: 'kevisual.xiongxiao.me', path: '/api/router' }; import { logger } from './logger.ts';
const domain = config?.proxy?.domain || 'kevisual.xiongxiao.me'; const domain = config?.proxy?.domain;
const allowedOrigins = config?.proxy?.allowedOrigin || []; const allowedOrigins = config?.proxy?.allowedOrigin || [];
const noProxyUrl = ['/', '/favicon.ico']; const noProxyUrl = ['/', '/favicon.ico'];
@ -56,7 +56,6 @@ const checkNotAuthPath = (user, app) => {
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => { export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
const querySearch = new URL(req.url, `http://${req.headers.host}`).searchParams; const querySearch = new URL(req.url, `http://${req.headers.host}`).searchParams;
const password = querySearch.get('p'); const password = querySearch.get('p');
const loginUser = await getLoginUser(req);
if (req.url === '/favicon.ico') { if (req.url === '/favicon.ico') {
res.writeHead(200, { 'Content-Type': 'image/x-icon' }); res.writeHead(200, { 'Content-Type': 'image/x-icon' });
res.end('proxy no favicon.ico\n'); res.end('proxy no favicon.ico\n');
@ -153,7 +152,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
return res.end(); return res.end();
} }
// 验证域名 // 验证域名
if (dns.hostName !== domain) { if (domain && dns.hostName !== domain) {
// redis获取域名对应的用户和应用 // redis获取域名对应的用户和应用
domainApp = true; domainApp = true;
const data = await UserApp.getDomainApp(dns.hostName); 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 }); const userApp = new UserApp({ user, app });
let isExist = await userApp.getExist(); let isExist = await userApp.getExist();
logger.debug('userApp', userApp, isExist);
if (!isExist) { if (!isExist) {
try { try {
const { code, loading, message } = await userApp.setCacheData(); const { code, loading, message } = await userApp.setCacheData();

View File

@ -1,4 +1,5 @@
import { config } from '../config.ts'; import { config } from '../config.ts';
import { logger } from '../logger.ts';
const api = config?.api || { host: 'https://kevisual.cn', path: '/api/router' }; const api = config?.api || { host: 'https://kevisual.cn', path: '/api/router' };
const apiPath = api.path || '/api/router'; const apiPath = api.path || '/api/router';

View File

@ -23,8 +23,8 @@ export const getAppLoadStatus = async (user: string, app: string): Promise<AppLo
}; };
} }
}; };
export const setAppLoadStatus = async (user: string, app: string, status: AppLoadStatus) => { export const setAppLoadStatus = async (user: string, app: string, status: AppLoadStatus, exp = 3 * 60) => {
const key = 'user:app:status:' + app + ':' + user; const key = 'user:app:status:' + app + ':' + user;
const value = JSON.stringify(status); const value = JSON.stringify(status);
await redis.set(key, value); await redis.set(key, value, 'EX', exp); // 5分钟过期
}; };

View File

@ -1,11 +1,11 @@
import { Redis } from 'ioredis'; import { Redis } from 'ioredis';
import { useContextKey } from '@kevisual/use-config/context'; import { useContextKey } from '@kevisual/use-config/context';
console.log(process.env.REDIS_HOST); console.log(process.env.REDIS_HOST);
import { config } from '../config.ts';
const redisConfig = { const redisConfig = {
host: 'localhost', // Redis 服务器的主机名或 IP 地址 host: config?.redis?.host || 'localhost', // Redis 服务器的主机名或 IP 地址
port: 6379, // Redis 服务器的端口号 port: config?.redis?.port || 6379, // Redis 服务器的端口号
// password: 'your_password', // Redis 的密码 (如果有) password: config?.redis?.password, // Redis 的密码 (如果有)
}; };
const init = () => { const init = () => {
return new Redis({ return new Redis({