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 { TaskScheduler } from './schedulers/task.scheduler' import userRoutes from './routes/user.routes' import recordsRoutes from './routes/records.routes' import fileRoutes from './routes/file.routes' import fishRoutes from './routes/fish.routes' import fishFriendsRoutes from './routes/fish-friends.routes' import messagesRoutes from './routes/messages.routes' import tgMsgSendRoutes from './routes/tg-msg-send.routes' import taskRoutes from './routes/task.routes' import testRoutes from './routes/test.routes' import tgUserRoutes from './routes/tg-user.routes' import chatGroupRoutes from './routes/chat-group.routes' import tgGroupRoutes from './routes/tg-group.routes' const options: FastifyEnvOptions = { schema: schema, dotenv: { debug: false } } export const createApp = async () => { const app = fastify({ disableRequestLogging: true, trustProxy: 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(recordsRoutes, { prefix: '/api/records' }) app.register(fileRoutes, { prefix: '/api/files' }) app.register(fishRoutes, { prefix: '/api/fish' }) app.register(fishFriendsRoutes, { prefix: '/api/fish-friends' }) app.register(messagesRoutes, { prefix: '/api/messages' }) app.register(tgMsgSendRoutes, { prefix: '/api/msg' }) app.register(taskRoutes, { prefix: '/api/tasks' }) app.register(tgUserRoutes, { prefix: '/api/tg-users' }) app.register(chatGroupRoutes, { prefix: '/api/chat-groups' }) app.register(tgGroupRoutes, { prefix: '/api/tg-groups' }) app.register(testRoutes, { prefix: '/api/test' }) const dataSource = createDataSource(app) await dataSource.initialize() app.decorate('dataSource', dataSource) app.addHook('onReady', async () => { const scheduler = TaskScheduler.getInstance(app) scheduler.trigger() app.log.info('TaskScheduler initialized') }) app.addHook('onClose', async () => { await dataSource.destroy() process.exit(0) }) return app }