Skip to content

Commit 19b5d76

Browse files
committed
feat: Add environment variable validation and update deployment guides
1 parent b2515b1 commit 19b5d76

File tree

6 files changed

+111
-1
lines changed

6 files changed

+111
-1
lines changed

DEPLOY_GUIDE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
### Step 1: Deploy API to Render
66

77
#### A. Sign up for Render
8+
89
1. Go to [render.com](https://render.com) and sign up
910
2. Connect your GitHub account
1011
3. Import your `ExpressLogistics` repository
1112

1213
#### B. Create Web Service
14+
1315
1. Click "New +" → "Web Service"
1416
2. Select your `ExpressLogistics` repository
1517
3. Configure the service:
@@ -25,6 +27,7 @@ Plan: Free (for testing)
2527
```
2628
2729
#### C. Add Environment Variables
30+
2831
In the Render dashboard, add these environment variables:
2932
3033
```env
@@ -46,6 +49,7 @@ FRONTEND_URL=https://your-app-name.vercel.app
4649
```
4750

4851
#### D. Deploy API
52+
4953
1. Click "Create Web Service"
5054
2. Wait for deployment (5-10 minutes)
5155
3. Note your API URL: `https://your-service-name.onrender.com`
@@ -55,10 +59,12 @@ FRONTEND_URL=https://your-app-name.vercel.app
5559
### Step 2: Deploy Client to Vercel
5660

5761
#### A. Sign up for Vercel
62+
5863
1. Go to [vercel.com](https://vercel.com) and sign up
5964
2. Import your `ExpressLogistics` repository
6065

6166
#### B. Configure Project
67+
6268
```yaml
6369
Framework Preset: Vite
6470
Root Directory: client
@@ -68,13 +74,15 @@ Install Command: npm install
6874
```
6975
7076
#### C. Add Environment Variables
77+
7178
```env
7279
VITE_API_URL=https://your-render-api-url.onrender.com/api
7380
VITE_CLOUDINARY_CLOUD_NAME=dk1cria0z
7481
VITE_CLOUDINARY_UPLOAD_PRESET=delivery_invoices
7582
```
7683

7784
#### D. Deploy Client
85+
7886
1. Click "Deploy"
7987
2. Wait for deployment (3-5 minutes)
8088
3. Note your app URL: `https://your-app-name.vercel.app`
@@ -84,24 +92,30 @@ VITE_CLOUDINARY_UPLOAD_PRESET=delivery_invoices
8492
### Step 3: Update Configuration
8593

8694
#### Update API with Client URL
95+
8796
1. Go back to Render dashboard
8897
2. Update `FRONTEND_URL` environment variable:
98+
8999
```env
90100
FRONTEND_URL=https://your-app-name.vercel.app
91101
```
102+
92103
3. Redeploy the API service
93104

94105
---
95106

96107
## Quick Test Commands
97108

98109
### Test API Health
110+
99111
```bash
100112
curl https://your-api-url.onrender.com/api/health
101113
```
102114

103115
### Test Client
116+
104117
Open your Vercel URL in browser and test:
118+
105119
1. Home page loads
106120
2. Tracking input works
107121
3. Admin login works
@@ -111,11 +125,13 @@ Open your Vercel URL in browser and test:
111125
## Alternative: One-Click Deploy
112126

113127
### Render Deploy Button
128+
114129
Click this button to deploy API instantly:
115130

116131
[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/RabbitDaCoder/ExpressLogistics)
117132

118133
### Vercel Deploy Button
134+
119135
Click this button to deploy Client instantly:
120136

121137
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/RabbitDaCoder/ExpressLogistics&root-directory=client&env=VITE_API_URL&envDescription=API%20endpoint%20URL&envLink=https://your-api.onrender.com/api)
@@ -127,15 +143,19 @@ Click this button to deploy Client instantly:
127143
### Common Issues
128144

129145
#### 1. API Build Fails with npm ci Error
146+
130147
**Solution:** The render.yaml is configured to use `npm install` instead of `npm ci`
131148

132149
#### 2. Client Can't Connect to API
150+
133151
**Solution:** Check CORS settings and update `FRONTEND_URL` in API environment variables
134152

135153
#### 3. Email Service Not Working
154+
136155
**Solution:** Verify SMTP credentials in Render environment variables
137156

138157
#### 4. PDF Generation Fails
158+
139159
**Solution:** Check Cloudinary credentials in both API and Client
140160

141161
---

RENDER_ENV_VARS.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copy and paste these environment variables in Render:
2+
3+
NODE_ENV=production
4+
PORT=10000
5+
MONGO_URI=mongodb+srv://techagbadev:7X7Vql0Q0t2gZVOv@aegisexpresslogistics.nj62fnk.mongodb.net/logisticsDB?retryWrites=true&w=majority&appName=AegisExpressLogistics
6+
MONGODB_URI=mongodb+srv://techagbadev:7X7Vql0Q0t2gZVOv@aegisexpresslogistics.nj62fnk.mongodb.net/logisticsDB?retryWrites=true&w=majority&appName=AegisExpressLogistics
7+
JWT_SECRET=your_secure_jwt_secret_change_this_in_production
8+
JWT_EXPIRES=7d
9+
REDIS_URL=rediss://default:AdZnAAIncDFkYzg1MTViMzVmMGI0MTFhYjhmZGQxMTczOTk5OTYzNnAxNTQ4ODc@full-skylark-54887.upstash.io:6379
10+
REDIS_CACHE_TTL=3600
11+
EMAIL_USER=techagbadev@gmail.com
12+
EMAIL_PASS=zimtqbuzrypfmkgk
13+
SMTP_HOST=smtp.gmail.com
14+
SMTP_PORT=465
15+
FROM_EMAIL=boltdropa@gmail.com
16+
CLOUDINARY_CLOUD_NAME=dk1cria0z
17+
CLOUDINARY_API_KEY=747368826295982
18+
CLOUDINARY_API_SECRET=HbljlQwEoDR6ndvo98KNQy6Lbyk
19+
FRONTEND_URL=https://aegis-express.vercel.app
20+
ADMIN_SEED_TOKEN=CHANGE_ME
21+
ADMIN_EMAIL=owner@example.com
22+
ADMIN_PASSWORD=Passw0rd!

VERCEL_ENV_VARS.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copy and paste these environment variables in Vercel:
2+
3+
VITE_API_URL=https://your-render-api-url.onrender.com/api
4+
VITE_CLOUDINARY_CLOUD_NAME=dk1cria0z
5+
VITE_CLOUDINARY_UPLOAD_PRESET=delivery_invoices

api/config/db.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ import mongoose from "mongoose";
22

33
const connectDB = async () => {
44
try {
5-
const conn = await mongoose.connect(process.env.MONGO_URI, {
5+
// Support both MONGO_URI and MONGODB_URI environment variable names
6+
const mongoUri = process.env.MONGO_URI || process.env.MONGODB_URI;
7+
8+
if (!mongoUri) {
9+
throw new Error(
10+
"MongoDB URI is not defined. Please set MONGO_URI or MONGODB_URI environment variable."
11+
);
12+
}
13+
14+
const conn = await mongoose.connect(mongoUri, {
615
useNewUrlParser: true,
716
useUnifiedTopology: true,
817
});

api/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import app from "./app.js";
66
import connectDB from "./config/db.js";
77
import { connectRedis } from "./config/redis.js";
88
import { seedAdminIfRequested } from "./controllers/auth.controller.js";
9+
import validateEnvironment from "./utils/validateEnv.js";
910

1011
const PORT = process.env.PORT || 5000;
1112

1213
const startServer = async () => {
1314
try {
15+
// Validate environment variables first
16+
validateEnvironment();
17+
1418
// connect MongoDB
1519
await connectDB();
1620

api/utils/validateEnv.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Environment variable validation for production deployment
2+
import dotenv from "dotenv";
3+
dotenv.config();
4+
5+
const requiredEnvVars = [
6+
"MONGO_URI",
7+
"JWT_SECRET",
8+
"REDIS_URL",
9+
"EMAIL_USER",
10+
"EMAIL_PASS",
11+
"CLOUDINARY_CLOUD_NAME",
12+
"CLOUDINARY_API_KEY",
13+
"CLOUDINARY_API_SECRET",
14+
];
15+
16+
const validateEnvironment = () => {
17+
console.log("🔍 Validating environment variables...");
18+
19+
const missing = [];
20+
21+
requiredEnvVars.forEach((varName) => {
22+
// Support alternative naming conventions
23+
const value =
24+
process.env[varName] ||
25+
process.env[varName.replace("MONGO_URI", "MONGODB_URI")] ||
26+
process.env[varName.replace("EMAIL_", "SMTP_")];
27+
28+
if (!value) {
29+
missing.push(varName);
30+
} else {
31+
console.log(`✅ ${varName}: ${value.substring(0, 20)}...`);
32+
}
33+
});
34+
35+
if (missing.length > 0) {
36+
console.error("❌ Missing required environment variables:");
37+
missing.forEach((varName) => {
38+
console.error(` - ${varName}`);
39+
});
40+
console.error(
41+
"\n📖 Please check your environment variables in Render dashboard."
42+
);
43+
console.error("🔗 Reference: RENDER_ENV_VARS.txt file in your project.");
44+
process.exit(1);
45+
}
46+
47+
console.log("✅ All required environment variables are set!");
48+
};
49+
50+
export default validateEnvironment;

0 commit comments

Comments
 (0)