fenghuo/apps/backend/src/index.ts

57 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-05-26 19:56:34 +08:00
import { Hono } from 'hono'
import { contextStorage, getContext } from 'hono/context-storage'
import { prettyJSON } from 'hono/pretty-json'
2025-05-27 08:56:07 +08:00
import { cors } from 'hono/cors'
2025-05-26 19:56:34 +08:00
import { trpcServer } from '@hono/trpc-server'
2025-05-27 08:56:07 +08:00
2025-05-26 19:56:34 +08:00
import Redis from 'ioredis'
import redis from './redis'
import minioClient from './minio'
import { Client } from 'minio'
2025-05-27 08:56:07 +08:00
import { appRouter } from './trpc'
2025-05-28 08:23:14 +08:00
import { oidcApp } from './oidc-demo'
2025-05-26 19:56:34 +08:00
type Env = {
Variables: {
redis: Redis
minio: Client
}
}
const app = new Hono<Env>()
2025-05-28 08:23:14 +08:00
// 全局CORS配置
2025-05-27 08:56:07 +08:00
app.use('*', cors({
2025-05-28 08:23:14 +08:00
origin: '*',
2025-05-27 08:56:07 +08:00
}))
2025-05-28 08:23:14 +08:00
// 注入依赖
2025-05-26 19:56:34 +08:00
app.use('*', async (c, next) => {
c.set('redis', redis)
c.set('minio', minioClient)
await next()
})
2025-05-28 08:23:14 +08:00
// 中间件
2025-05-26 19:56:34 +08:00
app.use(contextStorage())
2025-05-28 08:23:14 +08:00
app.use(prettyJSON())
app.route('/oidc', oidcApp)
// 挂载tRPC
2025-05-26 19:56:34 +08:00
app.use(
'/trpc/*',
trpcServer({
router: appRouter,
})
)
2025-05-28 08:23:14 +08:00
// 启动服务器
const port = parseInt(process.env.PORT || '3000');
export default {
port,
fetch: app.fetch,
}
console.log(`🚀 服务器运行在 http://localhost:${port}`)