generated from tailored/router-template
fix: fix proxy and api error
This commit is contained in:
parent
2e1cf531cf
commit
01503954ad
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/assistant-module",
|
"name": "@kevisual/assistant-module",
|
||||||
"version": "0.0.4-beta.2",
|
"version": "0.0.4-beta.3",
|
||||||
"description": "assistant module",
|
"description": "assistant module",
|
||||||
"main": "dist/assistant-module.mjs",
|
"main": "dist/assistant-module.mjs",
|
||||||
"types": "dist/assistant-module.d.ts",
|
"types": "dist/assistant-module.d.ts",
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查文件是否存在
|
||||||
|
* @param filePath 文件路径
|
||||||
|
* @param checkIsFile 是否检查文件类型 default: false
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export const checkFileExists = (filePath: string, checkIsFile = false) => {
|
export const checkFileExists = (filePath: string, checkIsFile = false) => {
|
||||||
try {
|
try {
|
||||||
fs.accessSync(filePath);
|
fs.accessSync(filePath);
|
||||||
|
@ -18,7 +18,7 @@ export const defaultApiProxy = [
|
|||||||
* @param paths ['/api/router', '/v1' ]
|
* @param paths ['/api/router', '/v1' ]
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const createApiProxy = (api: string, paths: string[] = ['/api/router', '/v1', '/resources']) => {
|
export const createApiProxy = (api: string, paths: string[] = ['/api', '/v1', '/resources']) => {
|
||||||
const pathList = paths.map((item) => {
|
const pathList = paths.map((item) => {
|
||||||
return {
|
return {
|
||||||
path: item,
|
path: item,
|
||||||
|
@ -1,25 +1,27 @@
|
|||||||
import http from 'http';
|
import http from 'http';
|
||||||
import send from 'send';
|
import send from 'send';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { fileIsExist } from '@kevisual/use-config';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { ProxyInfo } from './proxy.ts';
|
import { ProxyInfo } from './proxy.ts';
|
||||||
|
import { checkFileExists } from '@/file/index.ts';
|
||||||
|
|
||||||
export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
|
export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
|
||||||
// url开头的文件
|
// url开头的文件
|
||||||
const url = new URL(req.url, 'http://localhost');
|
const url = new URL(req.url, 'http://localhost');
|
||||||
let pathname = url.pathname.slice(1);
|
const [user, key, _info] = url.pathname.split('/');
|
||||||
|
const pathname = url.pathname.slice(1);
|
||||||
const { indexPath = '', target = '', rootPath = process.cwd() } = proxyApi;
|
const { indexPath = '', target = '', rootPath = process.cwd() } = proxyApi;
|
||||||
try {
|
try {
|
||||||
if (pathname.endsWith('/')) {
|
|
||||||
pathname = pathname + 'index.html';
|
|
||||||
}
|
|
||||||
// 检测文件是否存在,如果文件不存在,则返回404
|
// 检测文件是否存在,如果文件不存在,则返回404
|
||||||
let filePath = path.join(rootPath, target, pathname);
|
let filePath = '';
|
||||||
let exist = fileIsExist(filePath);
|
let exist = false;
|
||||||
|
if (_info) {
|
||||||
|
filePath = path.join(rootPath, target, pathname);
|
||||||
|
exist = checkFileExists(filePath, true);
|
||||||
|
}
|
||||||
if (!exist) {
|
if (!exist) {
|
||||||
filePath = path.join(rootPath, target, '/' + indexPath);
|
filePath = path.join(rootPath, target, indexPath);
|
||||||
exist = fileIsExist(filePath);
|
exist = checkFileExists(filePath, true);
|
||||||
}
|
}
|
||||||
console.log('filePath', filePath, exist);
|
console.log('filePath', filePath, exist);
|
||||||
|
|
||||||
|
@ -21,7 +21,8 @@
|
|||||||
"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\" ",
|
||||||
"clean": "rm -rf dist",
|
"clean": "rm -rf dist",
|
||||||
"prepub": "envision switch root",
|
"prepub": "envision switch root",
|
||||||
"pub": "npm run build && envision pack -p -u"
|
"pub": "npm run build && envision pack -p -u",
|
||||||
|
"download": "ev app download -i root/assistant-base-app"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "abearxiong <xiongxiao@xiongxiao.me>",
|
"author": "abearxiong <xiongxiao@xiongxiao.me>",
|
||||||
|
@ -48,8 +48,8 @@ export const setConfig = (config?: AssistantConfig) => {
|
|||||||
if (!config) {
|
if (!config) {
|
||||||
return assistantConfig;
|
return assistantConfig;
|
||||||
}
|
}
|
||||||
assistantConfig = config;
|
assistantConfig = { ...assistantConfig, ...config };
|
||||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
fs.writeFileSync(configPath, JSON.stringify(assistantConfig, null, 2));
|
||||||
return assistantConfig;
|
return assistantConfig;
|
||||||
};
|
};
|
||||||
type AppConfig = {
|
type AppConfig = {
|
||||||
@ -69,7 +69,8 @@ export const getAppConfig = (): AppConfig => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const setAppConfig = (config: AppConfig) => {
|
export const setAppConfig = (config: AppConfig) => {
|
||||||
fs.writeFileSync(appConfigPath, JSON.stringify(config, null, 2));
|
const _config = getAppConfig();
|
||||||
|
fs.writeFileSync(appConfigPath, JSON.stringify({ ..._config, ...config }, null, 2));
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -17,37 +17,50 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
|
|||||||
console.log('handle by router');
|
console.log('handle by router');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// client, api, v1, serve 开头的拦截
|
||||||
const apiProxyList = assistantConfig?.apiProxyList || [];
|
const apiProxyList = assistantConfig?.apiProxyList || [];
|
||||||
const defaultApiProxy = createApiProxy(assistantConfig?.pageApi || 'https://kevisual.xiongxiao.me');
|
const defaultApiProxy = createApiProxy(assistantConfig?.pageApi || 'https://kevisual.xiongxiao.me');
|
||||||
const apiBackendProxy = [...apiProxyList, ...defaultApiProxy].find((item) => pathname.startsWith(item.path));
|
const apiBackendProxy = [...apiProxyList, ...defaultApiProxy].find((item) => pathname.startsWith(item.path));
|
||||||
if (apiBackendProxy) {
|
if (apiBackendProxy) {
|
||||||
console.log('apiBackendProxy', apiBackendProxy);
|
console.log('apiBackendProxy', apiBackendProxy, req.url);
|
||||||
return apiProxy(req, res, {
|
return apiProxy(req, res, {
|
||||||
path: apiBackendProxy.path,
|
path: apiBackendProxy.path,
|
||||||
target: apiBackendProxy.target,
|
target: apiBackendProxy.target,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// client, api, v1, serve 开头的拦截
|
const urls = pathname.split('/');
|
||||||
|
const [_, _user, _app] = urls;
|
||||||
|
if (!_app) {
|
||||||
|
res.statusCode = 404;
|
||||||
|
res.end('Not Found Proxy');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_app && urls.length === 3) {
|
||||||
|
// 重定向到
|
||||||
|
res.writeHead(302, { Location: `${req.url}/` });
|
||||||
|
return res.end();
|
||||||
|
}
|
||||||
const proxyApiList = assistantConfig?.proxy || [];
|
const proxyApiList = assistantConfig?.proxy || [];
|
||||||
const proxyApi = proxyApiList.find((item) => pathname.startsWith(item.path));
|
const proxyApi = proxyApiList.find((item) => pathname.startsWith(item.path));
|
||||||
if (proxyApi) {
|
if (proxyApi) {
|
||||||
console.log('proxyApi', proxyApi, pathname);
|
console.log('proxyApi', proxyApi, pathname);
|
||||||
const { user, key } = proxyApi;
|
const { user, key } = proxyApi;
|
||||||
return fileProxy(req, res, {
|
return fileProxy(req, res, {
|
||||||
path: proxyApi.path,
|
path: proxyApi.path, // 代理路径, 比如/root/center
|
||||||
rootPath: appDir,
|
rootPath: appDir, // 根路径
|
||||||
indexPath: `${user}/${key}/index.html`,
|
indexPath: `${user}/${key}/index.html`, // 首页路径
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const localProxyProxy = localProxyProxyList.find((item) => pathname.startsWith(item.path));
|
const localProxyProxy = localProxyProxyList.find((item) => pathname.startsWith(item.path));
|
||||||
if (localProxyProxy) {
|
if (localProxyProxy) {
|
||||||
|
console.log('localProxyProxy', localProxyProxy, req.url);
|
||||||
return fileProxy(req, res, {
|
return fileProxy(req, res, {
|
||||||
path: localProxyProxy.path,
|
path: localProxyProxy.path,
|
||||||
rootPath: process.cwd(),
|
rootPath: process.cwd(),
|
||||||
indexPath: localProxyProxy.indexPath,
|
indexPath: localProxyProxy.indexPath,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
console.log('handle by router 404');
|
console.log('handle by router 404', req.url);
|
||||||
res.statusCode = 404;
|
res.statusCode = 404;
|
||||||
res.end('Not Found Proxy');
|
res.end('Not Found Proxy');
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user