diff --git a/src/infra/http/app.js b/src/infra/http/app.js index 2ce9df5..34e187b 100644 --- a/src/infra/http/app.js +++ b/src/infra/http/app.js @@ -1,23 +1,44 @@ // src/infra/http/app.js const express = require('express'); -const router = require('./routes') +const routes = require('./routes'); function createApp() { - const app = express(); + const app = express(); - app.use('/api', router); // O router agora tem seu próprio middleware de JSON. + app.use(express.json()); + app.use(express.text({ type: '*/*' })); + - return app; + app.use((req, res, next) => { + let data = ''; + + req.on('data', chunk => { + data += chunk; + }); + + req.on('end', () => { + if (data && !req.body) { + req.rawBody = data; + + // tenta JSON + try { + req.body = JSON.parse(data); + } catch { + req.body = data; + } + } + + next(); + }); +}); + + + + + app.use('/api', routes); + + return app; } module.exports = createApp; - -/** - * @module app - * @description Este módulo é responsável por criar e configurar a instância do aplicativo Express. - * - * Funções: - * - `createApp()`: Uma factory function que inicializa o Express, aplica middlewares essenciais (como o `express.json` para parsear o corpo das requisições) e anexa as rotas da aplicação. - * Isso desacopla a criação do app da sua execução, facilitando testes. - */ \ No newline at end of file diff --git a/src/infra/http/routes/health.routes.js b/src/infra/http/routes/health.routes.js new file mode 100644 index 0000000..26183fe --- /dev/null +++ b/src/infra/http/routes/health.routes.js @@ -0,0 +1,20 @@ +// src/infra/http/routes/health.routes.js + +const { Router } = require('express'); +const { getRoutes } = require('./routeRegistry'); + +module.exports = () => { + const router = Router(); + + router.get('/health', (req, res) => { + res.json({ + status: 'ok', + env: process.env.NODE_ENV, + uptime: process.uptime(), + timestamp: new Date().toISOString(), + routes: getRoutes() + }); + }); + + return router; +}; diff --git a/src/infra/http/routes/index.js b/src/infra/http/routes/index.js index 2f96423..9621c57 100644 --- a/src/infra/http/routes/index.js +++ b/src/infra/http/routes/index.js @@ -1,16 +1,23 @@ +// src/infra/http/routes/index.js + const { Router } = require('express'); -const express = require('express'); -const closureController = require('../../../modules/close/controller/close.controller.js'); -const commentController = require('../../../modules/comments/controller/glpiComment.controller.js'); +const { registerRoute } = require('./routeRegistry'); + +const closeController = require('../../../modules/close/controller/close.controller'); +const commentController = require('../../../modules/comments/controller/glpiComment.controller'); +const healthRoutes = require('./health.routes'); const router = Router(); -router.use(express.json({ type: '*/*' })); -router.post('/webhook/close-ticket', closureController.closeTicket); +// health +router.use(healthRoutes()); +registerRoute('GET', '/api/health'); + +// webhooks +router.post('/webhook/close-ticket', closeController.closeTicket); +registerRoute('POST', '/api/webhook/close-ticket'); + router.post('/webhook/new-comment', commentController.handleGlpiComment); +registerRoute('POST', '/api/webhook/new-comment'); - - - - -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/src/infra/http/routes/routeRegistry.js b/src/infra/http/routes/routeRegistry.js new file mode 100644 index 0000000..1b119fb --- /dev/null +++ b/src/infra/http/routes/routeRegistry.js @@ -0,0 +1,16 @@ +// src/infra/http/routes/routeRegistry.js + +const routes = []; + +function registerRoute(method, path) { + routes.push({ method, path }); +} + +function getRoutes() { + return routes; +} + +module.exports = { + registerRoute, + getRoutes +}; diff --git a/src/modules/close/controller/close.controller.js b/src/modules/close/controller/close.controller.js index 0929884..53f2ccd 100644 --- a/src/modules/close/controller/close.controller.js +++ b/src/modules/close/controller/close.controller.js @@ -9,6 +9,8 @@ const { logInfo, logError } = require('../../../shared/utils/logger'); * @param {import('express').Response} res - O objeto de resposta do Express. */ const closeTicket = async (req, res) => { + + try { const result = await close(req.body) res.status(200).json(result)