fix: lỗi đọc nhầm .env ở thư mục backend
This commit is contained in:
Vendored
+14
-25
@@ -49,24 +49,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.CommentGateway = exports.EmailService = exports.TourRoleGuard = exports.Roles = exports.ROLES_KEY = void 0;
|
exports.CommentGateway = exports.EmailService = exports.TourRoleGuard = exports.Roles = exports.ROLES_KEY = void 0;
|
||||||
const dotenv = __importStar(require("dotenv"));
|
|
||||||
const path = __importStar(require("path"));
|
const path = __importStar(require("path"));
|
||||||
const fs = __importStar(require("fs"));
|
const fs = __importStar(require("fs"));
|
||||||
const possibleEnvPaths = [
|
const config_1 = require("@nestjs/config");
|
||||||
path.resolve(process.cwd(), '.env'),
|
|
||||||
path.resolve(process.cwd(), '..', '.env'),
|
|
||||||
path.resolve(__dirname, '..', '.env'),
|
|
||||||
path.resolve(__dirname, '..', '..', '.env'),
|
|
||||||
];
|
|
||||||
let loadedEnvPath = null;
|
|
||||||
for (const p of possibleEnvPaths) {
|
|
||||||
if (fs.existsSync(p)) {
|
|
||||||
dotenv.config({ path: p });
|
|
||||||
console.log(`[Config] 📂 Biến môi trường được tải từ: ${p}`);
|
|
||||||
loadedEnvPath = p;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
require("reflect-metadata");
|
require("reflect-metadata");
|
||||||
const zlib = __importStar(require("zlib"));
|
const zlib = __importStar(require("zlib"));
|
||||||
const util_1 = require("util");
|
const util_1 = require("util");
|
||||||
@@ -98,12 +83,6 @@ const CACHE_TTL = {
|
|||||||
USER_ROLE: 300000,
|
USER_ROLE: 300000,
|
||||||
};
|
};
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
if (!process.env.DATABASE_URL) {
|
|
||||||
throw new Error('❌ DATABASE_URL không tồn tại trong biến môi trường! Kiểm tra file .env tại: ' + (loadedEnvPath || 'một trong các đường dẫn đã thử: ' + possibleEnvPaths.join(', ')));
|
|
||||||
}
|
|
||||||
console.log('====================================');
|
|
||||||
console.log('DATABASE_URL:', process.env.DATABASE_URL);
|
|
||||||
console.log('====================================');
|
|
||||||
const app = await core_1.NestFactory.create(AppModule);
|
const app = await core_1.NestFactory.create(AppModule);
|
||||||
app.setGlobalPrefix('api/v1');
|
app.setGlobalPrefix('api/v1');
|
||||||
app.enableCors();
|
app.enableCors();
|
||||||
@@ -293,9 +272,10 @@ AppController = __decorate([
|
|||||||
(0, common_1.Controller)()
|
(0, common_1.Controller)()
|
||||||
], AppController);
|
], AppController);
|
||||||
let AuthController = class AuthController {
|
let AuthController = class AuthController {
|
||||||
constructor(prisma, jwtService, emailService, cacheManager) {
|
constructor(prisma, jwtService, configService, emailService, cacheManager) {
|
||||||
this.prisma = prisma;
|
this.prisma = prisma;
|
||||||
this.jwtService = jwtService;
|
this.jwtService = jwtService;
|
||||||
|
this.configService = configService;
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
this.cacheManager = cacheManager;
|
this.cacheManager = cacheManager;
|
||||||
}
|
}
|
||||||
@@ -395,9 +375,10 @@ __decorate([
|
|||||||
], AuthController.prototype, "signupVerify", null);
|
], AuthController.prototype, "signupVerify", null);
|
||||||
AuthController = __decorate([
|
AuthController = __decorate([
|
||||||
(0, common_1.Controller)('auth'),
|
(0, common_1.Controller)('auth'),
|
||||||
__param(3, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
|
__param(4, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
|
||||||
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
|
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
|
||||||
jwt_1.JwtService,
|
jwt_1.JwtService,
|
||||||
|
config_1.ConfigService,
|
||||||
EmailService, Object])
|
EmailService, Object])
|
||||||
], AuthController);
|
], AuthController);
|
||||||
let PublicTourController = class PublicTourController {
|
let PublicTourController = class PublicTourController {
|
||||||
@@ -1818,6 +1799,11 @@ let AppModule = class AppModule {
|
|||||||
AppModule = __decorate([
|
AppModule = __decorate([
|
||||||
(0, common_1.Module)({
|
(0, common_1.Module)({
|
||||||
imports: [
|
imports: [
|
||||||
|
config_1.ConfigModule.forRoot({
|
||||||
|
isGlobal: true,
|
||||||
|
envFilePath: path.resolve(__dirname, '..', '..', '.env'),
|
||||||
|
ignoreEnvFile: process.env.NODE_ENV === 'production',
|
||||||
|
}),
|
||||||
cache_manager_1.CacheModule.registerAsync({
|
cache_manager_1.CacheModule.registerAsync({
|
||||||
isGlobal: true,
|
isGlobal: true,
|
||||||
useFactory: async () => ({
|
useFactory: async () => ({
|
||||||
@@ -1833,11 +1819,14 @@ AppModule = __decorate([
|
|||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
controllers: [AppController, AuthController, PublicTourController, TourController, UserController, RoutingController, LegController, LocationController, CommentController, PhotoController, AdminOtpController],
|
controllers: [AppController, AuthController, PublicTourController, TourController, UserController, RoutingController, LegController, LocationController, CommentController, PhotoController, AdminOtpController],
|
||||||
providers: [prisma_service_1.PrismaService, jwt_strategy_1.JwtStrategy, TourRoleGuard, jwt_auth_guard_1.JwtAuthGuard, admin_guard_1.AdminGuard, CommentGateway, core_2.Reflector, EmailService],
|
providers: [prisma_service_1.PrismaService, jwt_strategy_1.JwtStrategy, TourRoleGuard, jwt_auth_guard_1.JwtAuthGuard, admin_guard_1.AdminGuard, CommentGateway, core_2.Reflector, EmailService, config_1.ConfigService],
|
||||||
exports: [prisma_service_1.PrismaService]
|
exports: [prisma_service_1.PrismaService]
|
||||||
})
|
})
|
||||||
], AppModule);
|
], AppModule);
|
||||||
bootstrap().catch(err => {
|
bootstrap().catch(err => {
|
||||||
|
if (err.message.includes('DATABASE_URL')) {
|
||||||
|
console.error('❌ Lỗi nghiêm trọng: Biến môi trường DATABASE_URL không được tải. Hãy chắc chắn rằng file .env tồn tại ở thư mục gốc của dự án và chứa giá trị này.');
|
||||||
|
}
|
||||||
console.error('💥 Lỗi khởi động Server:');
|
console.error('💥 Lỗi khởi động Server:');
|
||||||
console.error(err);
|
console.error(err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -21,6 +21,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nestjs/cache-manager": "^3.1.3",
|
"@nestjs/cache-manager": "^3.1.3",
|
||||||
"@nestjs/common": "^11.1.27",
|
"@nestjs/common": "^11.1.27",
|
||||||
|
"@nestjs/config": "^4.0.4",
|
||||||
"@nestjs/core": "^11.1.27",
|
"@nestjs/core": "^11.1.27",
|
||||||
"@nestjs/jwt": "^11.0.2",
|
"@nestjs/jwt": "^11.0.2",
|
||||||
"@nestjs/passport": "^11.0.5",
|
"@nestjs/passport": "^11.0.5",
|
||||||
|
|||||||
+17
-28
@@ -1,24 +1,6 @@
|
|||||||
import * as dotenv from 'dotenv';
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
// Tự động tìm kiếm file .env ở nhiều vị trí để đảm bảo tải đúng trong môi trường monorepo
|
|
||||||
const possibleEnvPaths = [
|
|
||||||
path.resolve(process.cwd(), '.env'),
|
|
||||||
path.resolve(process.cwd(), '..', '.env'),
|
|
||||||
path.resolve(__dirname, '..', '.env'),
|
|
||||||
path.resolve(__dirname, '..', '..', '.env'),
|
|
||||||
];
|
|
||||||
|
|
||||||
let loadedEnvPath: string | null = null;
|
|
||||||
for (const p of possibleEnvPaths) {
|
|
||||||
if (fs.existsSync(p)) {
|
|
||||||
dotenv.config({ path: p });
|
|
||||||
console.log(`[Config] 📂 Biến môi trường được tải từ: ${p}`);
|
|
||||||
loadedEnvPath = p;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
import 'reflect-metadata';
|
import 'reflect-metadata';
|
||||||
import * as zlib from 'zlib';
|
import * as zlib from 'zlib';
|
||||||
@@ -62,14 +44,6 @@ const CACHE_TTL = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
if (!process.env.DATABASE_URL) {
|
|
||||||
throw new Error('❌ DATABASE_URL không tồn tại trong biến môi trường! Kiểm tra file .env tại: ' + (loadedEnvPath || 'một trong các đường dẫn đã thử: ' + possibleEnvPaths.join(', ')));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Kiểm tra log xem biến môi trường đã nhận đúng chưa
|
|
||||||
console.log('====================================');
|
|
||||||
console.log('DATABASE_URL:', process.env.DATABASE_URL);
|
|
||||||
console.log('====================================');
|
|
||||||
|
|
||||||
// Chuyển sang dùng NestExpressApplication để cấu hình static assets
|
// Chuyển sang dùng NestExpressApplication để cấu hình static assets
|
||||||
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
||||||
@@ -279,6 +253,11 @@ class AuthController {
|
|||||||
constructor(
|
constructor(
|
||||||
private prisma: PrismaService,
|
private prisma: PrismaService,
|
||||||
private jwtService: JwtService,
|
private jwtService: JwtService,
|
||||||
|
// Inject ConfigService để đọc biến môi trường một cách an toàn
|
||||||
|
// NestJS sẽ đảm bảo ConfigModule được tải trước khi AuthController được khởi tạo
|
||||||
|
// Do đó, các biến môi trường sẽ luôn có sẵn ở đây.
|
||||||
|
// Điều này cũng áp dụng cho EmailService.
|
||||||
|
private configService: ConfigService,
|
||||||
private emailService: EmailService,
|
private emailService: EmailService,
|
||||||
@Inject(CACHE_MANAGER) private cacheManager: Cache
|
@Inject(CACHE_MANAGER) private cacheManager: Cache
|
||||||
) {}
|
) {}
|
||||||
@@ -1716,6 +1695,13 @@ class AdminOtpController {
|
|||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
ConfigModule.forRoot({
|
||||||
|
isGlobal: true, // Giúp ConfigModule có sẵn ở mọi nơi trong ứng dụng
|
||||||
|
// Chỉ định đường dẫn tới file .env ở thư mục gốc của dự án
|
||||||
|
envFilePath: path.resolve(__dirname, '..', '..', '.env'),
|
||||||
|
// Bỏ qua lỗi nếu không tìm thấy file .env (hữu ích cho môi trường production dùng biến hệ thống)
|
||||||
|
ignoreEnvFile: process.env.NODE_ENV === 'production',
|
||||||
|
}),
|
||||||
CacheModule.registerAsync({
|
CacheModule.registerAsync({
|
||||||
isGlobal: true,
|
isGlobal: true,
|
||||||
useFactory: async () => ({
|
useFactory: async () => ({
|
||||||
@@ -1731,13 +1717,16 @@ class AdminOtpController {
|
|||||||
}) as any,
|
}) as any,
|
||||||
],
|
],
|
||||||
controllers: [AppController, AuthController, PublicTourController, TourController, UserController, RoutingController, LegController, LocationController, CommentController, PhotoController, AdminOtpController],
|
controllers: [AppController, AuthController, PublicTourController, TourController, UserController, RoutingController, LegController, LocationController, CommentController, PhotoController, AdminOtpController],
|
||||||
providers: [PrismaService, JwtStrategy, TourRoleGuard, JwtAuthGuard, AdminGuard, CommentGateway, Reflector, EmailService],
|
providers: [PrismaService, JwtStrategy, TourRoleGuard, JwtAuthGuard, AdminGuard, CommentGateway, Reflector, EmailService, ConfigService],
|
||||||
exports: [PrismaService]
|
exports: [PrismaService]
|
||||||
})
|
})
|
||||||
class AppModule {}
|
class AppModule {}
|
||||||
|
|
||||||
|
|
||||||
bootstrap().catch(err => {
|
bootstrap().catch(err => {
|
||||||
|
if (err.message.includes('DATABASE_URL')) {
|
||||||
|
console.error('❌ Lỗi nghiêm trọng: Biến môi trường DATABASE_URL không được tải. Hãy chắc chắn rằng file .env tồn tại ở thư mục gốc của dự án và chứa giá trị này.');
|
||||||
|
}
|
||||||
console.error('💥 Lỗi khởi động Server:');
|
console.error('💥 Lỗi khởi động Server:');
|
||||||
console.error(err);
|
console.error(err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|||||||
Generated
+55
@@ -25,6 +25,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nestjs/cache-manager": "^3.1.3",
|
"@nestjs/cache-manager": "^3.1.3",
|
||||||
"@nestjs/common": "^11.1.27",
|
"@nestjs/common": "^11.1.27",
|
||||||
|
"@nestjs/config": "^4.0.4",
|
||||||
"@nestjs/core": "^11.1.27",
|
"@nestjs/core": "^11.1.27",
|
||||||
"@nestjs/jwt": "^11.0.2",
|
"@nestjs/jwt": "^11.0.2",
|
||||||
"@nestjs/passport": "^11.0.5",
|
"@nestjs/passport": "^11.0.5",
|
||||||
@@ -2075,6 +2076,60 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@nestjs/config": {
|
||||||
|
"version": "4.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/@nestjs/config/-/config-4.0.4.tgz",
|
||||||
|
"integrity": "sha512-CJPjNitr0bAufSEnRe2N+JbnVmMmDoo6hvKCPzXgZoGwJSmp/dZPk9f/RMbuD/+Q1ZJPjwsRpq0vxna++Knwow==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"dotenv": "17.4.1",
|
||||||
|
"dotenv-expand": "12.0.3",
|
||||||
|
"lodash": "4.18.1"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
||||||
|
"rxjs": "^7.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@nestjs/config/node_modules/dotenv": {
|
||||||
|
"version": "17.4.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.4.1.tgz",
|
||||||
|
"integrity": "sha512-k8DaKGP6r1G30Lx8V4+pCsLzKr8vLmV2paqEj1Y55GdAgJuIqpRp5FfajGF8KtwMxCz9qJc6wUIJnm053d/WCw==",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://dotenvx.com"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@nestjs/config/node_modules/dotenv-expand": {
|
||||||
|
"version": "12.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-12.0.3.tgz",
|
||||||
|
"integrity": "sha512-uc47g4b+4k/M/SeaW1y4OApx+mtLWl92l5LMPP0GNXctZqELk+YGgOPIIC5elYmUH4OuoK3JLhuRUYegeySiFA==",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"dependencies": {
|
||||||
|
"dotenv": "^16.4.5"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://dotenvx.com"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@nestjs/config/node_modules/dotenv-expand/node_modules/dotenv": {
|
||||||
|
"version": "16.6.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.6.1.tgz",
|
||||||
|
"integrity": "sha512-uBq4egWHTcTt33a72vpSG0z3HnPuIl6NqYcTrKEg2azoEyl2hpW0zqlxysq2pK9HlDIHyHyakeYaYnSAwd8bow==",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://dotenvx.com"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@nestjs/core": {
|
"node_modules/@nestjs/core": {
|
||||||
"version": "11.1.27",
|
"version": "11.1.27",
|
||||||
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-11.1.27.tgz",
|
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-11.1.27.tgz",
|
||||||
|
|||||||
Reference in New Issue
Block a user