app.ts 3.4 KB

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