diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bebe903..3809d59b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,6 @@ * Docker files adjusted * Fixed in the postman collection the webhookByEvent parameter by webhook_by_events * Added validations in create instance -* Now it's getting the API URL directly in the request, no longer need the variable in the env file * Removed link preview endpoint, now it's done automatically from sending conventional text * Added group membership validation before sending message to groups * Adjusts in Dockerfile diff --git a/Docker/.env.example b/Docker/.env.example index af8927d8..f8511732 100644 --- a/Docker/.env.example +++ b/Docker/.env.example @@ -1,3 +1,5 @@ +SERVER_URL='http://localhost:8080' # http://localhost:3333 | http://localhost:3333/api/v1 + CORS_ORIGIN='*' # Or separate by commas - ex.: 'yourdomain1.com, yourdomain2.com' CORS_METHODS='POST,GET,PUT,DELETE' CORS_CREDENTIALS=true diff --git a/Dockerfile b/Dockerfile index b05dc02e..088f6fa1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,8 @@ COPY ./package.json . ENV DOCKER_ENV=true +ENV SERVER_URL='http://localhost:8080' + ENV CORS_ORIGIN=* ENV CORS_METHODS=POST,GET,PUT,DELETE ENV CORS_CREDENTIALS=true diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 1c7cc9ce..7221f474 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -3,7 +3,7 @@ import { load } from 'js-yaml'; import { join } from 'path'; import { isBooleanString } from 'class-validator'; -export type HttpServer = { TYPE: 'http' | 'https'; PORT: number }; +export type HttpServer = { TYPE: 'http' | 'https'; PORT: number; URL: string }; export type HttpMethods = 'POST' | 'GET' | 'PUT' | 'DELETE'; export type Cors = { @@ -173,6 +173,7 @@ export class ConfigService { SERVER: { TYPE: process.env.SERVER_TYPE as 'http' | 'https', PORT: Number.parseInt(process.env.SERVER_PORT), + URL: process.env.SERVER_URL, }, CORS: { ORIGIN: process.env.CORS_ORIGIN.split(','), diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 4145acef..8bb35e35 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -28,21 +28,18 @@ export class InstanceController { private readonly logger = new Logger(InstanceController.name); - public async createInstance( - { - instanceName, - webhook, - webhook_by_events, - events, - qrcode, - token, - chatwoot_account_id, - chatwoot_token, - chatwoot_url, - chatwoot_sign_msg, - }: InstanceDto, - apiURL: string, - ) { + public async createInstance({ + instanceName, + webhook, + webhook_by_events, + events, + qrcode, + token, + chatwoot_account_id, + chatwoot_token, + chatwoot_url, + chatwoot_sign_msg, + }: InstanceDto) { this.logger.verbose('requested createInstance from ' + instanceName + ' instance'); const mode = this.configService.get('AUTHENTICATION').INSTANCE.MODE; @@ -143,7 +140,7 @@ export class InstanceController { throw new BadRequestException('Invalid "url" property in chatwoot'); } - const urlServer = apiURL; + const urlServer = this.configService.get('SERVER').URL; try { this.chatwootService.create(instance, { @@ -285,7 +282,7 @@ export class InstanceController { throw new BadRequestException('Invalid "url" property in chatwoot'); } - const urlServer = apiURL; + const urlServer = this.configService.get('SERVER').URL; try { this.chatwootService.create(instance, { diff --git a/src/whatsapp/routers/instance.router.ts b/src/whatsapp/routers/instance.router.ts index 7dc35d30..850ffebd 100644 --- a/src/whatsapp/routers/instance.router.ts +++ b/src/whatsapp/routers/instance.router.ts @@ -24,14 +24,11 @@ export class InstanceRouter extends RouterBroker { logger.verbose('request query: '); logger.verbose(req.query); - const apiURL = req.headers.host || req.hostname; - logger.verbose('API URL: ' + apiURL); - const response = await this.dataValidate({ request: req, schema: instanceNameSchema, ClassRef: InstanceDto, - execute: (instance) => instanceController.createInstance(instance, apiURL), + execute: (instance) => instanceController.createInstance(instance), }); return res.status(HttpStatus.CREATED).json(response);