fix: 配置修改,添加socket代理

This commit is contained in:
2025-03-06 22:11:34 +08:00
parent d548d3ab04
commit bb86a7c507
10 changed files with 867 additions and 161 deletions

View File

@@ -0,0 +1,26 @@
import http from 'http';
import send from 'send';
import fs from 'fs';
import { fileIsExist } from '@kevisual/use-config';
import path from 'path';
export type ProxyInfo = {
path?: string;
target: string;
type?: 'static' | 'dynamic' | 'minio';
};
export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
// url开头的文件
const url = new URL(req.url, 'http://localhost');
const pathname = url.pathname;
// 检测文件是否存在如果文件不存在则返回404
const filePath = path.join(process.cwd(), proxyApi.target, pathname);
if (!fileIsExist(filePath)) {
res.statusCode = 404;
res.end('Not Found File');
return;
}
const file = send(req, pathname, {
root: proxyApi.target,
});
file.pipe(res);
};

View File

@@ -0,0 +1,24 @@
import http from 'http';
import { minioClient } from '../minio.ts';
export type ProxyInfo = {
path?: string;
target: string;
type?: 'static' | 'dynamic' | 'minio';
};
export const minioProxy = async (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
try {
const requestUrl = new URL(req.url, 'http://localhost');
const objectPath = requestUrl.pathname;
const bucketName = proxyApi.target;
let objectName = objectPath.slice(1);
if (objectName.startsWith(bucketName)) {
objectName = objectName.slice(bucketName.length);
}
const objectStream = await minioClient.getObject(bucketName, objectName);
objectStream.pipe(res);
} catch (error) {
console.error('Error fetching object from MinIO:', error);
res.statusCode = 500;
res.end('Internal Server Error');
}
};