temp: 更新重定向配置
This commit is contained in:
parent
2fff202ed8
commit
cad914eead
@ -8,5 +8,6 @@
|
||||
domain: 'kevisual.xiongxiao.me',
|
||||
resources: 'https://minio.xiongxiao.me/resources',
|
||||
allowedOrigins: ['localhost', 'xiongxiao.me', 'zxj.im', 'silkyai.cn'],
|
||||
home: '/ai/chat',
|
||||
},
|
||||
}
|
||||
|
@ -49,7 +49,8 @@
|
||||
"@kevisual/use-config": "^1.0.8",
|
||||
"archiver": "^7.0.1",
|
||||
"ioredis": "^5.5.0",
|
||||
"nanoid": "^5.1.2"
|
||||
"nanoid": "^5.1.2",
|
||||
"sequelize": "^6.37.5"
|
||||
},
|
||||
"resolutions": {
|
||||
"picomatch": "^4.0.2"
|
||||
|
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@ -29,6 +29,9 @@ importers:
|
||||
nanoid:
|
||||
specifier: ^5.1.2
|
||||
version: 5.1.2
|
||||
sequelize:
|
||||
specifier: ^6.37.5
|
||||
version: 6.37.5(pg@8.13.3)
|
||||
devDependencies:
|
||||
'@rollup/plugin-commonjs':
|
||||
specifier: ^28.0.2
|
||||
|
@ -25,5 +25,5 @@ export default {
|
||||
declaration: false,
|
||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||
],
|
||||
external: ['ioredis', '@kevisual/router', '@kevisual/use-config'],
|
||||
external: ['ioredis', '@kevisual/router', '@kevisual/use-config', 'sequelize'],
|
||||
};
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { App } from '@kevisual/router';
|
||||
|
||||
// import { redis } from './module/redis/redis.ts';
|
||||
export const app = new App({
|
||||
serverOptions: {
|
||||
path: '/api/proxy',
|
||||
|
@ -27,6 +27,11 @@ type ConfigType = {
|
||||
* 允许跨域访问的地址
|
||||
*/
|
||||
allowOrigin: string[];
|
||||
|
||||
/**
|
||||
* home 不在代理范围内跳转到的地址
|
||||
*/
|
||||
home: string;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -10,12 +10,13 @@ import { createRefreshHtml } from './html/create-refresh-html.ts';
|
||||
const api = config?.api || { host: 'kevisual.xiongxiao.me', path: '/api/router' };
|
||||
const domain = config?.proxy?.domain || 'kevisual.xiongxiao.me';
|
||||
const allowedOrigins = config?.proxy?.allowOrigin || [];
|
||||
const home = config?.proxy?.home || '/ai/chat';
|
||||
|
||||
const noProxyUrl = ['/', '/favicon.ico'];
|
||||
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
if (req.url === '/favicon.ico') {
|
||||
res.writeHead(200, { 'Content-Type': 'image/x-icon' });
|
||||
res.write('proxy no favicon.ico\n');
|
||||
res.end('proxy no favicon.ico\n');
|
||||
return;
|
||||
}
|
||||
if (req.url.startsWith('/api/proxy')) {
|
||||
@ -97,13 +98,13 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
const data = await UserApp.getDomainApp(dns.hostName);
|
||||
if (!data) {
|
||||
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
||||
res.write('Invalid domain\n');
|
||||
return res.end();
|
||||
res.end('Invalid domain\n');
|
||||
return;
|
||||
}
|
||||
if (!data.user || !data.app) {
|
||||
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
||||
res.write('Invalid domain config\n');
|
||||
return res.end();
|
||||
res.end('Invalid domain config\n');
|
||||
return;
|
||||
}
|
||||
user = data.user;
|
||||
app = data.app;
|
||||
@ -113,6 +114,13 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
const pathname = new URL(req.url, `http://${dns.hostName}`).pathname;
|
||||
const url = pathname;
|
||||
if (!domainApp && noProxyUrl.includes(url)) {
|
||||
if (url === '/') {
|
||||
// 获取一下登陆用户,如果没有登陆用户,重定向到ai-chat页面
|
||||
// 重定向到
|
||||
res.writeHead(302, { Location: home });
|
||||
return res.end();
|
||||
}
|
||||
// 不是域名代理,且是在不代理的url当中
|
||||
res.write('No proxy for this URL\n');
|
||||
return res.end();
|
||||
}
|
||||
|
5
src/module/models.ts
Normal file
5
src/module/models.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { User, UserInit, Org, OrgInit } from '@kevisual/code-center-module';
|
||||
|
||||
export { User, Org };
|
||||
UserInit();
|
||||
OrgInit();
|
@ -1,23 +1,27 @@
|
||||
import { Redis } from 'ioredis';
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
import { useContextKey } from '@kevisual/use-config/context';
|
||||
|
||||
const config = useConfig<{
|
||||
redis: ConstructorParameters<typeof Redis>;
|
||||
}>();
|
||||
const init = () => {
|
||||
return new Redis({
|
||||
host: 'localhost', // Redis 服务器的主机名或 IP 地址
|
||||
port: 6379, // Redis 服务器的端口号
|
||||
// password: 'your_password', // Redis 的密码 (如果有)
|
||||
db: 0, // 要使用的 Redis 数据库索引 (0-15)
|
||||
keyPrefix: '', // key 前缀
|
||||
retryStrategy(times) {
|
||||
// 连接重试策略
|
||||
return Math.min(times * 50, 2000); // 每次重试时延迟增加
|
||||
},
|
||||
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
|
||||
...config.redis,
|
||||
});
|
||||
};
|
||||
// 配置 Redis 连接
|
||||
export const redis = new Redis({
|
||||
host: 'localhost', // Redis 服务器的主机名或 IP 地址
|
||||
port: 6379, // Redis 服务器的端口号
|
||||
// password: 'your_password', // Redis 的密码 (如果有)
|
||||
db: 0, // 要使用的 Redis 数据库索引 (0-15)
|
||||
keyPrefix: '', // key 前缀
|
||||
retryStrategy(times) {
|
||||
// 连接重试策略
|
||||
return Math.min(times * 50, 2000); // 每次重试时延迟增加
|
||||
},
|
||||
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
|
||||
...config.redis,
|
||||
});
|
||||
export const redis = useContextKey('redis', init);
|
||||
export const subscriber = redis.duplicate(); // 创建一个订阅者连接
|
||||
|
||||
async function ensureKeyspaceNotifications() {
|
||||
|
30
src/module/sequelize.ts
Normal file
30
src/module/sequelize.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
import { Sequelize } from 'sequelize';
|
||||
import { useContextKey, useContext } from '@kevisual/use-config/context';
|
||||
|
||||
type PostgresConfig = {
|
||||
postgres: {
|
||||
username: string;
|
||||
password: string;
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
};
|
||||
};
|
||||
const config = useConfig<PostgresConfig>();
|
||||
|
||||
const postgresConfig = config.postgres;
|
||||
|
||||
if (!postgresConfig) {
|
||||
console.error('postgres config is required');
|
||||
process.exit(1);
|
||||
}
|
||||
// connect to db
|
||||
export const init = () => {
|
||||
return new Sequelize({
|
||||
dialect: 'postgres',
|
||||
...postgresConfig,
|
||||
// logging: false,
|
||||
});
|
||||
};
|
||||
export const sequelize = useContextKey('sequelize', init);
|
@ -4,10 +4,24 @@ import { redis } from '@/module/redis/redis.ts';
|
||||
import fs from 'fs';
|
||||
import { fileStore } from '../../module/config.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'auth-admin',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { user } = ctx.query;
|
||||
if (user !== 'admin') {
|
||||
ctx.throw('Not Found');
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'list',
|
||||
middleware: ['auth-admin'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const keys = await redis.keys('user:app:*');
|
||||
@ -24,6 +38,7 @@ app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'delete',
|
||||
middleware: ['auth-admin'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { user, app } = ctx.query;
|
||||
@ -76,6 +91,7 @@ app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'get',
|
||||
middleware: ['auth-admin'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { user, app } = ctx.query;
|
||||
@ -100,6 +116,7 @@ app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'status',
|
||||
middleware: ['auth-admin'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { user, app } = ctx.query;
|
||||
|
Loading…
x
Reference in New Issue
Block a user