temp: 更新重定向配置
This commit is contained in:
		@@ -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);
 | 
			
		||||
		Reference in New Issue
	
	Block a user