53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
/*
|
|
* @Date: 2025-06-02 19:52:12
|
|
* @LastEditors: 陈子健
|
|
* @LastEditTime: 2025-06-18 14:28:41
|
|
* @FilePath: /my-score/honoback/main.ts
|
|
*/
|
|
import { Hono } from 'hono'
|
|
import { jwt } from 'hono/jwt'
|
|
import type { JwtVariables } from 'hono/jwt'
|
|
import user from './routes/user.ts'
|
|
import media from './routes/media.ts'
|
|
import auth from './routes/auth.ts'
|
|
|
|
const app = new Hono<{ Variables: JwtVariables }>()
|
|
|
|
const AUTH_SECRET = process.env.AUTH_SECRET || 'it-is-a-secret'
|
|
|
|
// 添加请求日志中间件
|
|
app.use('*', async (c, next) => {
|
|
console.log(`${c.req.method} ${c.req.url}`)
|
|
await next()
|
|
})
|
|
|
|
// 注册认证路由
|
|
app.route('/api/auth', auth)
|
|
|
|
// 注册用户路由
|
|
app.route('/api/user', user)
|
|
|
|
// 注册媒体路由
|
|
app.route('/api/media', media)
|
|
|
|
// 需要认证的路由
|
|
app.use('/api/test/*', jwt({
|
|
secret: AUTH_SECRET,
|
|
}))
|
|
|
|
// 添加404处理
|
|
app.notFound((c) => {
|
|
return c.json({ code: 404, message: 'Not Found' }, 404)
|
|
})
|
|
|
|
// 获取端口配置
|
|
const port = parseInt(process.env.PORT || '8000')
|
|
|
|
// 启动服务器
|
|
Bun.serve({
|
|
port,
|
|
fetch: app.fetch,
|
|
})
|
|
|
|
console.log(`Server running on port ${port}`)
|