From b2e144f35ca1176eca5baa3cbb53770c3173f2c6 Mon Sep 17 00:00:00 2001 From: Gabriel Pastori <58153955+gabrielpastori1@users.noreply.github.com> Date: Mon, 11 Dec 2023 15:06:42 -0300 Subject: [PATCH] add manager --- package.json | 1 + src/whatsapp/controllers/views.controller.ts | 23 ------------------- src/whatsapp/routers/view.router.ts | 24 +++++++++++++++++--- src/whatsapp/whatsapp.module.ts | 2 -- 4 files changed, 22 insertions(+), 28 deletions(-) delete mode 100644 src/whatsapp/controllers/views.controller.ts diff --git a/package.json b/package.json index 8625030a5..b5d3ee92e 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "cross-env": "^7.0.3", "dayjs": "^1.11.7", "eventemitter2": "^6.4.9", + "evolution-manager": "latest", "exiftool-vendored": "^22.0.0", "express": "^4.18.2", "express-async-errors": "^3.1.1", diff --git a/src/whatsapp/controllers/views.controller.ts b/src/whatsapp/controllers/views.controller.ts deleted file mode 100644 index 7e15dfe7d..000000000 --- a/src/whatsapp/controllers/views.controller.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Request, Response } from 'express'; - -import { Auth, ConfigService, HttpServer } from '../../config/env.config'; -import { HttpStatus } from '../routers/index.router'; -import { WAMonitoringService } from '../services/monitor.service'; - -export class ViewsController { - constructor(private readonly waMonitor: WAMonitoringService, private readonly configService: ConfigService) {} - - public async manager(request: Request, response: Response) { - try { - const token = this.configService.get('AUTHENTICATION').API_KEY.KEY; - const port = this.configService.get('SERVER').PORT; - - const instances = await this.waMonitor.instanceInfo(); - - console.log('INSTANCES: ', instances); - return response.status(HttpStatus.OK).render('manager', { token, port, instances }); - } catch (error) { - console.log('ERROR: ', error); - } - } -} diff --git a/src/whatsapp/routers/view.router.ts b/src/whatsapp/routers/view.router.ts index 110027776..ecfe64c14 100644 --- a/src/whatsapp/routers/view.router.ts +++ b/src/whatsapp/routers/view.router.ts @@ -1,14 +1,32 @@ import { Router } from 'express'; +import fs from 'fs'; +import mime from 'mime-types'; import { RouterBroker } from '../abstract/abstract.router'; -import { viewsController } from '../whatsapp.module'; export class ViewsRouter extends RouterBroker { constructor() { super(); - this.router.get('/', (req, res) => { - return viewsController.manager(req, res); + const basePath = 'evolution-manager/dist'; + + const indexPath = require.resolve(`${basePath}/index.html`); + + this.router.get('/*', (req, res) => { + try { + const pathname = req.url.split('?')[0]; + + // verify if url is a file in dist folder + if (pathname === '/') throw {}; + const filePath = require.resolve(`${basePath}${pathname}`); + + const contentType = mime.lookup(filePath) || 'text/plain'; + res.set('Content-Type', contentType); + res.end(fs.readFileSync(filePath)); + } catch { + res.set('Content-Type', 'text/html'); + res.send(fs.readFileSync(indexPath)); + } }); } diff --git a/src/whatsapp/whatsapp.module.ts b/src/whatsapp/whatsapp.module.ts index 0bb9409fd..d64d2532c 100644 --- a/src/whatsapp/whatsapp.module.ts +++ b/src/whatsapp/whatsapp.module.ts @@ -14,7 +14,6 @@ import { SendMessageController } from './controllers/sendMessage.controller'; import { SettingsController } from './controllers/settings.controller'; import { SqsController } from './controllers/sqs.controller'; import { TypebotController } from './controllers/typebot.controller'; -import { ViewsController } from './controllers/views.controller'; import { WebhookController } from './controllers/webhook.controller'; import { WebsocketController } from './controllers/websocket.controller'; import { @@ -154,7 +153,6 @@ export const instanceController = new InstanceController( typebotService, cache, ); -export const viewsController = new ViewsController(waMonitor, configService); export const sendMessageController = new SendMessageController(waMonitor); export const chatController = new ChatController(waMonitor); export const groupController = new GroupController(waMonitor);