feat: env to hide index and manager

This commit is contained in:
Gabriel Pastori 2023-09-09 15:29:53 +00:00
parent cd0da914f4
commit d9629157dd
3 changed files with 24 additions and 6 deletions

View File

@ -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(',') || ['*'],

View File

@ -9,6 +9,8 @@ SERVER:
TYPE: http # https
PORT: 8080 # 443
URL: localhost
HIDE_INDEX: false
HIDE_MANAGER: false
CORS:
ORIGIN:

View File

@ -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<Auth>('AUTHENTICATION').TYPE;
const httpServer = configService.get<HttpServer>('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)