From d9629157ddb5295446c432251c441bde401e1846 Mon Sep 17 00:00:00 2001 From: Gabriel Pastori <58153955+gabrielpastori1@users.noreply.github.com> Date: Sat, 9 Sep 2023 15:29:53 +0000 Subject: [PATCH] feat: env to hide index and manager --- src/config/env.config.ts | 10 +++++++++- src/dev-env.yml | 2 ++ src/whatsapp/routers/index.router.ts | 18 +++++++++++++----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index e4995aab..1e0a5379 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -3,7 +3,13 @@ import { readFileSync } from 'fs'; import { load } from 'js-yaml'; import { join } from 'path'; -export type HttpServer = { TYPE: 'http' | 'https'; PORT: number; URL: string }; +export type HttpServer = { + TYPE: 'http' | 'https'; + PORT: number; + URL: string; + HIDE_INDEX: boolean; + HIDE_MANAGER: boolean; +}; export type HttpMethods = 'POST' | 'GET' | 'PUT' | 'DELETE'; export type Cors = { @@ -173,6 +179,8 @@ export class ConfigService { TYPE: process.env.SERVER_TYPE as 'http' | 'https', PORT: Number.parseInt(process.env.SERVER_PORT) || 8080, URL: process.env.SERVER_URL, + HIDE_INDEX: process.env?.SERVER_HIDE_INDEX === 'true', + HIDE_MANAGER: process.env?.SERVER_HIDE_MANAGER === 'true', }, CORS: { ORIGIN: process.env.CORS_ORIGIN.split(',') || ['*'], diff --git a/src/dev-env.yml b/src/dev-env.yml index e670f1ec..77331d37 100644 --- a/src/dev-env.yml +++ b/src/dev-env.yml @@ -9,6 +9,8 @@ SERVER: TYPE: http # https PORT: 8080 # 443 URL: localhost + HIDE_INDEX: false + HIDE_MANAGER: false CORS: ORIGIN: diff --git a/src/whatsapp/routers/index.router.ts b/src/whatsapp/routers/index.router.ts index f67c936a..9ace3366 100644 --- a/src/whatsapp/routers/index.router.ts +++ b/src/whatsapp/routers/index.router.ts @@ -1,7 +1,7 @@ import { Router } from 'express'; import fs from 'fs'; -import { Auth, configService } from '../../config/env.config'; +import { Auth, configService, HttpServer } from '../../config/env.config'; import { authGuard } from '../guards/auth.guard'; import { instanceExistsGuard, instanceLoggedGuard } from '../guards/instance.guard'; import { ChamaaiRouter } from './chamaai.router'; @@ -30,19 +30,27 @@ enum HttpStatus { const router = Router(); const authType = configService.get('AUTHENTICATION').TYPE; +const httpServer = configService.get('SERVER'); + const guards = [instanceExistsGuard, instanceLoggedGuard, authGuard[authType]]; const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); -router - .get('/', (req, res) => { +// Hide index if needed + +if (!httpServer.HIDE_INDEX) + router.get('/', (req, res) => { res.status(HttpStatus.OK).json({ status: HttpStatus.OK, message: 'Welcome to the Evolution API, it is working!', version: packageJson.version, }); - }) - .use('/instance', new InstanceRouter(configService, ...guards).router) + }); + +// Hide manager if needed +if (!httpServer.HIDE_MANAGER) router.use('/manager', new ViewsRouter().router); + +router .use('/manager', new ViewsRouter().router) .use('/message', new MessageRouter(...guards).router) .use('/chat', new ChatRouter(...guards).router)