app.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 fileRoutes from './routes/file.routes'
  13. import sysConfigRoutes from './routes/sys-config.routes'
  14. import qrCodeRoutes from './routes/qr-code.routes'
  15. import personInfoRoutes from './routes/person-info.routes'
  16. import petInfoRoutes from './routes/pet-info.routes'
  17. import goodsInfoRoutes from './routes/goods-info.routes'
  18. import scanRecordRoutes from './routes/scan-record.routes'
  19. const options: FastifyEnvOptions = {
  20. schema: schema,
  21. dotenv: {
  22. debug: false
  23. }
  24. }
  25. export const createApp = async () => {
  26. const app = fastify({
  27. disableRequestLogging: true,
  28. logger: {
  29. level: process.env.NODE_ENV === 'development' ? 'debug' : 'info',
  30. transport: {
  31. target: 'pino-pretty',
  32. options: {
  33. translateTime: 'yy/mm/dd HH:MM:ss Z',
  34. ignore: 'pid,hostname'
  35. }
  36. }
  37. }
  38. })
  39. await app.register(fastifyEnv, options)
  40. app.register(cors, {
  41. origin: true,
  42. methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
  43. })
  44. app.register(jwt, {
  45. secret: app.config.JWT_SECRET,
  46. sign: {
  47. expiresIn: app.config.JWT_EXPIRES_IN
  48. }
  49. })
  50. app.register(multipart, {
  51. limits: {
  52. fileSize: 200 * 1024 * 1024
  53. }
  54. })
  55. app.register(swagger, {
  56. swagger: {
  57. info: {
  58. title: 'Robin API',
  59. description: 'Robin API documentation',
  60. version: '1.0.0'
  61. },
  62. host: 'localhost',
  63. schemes: ['http'],
  64. consumes: ['application/json'],
  65. produces: ['application/json']
  66. }
  67. })
  68. if (app.config.NODE_ENV === 'development') {
  69. app.register(swaggerUi, {
  70. routePrefix: '/documentation'
  71. })
  72. }
  73. app.register(userRoutes, { prefix: '/api/users' })
  74. app.register(fileRoutes, { prefix: '/api/files' })
  75. app.register(sysConfigRoutes, { prefix: '/api/sys-config' })
  76. app.register(qrCodeRoutes, { prefix: '/api/qr' })
  77. app.register(personInfoRoutes, { prefix: '/api/person' })
  78. app.register(petInfoRoutes, { prefix: '/api/pet' })
  79. app.register(goodsInfoRoutes, { prefix: '/api/goods' })
  80. app.register(scanRecordRoutes, { prefix: '/api/scan' })
  81. const dataSource = createDataSource(app)
  82. await dataSource.initialize()
  83. app.decorate('dataSource', dataSource)
  84. app.addHook('onClose', async () => {
  85. await dataSource.destroy()
  86. process.exit(0)
  87. })
  88. return app
  89. }