| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import 'reflect-metadata'
- import { fastify, errorCodes } from 'fastify'
- import cors from '@fastify/cors'
- import jwt from '@fastify/jwt'
- import swagger from '@fastify/swagger'
- import swaggerUi from '@fastify/swagger-ui'
- import multipart from '@fastify/multipart'
- import fastifyEnv, { FastifyEnvOptions } from '@fastify/env'
- import { schema } from './config/env'
- import { createDataSource } from './config/database'
- import userRoutes from './routes/user.routes'
- import fileRoutes from './routes/file.routes'
- import sysConfigRoutes from './routes/sys-config.routes'
- import qrCodeRoutes from './routes/qr-code.routes'
- import personInfoRoutes from './routes/person-info.routes'
- import petInfoRoutes from './routes/pet-info.routes'
- import goodsInfoRoutes from './routes/goods-info.routes'
- import scanRecordRoutes from './routes/scan-record.routes'
- const options: FastifyEnvOptions = {
- schema: schema,
- dotenv: {
- debug: false
- }
- }
- export const createApp = async () => {
- const app = fastify({
- disableRequestLogging: true,
- logger: {
- level: process.env.NODE_ENV === 'development' ? 'debug' : 'info',
- transport: {
- target: 'pino-pretty',
- options: {
- translateTime: 'yy/mm/dd HH:MM:ss Z',
- ignore: 'pid,hostname'
- }
- }
- }
- })
- await app.register(fastifyEnv, options)
- app.register(cors, {
- origin: true,
- methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
- })
- app.register(jwt, {
- secret: app.config.JWT_SECRET,
- sign: {
- expiresIn: app.config.JWT_EXPIRES_IN
- }
- })
- app.register(multipart, {
- limits: {
- fileSize: 200 * 1024 * 1024
- }
- })
- app.register(swagger, {
- swagger: {
- info: {
- title: 'Robin API',
- description: 'Robin API documentation',
- version: '1.0.0'
- },
- host: 'localhost',
- schemes: ['http'],
- consumes: ['application/json'],
- produces: ['application/json']
- }
- })
- if (app.config.NODE_ENV === 'development') {
- app.register(swaggerUi, {
- routePrefix: '/documentation'
- })
- }
- app.register(userRoutes, { prefix: '/api/users' })
- app.register(fileRoutes, { prefix: '/api/files' })
- app.register(sysConfigRoutes, { prefix: '/api/sys-config' })
- app.register(qrCodeRoutes, { prefix: '/api/qr' })
- app.register(personInfoRoutes, { prefix: '/api/person' })
- app.register(petInfoRoutes, { prefix: '/api/pet' })
- app.register(goodsInfoRoutes, { prefix: '/api/goods' })
- app.register(scanRecordRoutes, { prefix: '/api/scan' })
- const dataSource = createDataSource(app)
- await dataSource.initialize()
- app.decorate('dataSource', dataSource)
- app.addHook('onClose', async () => {
- await dataSource.destroy()
- process.exit(0)
- })
- return app
- }
|