fenghuo/apps/backend/src/index.ts

81 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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';
import { appRouter } from './trpc';
import { wsHandler, wsConfig } from './socket';
// 导入新的路由
import userRest from './user/user.rest';
// 使用新的 @repo/storage 包
import { createStorageApp, startCleanupScheduler } from '@repo/storage';
type Env = {
Variables: {
redis: Redis;
minio: Client;
};
};
const app = new Hono<Env>();
app.use(
'*',
cors({
origin: 'http://localhost:3001',
credentials: true,
}),
);
app.use('*', async (c, next) => {
c.set('redis', redis);
c.set('minio', minioClient);
await next();
});
app.use('*', async (c, next) => {
c.set('redis', redis);
await next();
});
app.use(contextStorage());
app.use(prettyJSON()); // With options: prettyJSON({ space: 4 })
app.use(logger());
app.use(
'/trpc/*',
trpcServer({
router: appRouter,
}),
);
// 添加 REST API 路由
app.route('/api/users', userRest);
// 使用新的存储应用包含API和上传功能
const storageApp = createStorageApp({
apiBasePath: '/api/storage',
uploadPath: '/upload',
});
app.route('/', storageApp);
// 添加 WebSocket 路由
app.get('/ws', wsHandler);
// 启动上传清理定时任务
startCleanupScheduler();
const bunServerConfig = {
port: 3000,
fetch: app.fetch,
...wsConfig,
};
// 启动 Bun 服务器
Bun.serve(bunServerConfig);
export default app;