|
@@ -1,5 +1,5 @@
|
|
|
import 'reflect-metadata'
|
|
import 'reflect-metadata'
|
|
|
-import fastify from 'fastify'
|
|
|
|
|
|
|
+import { fastify, errorCodes } from 'fastify'
|
|
|
import cors from '@fastify/cors'
|
|
import cors from '@fastify/cors'
|
|
|
import jwt from '@fastify/jwt'
|
|
import jwt from '@fastify/jwt'
|
|
|
import swagger from '@fastify/swagger'
|
|
import swagger from '@fastify/swagger'
|
|
@@ -11,35 +11,34 @@ import { createDataSource } from './config/database'
|
|
|
import userRoutes from './routes/user.routes'
|
|
import userRoutes from './routes/user.routes'
|
|
|
import recordsRoutes from './routes/records.routes'
|
|
import recordsRoutes from './routes/records.routes'
|
|
|
import fileRoutes from './routes/file.routes'
|
|
import fileRoutes from './routes/file.routes'
|
|
|
-import { config } from 'dotenv'
|
|
|
|
|
-
|
|
|
|
|
-config()
|
|
|
|
|
|
|
|
|
|
const options: FastifyEnvOptions = {
|
|
const options: FastifyEnvOptions = {
|
|
|
schema: schema,
|
|
schema: schema,
|
|
|
dotenv: {
|
|
dotenv: {
|
|
|
- debug: true
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ debug: false,
|
|
|
|
|
+ },
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const app = fastify({
|
|
|
|
|
- // disableRequestLogging: true,
|
|
|
|
|
- logger: {
|
|
|
|
|
- transport: {
|
|
|
|
|
- target: 'pino-pretty',
|
|
|
|
|
- options: {
|
|
|
|
|
- translateTime: 'yy/mm/dd HH:MM:ss Z',
|
|
|
|
|
- ignore: 'pid,hostname'
|
|
|
|
|
|
|
+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'
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
-})
|
|
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
-const start = async () => {
|
|
|
|
|
await app.register(fastifyEnv, options)
|
|
await app.register(fastifyEnv, options)
|
|
|
|
|
|
|
|
app.register(cors, {
|
|
app.register(cors, {
|
|
|
- origin: true
|
|
|
|
|
|
|
+ origin: true,
|
|
|
|
|
+ methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
app.register(jwt, {
|
|
app.register(jwt, {
|
|
@@ -51,7 +50,7 @@ const start = async () => {
|
|
|
|
|
|
|
|
app.register(multipart, {
|
|
app.register(multipart, {
|
|
|
limits: {
|
|
limits: {
|
|
|
- fileSize: 200 * 1024 * 1024 // 200MB
|
|
|
|
|
|
|
+ fileSize: 200 * 1024 * 1024
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -69,9 +68,11 @@ const start = async () => {
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- app.register(swaggerUi, {
|
|
|
|
|
- routePrefix: '/documentation'
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ if (app.config.NODE_ENV === 'development') {
|
|
|
|
|
+ app.register(swaggerUi, {
|
|
|
|
|
+ routePrefix: '/documentation'
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
app.register(userRoutes, { prefix: '/api/users' })
|
|
app.register(userRoutes, { prefix: '/api/users' })
|
|
|
app.register(recordsRoutes, { prefix: '/api/records' })
|
|
app.register(recordsRoutes, { prefix: '/api/records' })
|
|
@@ -79,18 +80,12 @@ const start = async () => {
|
|
|
|
|
|
|
|
const dataSource = createDataSource(app)
|
|
const dataSource = createDataSource(app)
|
|
|
await dataSource.initialize()
|
|
await dataSource.initialize()
|
|
|
-
|
|
|
|
|
app.decorate('dataSource', dataSource)
|
|
app.decorate('dataSource', dataSource)
|
|
|
|
|
|
|
|
- await app.listen({
|
|
|
|
|
- port: app.config.PORT,
|
|
|
|
|
- host: app.config.HOST
|
|
|
|
|
|
|
+ app.addHook('onClose', async () => {
|
|
|
|
|
+ await dataSource.destroy()
|
|
|
|
|
+ process.exit(0)
|
|
|
})
|
|
})
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-start().catch(err => {
|
|
|
|
|
- console.error('Error starting application:', err)
|
|
|
|
|
- process.exit(1)
|
|
|
|
|
-})
|
|
|
|
|
-
|
|
|
|
|
-export default app
|
|
|
|
|
|
|
+ return app
|
|
|
|
|
+}
|