app.ts 2.9 KB

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