Skip to content

Commit 39fa5f8

Browse files
DangDang
authored andcommitted
Fix MongoDB connection string configuration for Docker deployment
- Add environment variable mapping for MongoDB connection string - Improve InfrastructureModule with fallback connection string handling - Add comprehensive environment variable mapping in Program.cs - Update environment template with Render.com deployment instructions - Resolve MongoDB connection string empty error in production
1 parent 2be730c commit 39fa5f8

File tree

4 files changed

+117
-4
lines changed

4 files changed

+117
-4
lines changed

VehicleShowroomManagement/CONFIGURATION_FIX.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Create a `.env` file in the project root with the following variables:
2626

2727
```bash
28-
# MongoDB Connection
28+
# MongoDB Connection (REQUIRED)
2929
MONGODB_CONNECTION_STRING=mongodb://localhost:27017/VehicleShowroomManagement
3030

3131
# JWT Configuration (REQUIRED)
@@ -53,6 +53,17 @@ DOTNET_RUNNING_IN_CONTAINER=false
5353
PORT=10000
5454
```
5555

56+
### For Render.com Deployment
57+
58+
Set these environment variables in your Render.com dashboard:
59+
60+
1. **MongoDB**: `MONGODB_CONNECTION_STRING=mongodb+srv://username:[email protected]/VehicleShowroomDB`
61+
2. **JWT**: `JWT_KEY=your-production-jwt-key-at-least-32-characters`
62+
3. **JWT Issuer**: `JWT_ISSUER=VehicleShowroomManagement`
63+
4. **JWT Audience**: `JWT_AUDIENCE=VehicleShowroomManagement-Users`
64+
5. **Email Settings**: Configure your SMTP settings
65+
6. **Cloudinary**: Set up your image hosting service
66+
5667
### Production Deployment
5768

5869
For production deployment (e.g., Render.com), set these environment variables:

VehicleShowroomManagement/env.template

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Vehicle Showroom Management Environment Configuration Template
22
# Copy this file to .env and fill in your actual values
33

4-
# MongoDB Connection
4+
# MongoDB Connection (REQUIRED)
55
MONGODB_CONNECTION_STRING=mongodb://localhost:27017/VehicleShowroomManagement
66

7-
# JWT Configuration
7+
# JWT Configuration (REQUIRED)
88
JWT_KEY=your-super-secret-jwt-key-that-is-at-least-32-characters-long-for-security
99
JWT_ISSUER=VehicleShowroomManagement
1010
JWT_AUDIENCE=VehicleShowroomManagement-Users
@@ -27,3 +27,16 @@ CLOUDINARY_API_SECRET=your-api-secret
2727
ASPNETCORE_ENVIRONMENT=Development
2828
DOTNET_RUNNING_IN_CONTAINER=false
2929
PORT=10000
30+
31+
# For Render.com deployment, set these environment variables in your dashboard:
32+
# MONGODB_CONNECTION_STRING=mongodb+srv://username:[email protected]/VehicleShowroomDB
33+
# JWT_KEY=your-production-jwt-key-at-least-32-characters
34+
# JWT_ISSUER=VehicleShowroomManagement
35+
# JWT_AUDIENCE=VehicleShowroomManagement-Users
36+
# EMAIL_SMTP_HOST=smtp.gmail.com
37+
# EMAIL_SMTP_PORT=587
38+
39+
# EMAIL_SMTP_PASSWORD=your-app-password
40+
# CLOUDINARY_CLOUD_NAME=your-cloud-name
41+
# CLOUDINARY_API_KEY=your-api-key
42+
# CLOUDINARY_API_SECRET=your-api-secret

VehicleShowroomManagement/src/Infrastructure/DependencyInjection/InfrastructureModule.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,22 @@ protected override void Load(ContainerBuilder builder)
2323
// Configure MongoDB class maps (must be done before any MongoDB operations)
2424
MongoDbClassMapConfiguration.Configure();
2525

26-
// MongoDB Configuration
26+
// MongoDB Configuration with fallback
2727
var connectionString = configuration.GetConnectionString("MongoDB");
28+
29+
// If connection string is empty, try to get from environment variables
30+
if (string.IsNullOrWhiteSpace(connectionString))
31+
{
32+
connectionString = Environment.GetEnvironmentVariable("MONGODB_CONNECTION_STRING")
33+
?? Environment.GetEnvironmentVariable("ConnectionStrings__MongoDB")
34+
?? "mongodb://localhost:27017/VehicleShowroomManagement"; // Fallback for development
35+
}
36+
37+
if (string.IsNullOrWhiteSpace(connectionString))
38+
{
39+
throw new InvalidOperationException("MongoDB connection string is not configured. Please set MONGODB_CONNECTION_STRING environment variable or ConnectionStrings:MongoDB in configuration.");
40+
}
41+
2842
var mongoClient = new MongoClient(connectionString);
2943
var database = mongoClient.GetDatabase("VehicleShowroomDB");
3044

VehicleShowroomManagement/src/WebAPI/Program.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,81 @@
4242

4343
var builder = WebApplication.CreateBuilder(args);
4444

45+
// Add environment variables to configuration
46+
builder.Configuration.AddEnvironmentVariables();
47+
48+
// Map environment variables to configuration keys
49+
var mongoConnectionString = Environment.GetEnvironmentVariable("MONGODB_CONNECTION_STRING");
50+
if (!string.IsNullOrWhiteSpace(mongoConnectionString))
51+
{
52+
builder.Configuration["ConnectionStrings:MongoDB"] = mongoConnectionString;
53+
Log.Information("MongoDB connection string loaded from environment variable");
54+
}
55+
56+
var jwtKey = Environment.GetEnvironmentVariable("JWT_KEY");
57+
if (!string.IsNullOrWhiteSpace(jwtKey))
58+
{
59+
builder.Configuration["Jwt:Key"] = jwtKey;
60+
Log.Information("JWT Key loaded from environment variable");
61+
}
62+
63+
var jwtIssuer = Environment.GetEnvironmentVariable("JWT_ISSUER");
64+
if (!string.IsNullOrWhiteSpace(jwtIssuer))
65+
{
66+
builder.Configuration["Jwt:Issuer"] = jwtIssuer;
67+
Log.Information("JWT Issuer loaded from environment variable");
68+
}
69+
70+
var jwtAudience = Environment.GetEnvironmentVariable("JWT_AUDIENCE");
71+
if (!string.IsNullOrWhiteSpace(jwtAudience))
72+
{
73+
builder.Configuration["Jwt:Audience"] = jwtAudience;
74+
Log.Information("JWT Audience loaded from environment variable");
75+
}
76+
77+
// Map other environment variables
78+
var emailHost = Environment.GetEnvironmentVariable("EMAIL_SMTP_HOST");
79+
if (!string.IsNullOrWhiteSpace(emailHost))
80+
{
81+
builder.Configuration["EmailSettings:SmtpHost"] = emailHost;
82+
}
83+
84+
var emailPort = Environment.GetEnvironmentVariable("EMAIL_SMTP_PORT");
85+
if (!string.IsNullOrWhiteSpace(emailPort))
86+
{
87+
builder.Configuration["EmailSettings:SmtpPort"] = emailPort;
88+
}
89+
90+
var emailUsername = Environment.GetEnvironmentVariable("EMAIL_SMTP_USERNAME");
91+
if (!string.IsNullOrWhiteSpace(emailUsername))
92+
{
93+
builder.Configuration["EmailSettings:SmtpUsername"] = emailUsername;
94+
}
95+
96+
var emailPassword = Environment.GetEnvironmentVariable("EMAIL_SMTP_PASSWORD");
97+
if (!string.IsNullOrWhiteSpace(emailPassword))
98+
{
99+
builder.Configuration["EmailSettings:SmtpPassword"] = emailPassword;
100+
}
101+
102+
var cloudinaryCloudName = Environment.GetEnvironmentVariable("CLOUDINARY_CLOUD_NAME");
103+
if (!string.IsNullOrWhiteSpace(cloudinaryCloudName))
104+
{
105+
builder.Configuration["CloudinarySettings:CloudName"] = cloudinaryCloudName;
106+
}
107+
108+
var cloudinaryApiKey = Environment.GetEnvironmentVariable("CLOUDINARY_API_KEY");
109+
if (!string.IsNullOrWhiteSpace(cloudinaryApiKey))
110+
{
111+
builder.Configuration["CloudinarySettings:ApiKey"] = cloudinaryApiKey;
112+
}
113+
114+
var cloudinaryApiSecret = Environment.GetEnvironmentVariable("CLOUDINARY_API_SECRET");
115+
if (!string.IsNullOrWhiteSpace(cloudinaryApiSecret))
116+
{
117+
builder.Configuration["CloudinarySettings:ApiSecret"] = cloudinaryApiSecret;
118+
}
119+
45120
// Configure environment-specific settings
46121
if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true")
47122
{

0 commit comments

Comments
 (0)