diff --git a/.env b/.env index 5ffdcb0..2575902 100644 --- a/.env +++ b/.env @@ -28,6 +28,9 @@ COOKIE_SAME_SITE=lax RANGES_API_URL=http://192.168.2.39:9999 RANGES_API_ENDPOINT=/api/ranges/9999 +FORMULA_API_URL=http://192.168.2.39:9999 +FORMULA_API_ENDPOINT=/api/integration/7777 + # ClickHouse CLICKHOUSE_HOST=http://192.168.2.37:8123 CLICKHOUSE_USER=vlad diff --git a/src/main.ts b/src/main.ts index a97b0c4..e3064aa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -42,7 +42,7 @@ async function bootstrap() { }); // Настройка CORS - app.enableCors({//ПОСТАВИТЬ ПРОКСИ, ЧТОБЫ КОРС НЕ РУГАЛСЯ, ИЗМЕНЕНИЕ ПОЛИТИКИ СЕТЕВЫХ ПАКЕТОВ. ПИШУ IP СВОЙ, А ПОРТ ПРОКСИ. REVERSE PROXY. + app.enableCors({ origin: [process.env.FRONTEND_URL, "http://dev.msf.enode"], credentials: true, methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', diff --git a/src/menu/formula.controller.ts b/src/menu/formula.controller.ts new file mode 100644 index 0000000..65b871a --- /dev/null +++ b/src/menu/formula.controller.ts @@ -0,0 +1,48 @@ +import { Controller, Get, Post, Body, HttpException, HttpStatus, Param } from '@nestjs/common'; +import { FormulaService } from './formula.service'; +import { MenuService } from './menu.service'; + +@Controller('formula') +export class FormulaController { + constructor( + private readonly FormulaService: FormulaService, + private readonly menuService: MenuService + ) { } + + @Get(':id') + async getFormulaData(@Param('id') id: string) { + try { + return await this.FormulaService.getFormulaData(id); + } catch (error) { + throw new HttpException('Failed to fetch Formula data', HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @Post(':id/update') + async updateFormulaData( + @Param('id') id: string, + @Body() data: any + ) { + if (!data) { + throw new HttpException('Invalid data format', HttpStatus.BAD_REQUEST); + } + + try { + const result = await this.FormulaService.updateFormulaData(id, data); + this.menuService.invalidateCache(); + return result; + } catch (error) { + throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + // OPTIONS метод для получения данных (как в вашем примере curl) + @Get(':id/options') + async getFormulaOptions(@Param('id') id: string) { + try { + return await this.FormulaService.getFormulaOptions(id); + } catch (error) { + throw new HttpException('Failed to fetch Formula options', HttpStatus.INTERNAL_SERVER_ERROR); + } + } +} \ No newline at end of file diff --git a/src/menu/formula.service.ts b/src/menu/formula.service.ts new file mode 100644 index 0000000..e83bd38 --- /dev/null +++ b/src/menu/formula.service.ts @@ -0,0 +1,92 @@ +import { Injectable } from '@nestjs/common'; +import { HttpService } from '@nestjs/axios'; +import { firstValueFrom } from 'rxjs'; +import { ConfigService } from '@nestjs/config'; + +@Injectable() +export class FormulaService { + private readonly FormulaApiUrl: string; + private readonly FormulaApiEndpoint: string; + + constructor( + private readonly httpService: HttpService, + private readonly configService: ConfigService + ) { + this.FormulaApiUrl = this.configService.get('FORMULA_API_URL', 'http://192.168.2.39:9999'); + this.FormulaApiEndpoint = this.configService.get('FORMULA_API_ENDPOINT', '/api/integration/7777'); + } + + async getFormulaData(id: string): Promise { + try { + const response = await firstValueFrom( + this.httpService.get(`${this.FormulaApiUrl}${this.FormulaApiEndpoint}/${id}`, { + headers: { + 'Accept': 'application/json' + } + }) + ); + + return response.data; + } catch (error) { + console.error('Failed to fetch Formula data:', error); + this.handleError(error); + return {}; + } + } + + async getFormulaOptions(id: string): Promise { + try { + const url = `${this.FormulaApiUrl}${this.FormulaApiEndpoint}`; + console.log('Fetching Formula options via OPTIONS:', url); + + const response = await firstValueFrom( + this.httpService.request({ + method: 'OPTIONS', + url, + headers: { 'Accept': 'application/json' } + }) + ); + + console.log('Response from Formula API:', response.data); + return response.data; + } catch (error) { + console.error('Failed to fetch Formula options:', error); + this.handleError(error); + return []; + } + } + + async updateFormulaData(id: string, data: any) { + try { + const response = await firstValueFrom( + this.httpService.post( + `${this.FormulaApiUrl}${this.FormulaApiEndpoint}/${id}`, + data, + { + headers: { + 'Content-Type': 'application/json' + }, + } + ) + ); + return response.data; + } catch (error) { + console.error('Failed to update Formula data:', error); + this.handleError(error); + throw new Error('Failed to update Formula data'); + } + } + + private handleError(error: any): void { + if (error.response) { + console.error('Server responded with:', { + status: error.response.status, + data: error.response.data + }); + } else if (error.request) { + console.error('No response received:', error.request); + } else { + console.error('Request setup error:', error.message); + } + } +} \ No newline at end of file diff --git a/src/menu/menu.module.ts b/src/menu/menu.module.ts index 403c187..82ad5af 100644 --- a/src/menu/menu.module.ts +++ b/src/menu/menu.module.ts @@ -5,10 +5,12 @@ import { MenuService } from './menu.service'; import { PrometheusModule } from '../prometheus/prometheus.module'; import { RangeService } from './range.service'; import { RangeController } from './range.controller'; +import { FormulaController } from './formula.controller'; +import { FormulaService } from './formula.service'; @Module({ imports: [PrometheusModule, HttpModule], - controllers: [MenuController, RangeController], - providers: [MenuService, RangeService] + controllers: [MenuController, RangeController, FormulaController], + providers: [MenuService, RangeService, FormulaService] }) export class MenuModule { } \ No newline at end of file diff --git a/src/prometheus/metrics.gateway.ts b/src/prometheus/metrics.gateway.ts index af8e1c7..8d9f875 100644 --- a/src/prometheus/metrics.gateway.ts +++ b/src/prometheus/metrics.gateway.ts @@ -56,13 +56,14 @@ export class MetricsGateway implements OnModuleInit, OnModuleDestroy { ); const wsPort = Number(this.configService.get('WS_PORT') || 3001); - this.httpServer.listen(wsPort, () => { + const wsHost = this.configService.get('WS_HOST') || '0.0.0.0'; + + this.httpServer.listen(wsPort, wsHost, () => { this.logger.log( - `WebSocket server running at ws://localhost:${wsPort}/metrics-ws` + `WebSocket server running at ws://${wsHost}:${wsPort}/metrics-ws` ); }); } - onModuleDestroy() { // Очистка всех ресурсов this.clearAllSubscriptions();