fenghuo/apps/backend/src/index.ts

81 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-05-27 15:37:27 +08:00
import { Hono } from 'hono';
import { logger } from 'hono/logger';
import { contextStorage, getContext } from 'hono/context-storage';
import { prettyJSON } from 'hono/pretty-json';
import { cors } from 'hono/cors';
import { trpcServer } from '@hono/trpc-server';
import Redis from 'ioredis';
import redis from './redis';
import minioClient from './minio';
import { Client } from 'minio';
2025-05-27 15:37:27 +08:00
import { appRouter } from './trpc';
import { wsHandler, wsConfig } from './socket';
2025-05-28 08:23:15 +08:00
// 导入新的路由
import userRest from './user/user.rest';
2025-05-28 20:00:36 +08:00
// 使用新的 @repo/storage 包
import { createStorageApp, startCleanupScheduler } from '@repo/storage';
2025-05-29 12:23:29 +08:00
2025-05-26 19:56:34 +08:00
type Env = {
2025-05-27 08:56:38 +08:00
Variables: {
redis: Redis;
minio: Client;
};
};
2025-05-26 19:56:34 +08:00
2025-05-27 08:56:38 +08:00
const app = new Hono<Env>();
2025-05-26 19:56:34 +08:00
2025-05-27 15:37:27 +08:00
app.use(
'*',
cors({
origin: 'http://localhost:3001',
credentials: true,
}),
);
2025-05-27 08:56:07 +08:00
2025-05-26 19:56:34 +08:00
app.use('*', async (c, next) => {
2025-05-27 08:56:38 +08:00
c.set('redis', redis);
c.set('minio', minioClient);
await next();
});
2025-05-26 19:56:34 +08:00
app.use('*', async (c, next) => {
2025-05-27 08:56:38 +08:00
c.set('redis', redis);
await next();
2025-05-26 19:56:34 +08:00
});
2025-05-27 08:56:38 +08:00
app.use(contextStorage());
app.use(prettyJSON()); // With options: prettyJSON({ space: 4 })
app.use(logger());
2025-05-26 19:56:34 +08:00
app.use(
2025-05-27 08:56:38 +08:00
'/trpc/*',
trpcServer({
router: appRouter,
}),
);
2025-05-28 08:23:14 +08:00
2025-05-28 08:23:15 +08:00
// 添加 REST API 路由
app.route('/api/users', userRest);
2025-05-28 08:23:14 +08:00
2025-05-28 20:00:36 +08:00
// 使用新的存储应用包含API和上传功能
const storageApp = createStorageApp({
apiBasePath: '/api/storage',
uploadPath: '/upload',
});
app.route('/', storageApp);
2025-05-27 15:37:27 +08:00
// 添加 WebSocket 路由
app.get('/ws', wsHandler);
2025-05-28 08:23:15 +08:00
// 启动上传清理定时任务
startCleanupScheduler();
2025-05-27 15:37:27 +08:00
const bunServerConfig = {
port: 3000,
fetch: app.fetch,
...wsConfig,
};
// 启动 Bun 服务器
Bun.serve(bunServerConfig);
2025-05-27 08:56:38 +08:00
export default app;