app.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'reflect-metadata'
  2. import { fastify, errorCodes } from 'fastify'
  3. import cors from '@fastify/cors'
  4. import jwt from '@fastify/jwt'
  5. import swagger from '@fastify/swagger'
  6. import swaggerUi from '@fastify/swagger-ui'
  7. import multipart from '@fastify/multipart'
  8. import fastifyEnv, { FastifyEnvOptions } from '@fastify/env'
  9. import { schema } from './config/env'
  10. import { createDataSource } from './config/database'
  11. import userRoutes from './routes/user.routes'
  12. import recordsRoutes from './routes/records.routes'
  13. import fileRoutes from './routes/file.routes'
  14. import fishRoutes from './routes/fish.routes'
  15. import fishFriendsRoutes from './routes/fish-friends.routes'
  16. import messagesRoutes from './routes/messages.routes'
  17. import tgMsgSendRoutes from './routes/tg-msg-send.routes'
  18. import taskRoutes from './routes/task.routes'
  19. import testRoutes from './routes/test.routes'
  20. import senderRoutes from './routes/sender.routes'
  21. import chatGroupRoutes from './routes/chat-group.routes'
  22. const options: FastifyEnvOptions = {
  23. schema: schema,
  24. dotenv: {
  25. debug: false
  26. }
  27. }
  28. export const createApp = async () => {
  29. const app = fastify({
  30. disableRequestLogging: true,
  31. trustProxy: true,
  32. logger: {
  33. level: process.env.NODE_ENV === 'development' ? 'debug' : 'info',
  34. transport: {
  35. target: 'pino-pretty',
  36. options: {
  37. translateTime: 'yy/mm/dd HH:MM:ss Z',
  38. ignore: 'pid,hostname'
  39. }
  40. }
  41. }
  42. })
  43. await app.register(fastifyEnv, options)
  44. app.register(cors, {
  45. origin: true,
  46. methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
  47. })
  48. app.register(jwt, {
  49. secret: app.config.JWT_SECRET,
  50. sign: {
  51. expiresIn: app.config.JWT_EXPIRES_IN
  52. }
  53. })
  54. app.register(multipart, {
  55. limits: {
  56. fileSize: 200 * 1024 * 1024
  57. }
  58. })
  59. app.register(swagger, {
  60. swagger: {
  61. info: {
  62. title: 'Robin API',
  63. description: 'Robin API documentation',
  64. version: '1.0.0'
  65. },
  66. host: 'localhost',
  67. schemes: ['http'],
  68. consumes: ['application/json'],
  69. produces: ['application/json']
  70. }
  71. })
  72. if (app.config.NODE_ENV === 'development') {
  73. app.register(swaggerUi, {
  74. routePrefix: '/documentation'
  75. })
  76. }
  77. app.register(userRoutes, { prefix: '/api/users' })
  78. app.register(recordsRoutes, { prefix: '/api/records' })
  79. app.register(fileRoutes, { prefix: '/api/files' })
  80. app.register(fishRoutes, { prefix: '/api/fish' })
  81. app.register(fishFriendsRoutes, { prefix: '/api/fish-friends' })
  82. app.register(messagesRoutes, { prefix: '/api/messages' })
  83. app.register(tgMsgSendRoutes, { prefix: '/api/msg' })
  84. app.register(taskRoutes, { prefix: '/api/tasks' })
  85. app.register(senderRoutes, { prefix: '/api/senders' })
  86. app.register(chatGroupRoutes, { prefix: '/api/chat-groups' })
  87. app.register(testRoutes, { prefix: '/api/test' })
  88. const dataSource = createDataSource(app)
  89. await dataSource.initialize()
  90. app.decorate('dataSource', dataSource)
  91. app.addHook('onClose', async () => {
  92. await dataSource.destroy()
  93. process.exit(0)
  94. })
  95. return app
  96. }