| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import { OpenAPIHono } from '@hono/zod-openapi'
- import { errorHandler } from './middleware/errorHandler'
- import base from './api/base'
- import { userOpenApiApp } from './api/user'
- import { authOpenApiApp } from './api/auth'
- // const app = new OpenAPIHono()
- const createApi = (app:OpenAPIHono) => {
- app.onError(errorHandler)
- // Rate limiting
- app.use('/api/v1/*', async (c, next) => {
- const ip = c.req.header('x-forwarded-for') || c.req.header('cf-connecting-ip')
- // 实现速率限制逻辑
- await next()
- })
- // Register routes
- app.route('/api/v1', base)
- app.route('/api/v1', userOpenApiApp)
- app.route('/api/v1', authOpenApiApp)
- // OpenAPI documentation endpoint
- app.doc31('/doc', {
- openapi: '3.1.0',
- info: {
- title: 'API Documentation',
- version: '1.0.0'
- },
- security: [{
- bearerAuth: []
- }]
- // servers: [{ url: '/api/v1' }]
- })
- // 注册Bearer认证方案
- app.openAPIRegistry.registerComponent('securitySchemes','bearerAuth',{
- type:'http',
- scheme:'bearer',
- bearerFormat:'JWT',
- description:'使用JWT进行认证'
- })
- return app;
- }
- export default createApi
|