api.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { OpenAPIHono } from '@hono/zod-openapi'
  2. import { errorHandler } from './middleware/errorHandler'
  3. import base from './api/base'
  4. import { userOpenApiApp } from './api/user'
  5. import { authOpenApiApp } from './api/auth'
  6. // const app = new OpenAPIHono()
  7. const createApi = (app:OpenAPIHono) => {
  8. app.onError(errorHandler)
  9. // Rate limiting
  10. app.use('/api/v1/*', async (c, next) => {
  11. const ip = c.req.header('x-forwarded-for') || c.req.header('cf-connecting-ip')
  12. // 实现速率限制逻辑
  13. await next()
  14. })
  15. // Register routes
  16. app.route('/api/v1', base)
  17. app.route('/api/v1', userOpenApiApp)
  18. app.route('/api/v1', authOpenApiApp)
  19. // OpenAPI documentation endpoint
  20. app.doc31('/doc', {
  21. openapi: '3.1.0',
  22. info: {
  23. title: 'API Documentation',
  24. version: '1.0.0'
  25. },
  26. security: [{
  27. bearerAuth: []
  28. }]
  29. // servers: [{ url: '/api/v1' }]
  30. })
  31. // 注册Bearer认证方案
  32. app.openAPIRegistry.registerComponent('securitySchemes','bearerAuth',{
  33. type:'http',
  34. scheme:'bearer',
  35. bearerFormat:'JWT',
  36. description:'使用JWT进行认证'
  37. })
  38. return app;
  39. }
  40. export default createApi