From 3e7a847c5a8d1365b77ebf840169c1a9058f8640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9=20=D0=92=D0=B8?= =?UTF-8?q?=D0=B3=D0=B4=D0=BE=D1=80=D0=BE=D0=B2?= Date: Fri, 7 Mar 2025 00:30:49 +0300 Subject: [PATCH] add env --- package.json | 2 ++ src/consts.ts | 19 +++++++++++++++---- src/main.ts | 40 +++++++++++++++++++++------------------- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 725e73c..e863fb4 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,8 @@ "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", + "_example_imageBuild": "docker buildx build --platform linux/amd64 -t vigdorov/image-back:0.0.1-amd64 .", + "_example_imagePush": "docker push vigdorov/image-back:0.0.1-amd64", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", diff --git a/src/consts.ts b/src/consts.ts index 8e4c624..699e7e8 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -1,11 +1,22 @@ -export const MONGO_URL = 'mongodb://localhost:27017'; +// Подключение к MongoDB +export const MONGO_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017'; -export const DB_AUTHORS = 'image-back-authors'; -export const DB_IMAGES = 'image-back-images'; +// Имя базы данных для пользователей и хранилищ +export const DB_AUTHORS = process.env.DB_AUTHORS || 'image-back-authors'; +export const DB_IMAGES = process.env.DB_IMAGES || 'image-back-images'; +// Порт приложения +export const APP_PORT = parseInt(process.env.APP_PORT || '3000', 10); + +// Другие константы export const APP_CONTROLLER = 'image-app'; -export const ALLOW_ORIGIN_ALL: [string, string] = ['Access-Control-Allow-Origin', '*']; +// CORS настройки +export const ALLOW_ORIGIN_ALL: [string, string] = [ + 'Access-Control-Allow-Origin', + process.env.CORS_ORIGIN || '*', + ]; + export const ALLOW_CREDENTIALS: [string, string] = ['Access-Control-Allow-Credentials', 'true']; export const CONTENT_LENGTH: [string, string] = ['Content-Length', '0']; export const ALLOW_METHOD: [string, string] = ['Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE']; diff --git a/src/main.ts b/src/main.ts index 4b74724..4564d3f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,26 +1,28 @@ -import {NestFactory} from '@nestjs/core'; -import {SwaggerModule, DocumentBuilder} from '@nestjs/swagger'; -import {AppModule} from './app.module'; -import {APP_CONTROLLER} from './consts'; +import { NestFactory } from '@nestjs/core'; +import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; +import { AppModule } from './app.module'; +import { APP_CONTROLLER, APP_PORT } from './consts'; async function bootstrap() { - const app = await NestFactory.create(AppModule); + const app = await NestFactory.create(AppModule); - const options = new DocumentBuilder() - .addSecurity('apiKey', { - type: 'apiKey', - in: 'header', - name: 'Authorization', - }) - .setTitle('Image API') - .setDescription('API для работы с картинками') - .setVersion('1.0.0') - .addTag(APP_CONTROLLER) - .build(); + const options = new DocumentBuilder() + .addSecurity('apiKey', { + type: 'apiKey', + in: 'header', + name: 'Authorization', + }) + .setTitle('Image API') + .setDescription('API для работы с картинками') + .setVersion('1.0.0') + .addTag(APP_CONTROLLER) + .build(); - const document = SwaggerModule.createDocument(app, options); - SwaggerModule.setup('api', app, document); + const document = SwaggerModule.createDocument(app, options); + SwaggerModule.setup('api', app, document); - await app.listen(3000); + console.log(`Application is starting on port ${APP_PORT}`); + + await app.listen(APP_PORT); } bootstrap();