fix: 配置修改,添加socket代理
This commit is contained in:
26
src/module/proxy/file-proxy.ts
Normal file
26
src/module/proxy/file-proxy.ts
Normal 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);
|
||||
};
|
||||
24
src/module/proxy/minio-proxy.ts
Normal file
24
src/module/proxy/minio-proxy.ts
Normal 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');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user