app.ts 3.2 KB

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