feat: 更新N5Proxy逻辑,使用baseURL构建请求链接,并优化错误处理与HTML渲染
This commit is contained in:
@@ -7,3 +7,6 @@ import { useKey } from "@kevisual/use-config";
|
|||||||
export const proxyDomain = useKey('PROXY_DOMAIN') || ''; // 请在这里填写你的域名
|
export const proxyDomain = useKey('PROXY_DOMAIN') || ''; // 请在这里填写你的域名
|
||||||
|
|
||||||
export const baseProxyUrl = proxyDomain ? `https://${proxyDomain}` : 'https://kevisual.cn';
|
export const baseProxyUrl = proxyDomain ? `https://${proxyDomain}` : 'https://kevisual.cn';
|
||||||
|
|
||||||
|
|
||||||
|
export const baseURL = baseProxyUrl;
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
import { convex, convexApi } from '@/app.ts';
|
import { convex, convexApi } from '@/app.ts';
|
||||||
import { User } from '@/models/user.ts';
|
import { User } from '@/models/user.ts';
|
||||||
|
import { omit } from 'es-toolkit';
|
||||||
import { IncomingMessage, ServerResponse } from 'http';
|
import { IncomingMessage, ServerResponse } from 'http';
|
||||||
|
import { renderServerHtml } from '../html/render-server-html.ts';
|
||||||
|
import { baseURL } from '../domain.ts';
|
||||||
|
|
||||||
type ProxyOptions = {
|
type ProxyOptions = {
|
||||||
createNotFoundPage: (msg?: string) => any;
|
createNotFoundPage: (msg?: string) => any;
|
||||||
@@ -8,7 +11,7 @@ type ProxyOptions = {
|
|||||||
// /n5/:slug/
|
// /n5/:slug/
|
||||||
export const N5Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => {
|
export const N5Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => {
|
||||||
const { url } = req;
|
const { url } = req;
|
||||||
const _url = new URL(url || '', `http://localhost`);
|
const _url = new URL(url || '', baseURL);
|
||||||
const { pathname, searchParams } = _url;
|
const { pathname, searchParams } = _url;
|
||||||
let [user, app, userAppKey] = pathname.split('/').slice(1);
|
let [user, app, userAppKey] = pathname.split('/').slice(1);
|
||||||
if (!app) {
|
if (!app) {
|
||||||
@@ -23,7 +26,7 @@ export const N5Proxy = async (req: IncomingMessage, res: ServerResponse, opts?:
|
|||||||
const data = convexResult.data ?? {};
|
const data = convexResult.data ?? {};
|
||||||
const link = data.link;
|
const link = data.link;
|
||||||
if (!link) {
|
if (!link) {
|
||||||
opts?.createNotFoundPage?.('应用未找到');
|
opts?.createNotFoundPage?.('不存在对应的跳转的链接');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (data?.useOwnerToken) {
|
if (data?.useOwnerToken) {
|
||||||
@@ -42,13 +45,24 @@ export const N5Proxy = async (req: IncomingMessage, res: ServerResponse, opts?:
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error fetching the link:', e);
|
console.error('Error fetching the link:', e);
|
||||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||||
res.end(JSON.stringify({ error: 'Failed to fetch the link' }));
|
res.end(JSON.stringify({ error: '请求服务区失败' }));
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
res.writeHead(302, {
|
const type = data?.type;
|
||||||
Location: link,
|
if (type === 'html-render') {
|
||||||
});
|
const createJson = omit(data, ['useOwnerToken', 'permission']);
|
||||||
|
const html = await fetch(link, { method: 'GET' }).then(res => res.text());
|
||||||
|
const renderedHtml = await renderServerHtml({
|
||||||
|
data: createJson,
|
||||||
|
pathname: _url.toString(),
|
||||||
|
}, html);
|
||||||
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||||
|
res.end(renderedHtml);
|
||||||
|
} else {
|
||||||
|
res.writeHead(302, {
|
||||||
|
Location: link,
|
||||||
|
});
|
||||||
|
}
|
||||||
res.end();
|
res.end();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user