From 56e90a54d82d9c68ce6a30ad83b7789f16c4c879 Mon Sep 17 00:00:00 2001 From: Nikita Biziaev Date: Fri, 3 Jul 2026 12:54:23 +0500 Subject: [PATCH] CORS problem solution --- .env.example | 5 ++--- .env.local.example | 1 - README.md | 20 +++++++++++--------- docker-compose.yml | 1 - src/main.ts | 10 ++-------- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/.env.example b/.env.example index 6f3cf61..c304264 100644 --- a/.env.example +++ b/.env.example @@ -1,10 +1,9 @@ PORT=3101 branch= DATABASE_URL=postgres://greact:pG3526l4@194.36.208.86:5433/cloud -CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173 -# Optional. When set, POST /ingest requires x-api-key: . +# Optional. When set, POST /api/ingest requires x-api-key: . INGEST_API_KEY= -# Poll interval for GET /current/events SSE snapshots. +# Poll interval for GET /api/current/events SSE snapshots. CURRENT_EVENTS_POLL_MS=1000 diff --git a/.env.local.example b/.env.local.example index d52377c..c26de64 100644 --- a/.env.local.example +++ b/.env.local.example @@ -1,5 +1,4 @@ PORT=3101 DATABASE_URL=postgres://greact:pG3526l4@194.36.208.86:5433/cloud -CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173 INGEST_API_KEY= CURRENT_EVENTS_POLL_MS=1000 diff --git a/README.md b/README.md index acf07e8..a5cda8a 100644 --- a/README.md +++ b/README.md @@ -28,15 +28,17 @@ Historical chart ranges use continuous aggregates: ## API -- `GET /health` -- `GET /edge` -- `GET /tag?edge=edge5&search=pressure` -- `GET /current?edge=edge5&tags=tag1,tag2` -- `GET /history?edge=edge5&tags=tag1,tag2&from=2026-06-01T00:00:00Z&to=2026-06-02T00:00:00Z` -- `POST /ingest` -- `POST /ingest/:edge` +All routes are served under the `/api` prefix. -`POST /ingest` accepts one point: +- `GET /api/health` +- `GET /api/edge` +- `GET /api/tag?edge=edge5&search=pressure` +- `GET /api/current?edge=edge5&tags=tag1,tag2` +- `GET /api/history?edge=edge5&tags=tag1,tag2&from=2026-06-01T00:00:00Z&to=2026-06-02T00:00:00Z` +- `POST /api/ingest` +- `POST /api/ingest/:edge` + +`POST /api/ingest` accepts one point: ```json { @@ -47,7 +49,7 @@ Historical chart ranges use continuous aggregates: } ``` -`POST /ingest/:edge` accepts a compact edge payload: +`POST /api/ingest/:edge` accepts a compact edge payload: ```json { diff --git a/docker-compose.yml b/docker-compose.yml index bbcca62..1832e5c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,6 @@ services: NODE_ENV: production PORT: ${PORT} DATABASE_URL: ${DATABASE_URL} - CORS_ALLOWED_ORIGINS: ${CORS_ALLOWED_ORIGINS} INGEST_API_KEY: ${INGEST_API_KEY} CURRENT_EVENTS_POLL_MS: ${CURRENT_EVENTS_POLL_MS} expose: diff --git a/src/main.ts b/src/main.ts index f190323..d3efa04 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,17 +3,11 @@ import { NestFactory } from '@nestjs/core'; import compression from 'compression'; import { AppModule } from './app.module'; -/** Запускает HTTP API со сжатием, CORS и глобальной валидацией DTO. */ +/** Запускает HTTP API со сжатием и глобальной валидацией DTO. */ async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(compression()); - - app.enableCors({ - origin: process.env.CORS_ALLOWED_ORIGINS, - credentials: true, - methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], - allowedHeaders: ['Content-Type', 'Authorization', 'x-api-key'], - }); + app.setGlobalPrefix('api'); app.useGlobalPipes( new ValidationPipe({