浏览代码

feat: Add local development domain support for MT5 EA connectivity

- Update Nginx config to accept market-data.local domain alongside localhost
- Add automated setup script with macOS password prompt support
- Update MT5 EA with local dev URL documentation
- Add comprehensive local development setup guide
- Update README with Docker-based local dev instructions
- Remove redundant documentation files (LOCAL_DEV_URL_ANALYSIS.md, QUICK_START.md)

This enables MT5 Expert Advisors to connect to local development environment
using market-data.local domain, as MT5 doesn't allow localhost/127.0.0.1 URLs.

Setup is now fully automated on macOS (prompts for password) and works
on Linux with sudo fallback.
Hussain Afzal 3 月之前
父节点
当前提交
4c16f0f502
共有 6 个文件被更改,包括 676 次插入40 次删除
  1. 3 0
      MT5/Experts/MarketDataSender.mq5
  2. 116 39
      README.md
  3. 2 0
      docker-compose.yml
  4. 462 0
      docs/LOCAL_DEV_SETUP.md
  5. 3 1
      nginx/nginx.conf
  6. 90 0
      scripts/setup-local-dev.sh

+ 3 - 0
MT5/Experts/MarketDataSender.mq5

@@ -6,6 +6,9 @@
6 6
 
7 7
 #include <Trade\SymbolInfo.mqh>
8 8
 
9
+// API Base URL Configuration
10
+// Production: "http://market-price.insightbull.io"
11
+// Local Dev:  "http://market-data.local" (requires hosts file setup - see docs/LOCAL_DEV_SETUP.md)
9 12
 input string ApiBaseUrl = "http://market-price.insightbull.io";
10 13
 input int HistoricalCandleCount = 1000;
11 14
 input ENUM_TIMEFRAMES HistoricalTimeframe = PERIOD_H1;

+ 116 - 39
README.md

@@ -360,31 +360,131 @@ curl -X POST https://your-domain.com/api/live-prices/bulk \
360 360
 
361 361
 ## 💻 LOCAL DEVELOPMENT SETUP
362 362
 
363
-This section explains how to run the project locally on your development machine.
363
+This section provides a quick overview. For detailed instructions, troubleshooting, and advanced configuration, see **[docs/LOCAL_DEV_SETUP.md](./docs/LOCAL_DEV_SETUP.md)**.
364 364
 
365
-### Prerequisites for Local Development
366
-- **Node.js**: v18.x LTS or higher
367
-- **PostgreSQL**: v13.x or higher
368
-- **Git**: Latest version
365
+### Prerequisites
366
+- **Docker** and **Docker Compose** installed
367
+- **Git** installed
368
+- **Administrator access** (script will prompt for password automatically on macOS)
369 369
 
370
-### Step-by-Step Local Setup
370
+### Quick Start
371 371
 
372 372
 #### 1. Clone the Repository
373 373
 ```bash
374 374
 # Clone from GitHub
375 375
 git clone https://git.mqldevelopment.com/muhammad.uzair/market-data-service.git
376 376
 cd market-data-service
377
+```
377 378
 
378
-# Install dependencies
379
+#### 2. Setup Local Domain (One-time, Required for MT5 EA)
380
+```bash
381
+# Run automated setup script (fully automated on macOS - will prompt for password)
382
+# On Linux, you may need to run with: sudo bash scripts/setup-local-dev.sh
383
+bash scripts/setup-local-dev.sh
384
+```
385
+
386
+**Note:** On macOS, the script automatically prompts for your administrator password. On Linux, you may need to run with `sudo`.
387
+
388
+**Alternative (Manual):**
389
+```bash
390
+# Add market-data.local to hosts file manually
391
+echo "127.0.0.1 market-data.local" | sudo tee -a /etc/hosts
392
+```
393
+
394
+#### 3. Start Docker Services
395
+```bash
396
+# Start all services (database, API, Nginx)
397
+docker-compose up -d
398
+
399
+# Verify services are running
400
+docker-compose ps
401
+```
402
+
403
+#### 4. Verify Setup
404
+```bash
405
+# Test health endpoint
406
+curl http://market-data.local/health
407
+
408
+# Test API endpoints
409
+curl http://market-data.local/api/symbols
410
+curl http://market-data.local/api/health
411
+```
412
+
413
+**✅ Setup Complete!** Your API is now accessible at `http://market-data.local`
414
+
415
+### Why market-data.local?
416
+
417
+MT5 Expert Advisors require valid domain URLs and **do not allow** `localhost` or `127.0.0.1`. The `market-data.local` domain resolves to your local Docker setup, enabling MT5 EA connectivity.
418
+
419
+### MT5 EA Configuration
420
+
421
+1. **Add URL to MT5 Allowed List:**
422
+   - Open MT5 → Tools → Options → Expert Advisors
423
+   - Click "Allowed URLs" tab → Add → Enter: `http://market-data.local`
424
+
425
+2. **Configure EA:**
426
+   - Attach `MarketDataSender.mq5` to any chart
427
+   - Set `ApiBaseUrl = "http://market-data.local"`
428
+   - Other settings as needed
429
+
430
+3. **Verify Connection:**
431
+   - Check MT5 Experts tab for EA logs
432
+   - Look for: `✅ Symbols initialized`
433
+
434
+### Development Workflow
435
+
436
+**View Logs:**
437
+```bash
438
+# All services
439
+docker-compose logs -f
440
+
441
+# Specific service
442
+docker-compose logs -f api
443
+docker-compose logs -f nginx
444
+```
445
+
446
+**Code Changes:**
447
+- Code in `src/` is automatically reloaded (no container restart needed)
448
+- View API logs to see changes: `docker-compose logs -f api`
449
+
450
+**Stop Services:**
451
+```bash
452
+docker-compose down
453
+```
454
+
455
+**Restart Services:**
456
+```bash
457
+docker-compose restart
458
+```
459
+
460
+### Need More Help?
461
+
462
+📖 **For comprehensive setup instructions, troubleshooting, and advanced configuration, see:**
463
+- **[docs/LOCAL_DEV_SETUP.md](./docs/LOCAL_DEV_SETUP.md)** - Complete local development guide with detailed steps, troubleshooting, and MT5 EA configuration
464
+
465
+### Alternative: Non-Docker Local Setup
466
+
467
+If you prefer to run without Docker (not recommended for MT5 EA):
468
+
469
+<details>
470
+<summary>Click to expand non-Docker setup instructions</summary>
471
+
472
+#### Prerequisites
473
+- **Node.js**: v18.x LTS or higher
474
+- **PostgreSQL**: v13.x or higher
475
+
476
+#### Setup Steps
477
+
478
+1. **Install Dependencies:**
479
+```bash
379 480
 npm install
380 481
 ```
381 482
 
382
-#### 2. Set Up PostgreSQL Database
483
+2. **Set Up Database:**
383 484
 ```bash
384
-# Create local database
385 485
 createdb financial_data
386 486
 
387
-# Set up environment variables
487
+# Create .env file
388 488
 cat > .env << EOF
389 489
 DB_TYPE=postgres
390 490
 DB_HOST=localhost
@@ -392,55 +492,32 @@ DB_PORT=5432
392 492
 DB_NAME=financial_data
393 493
 DB_USER=your_local_username
394 494
 DB_PASSWORD=your_local_password
395
-
396 495
 PORT=3001
397 496
 NODE_ENV=development
398
-
399 497
 JWT_SECRET=your_development_jwt_secret_key
400 498
 CORS_ORIGIN=http://localhost:3000
401
-
402 499
 LOG_LEVEL=debug
403 500
 EOF
404 501
 ```
405 502
 
406
-#### 3. Run Database Migrations
503
+3. **Run Migrations:**
407 504
 ```bash
408
-# Install Sequelize CLI (if not already installed)
409
-npm install -g sequelize-cli
410
-
411
-# Run migrations to create tables
412 505
 npx sequelize-cli db:migrate
413
-
414
-# Verify tables were created
415
-psql -d financial_data -c "\dt"
416 506
 ```
417 507
 
418
-#### 4. Start Development Server
508
+4. **Start Server:**
419 509
 ```bash
420
-# Option 1: Using npm script (recommended)
421 510
 npm run dev
422
-
423
-# Option 2: Using nodemon directly
424
-npx nodemon src/server.js
425
-
426
-# Option 3: Using node directly
427
-node src/server.js
428 511
 ```
429 512
 
430
-#### 5. Verify Local Setup
513
+5. **Test:**
431 514
 ```bash
432
-# Test health endpoint
433 515
 curl http://localhost:3001/health
516
+```
434 517
 
435
-# Test API endpoints
436
-curl http://localhost:3001/api/symbols
437
-curl http://localhost:3001/api/live-prices
518
+**Note:** This setup won't work with MT5 EA (requires domain URL). Use Docker setup for MT5 integration.
438 519
 
439
-# Test bulk endpoint
440
-curl -X POST http://localhost:3001/api/live-prices/bulk \
441
-  -H "Content-Type: application/json" \
442
-  -d '{"prices": [{"symbolId": 1, "price": 123.45}]}'
443
-```
520
+</details>
444 521
 
445 522
 
446 523
 ## 🔄 UPDATING EXISTING DEPLOYMENT

+ 2 - 0
docker-compose.yml

@@ -62,6 +62,8 @@ services:
62 62
     command: -c "/usr/local/bin/wait-for-db.sh && node docker/sync-models.js && (npx sequelize-cli db:migrate || echo 'No migrations to run or migration skipped') && npm install -g nodemon && nodemon src/server.js"
63 63
 
64 64
   # Nginx Reverse Proxy
65
+  # For local development with MT5 EA, use: http://market-data.local
66
+  # Run: sudo bash scripts/setup-local-dev.sh (one-time setup)
65 67
   nginx:
66 68
     image: nginx:alpine
67 69
     container_name: market-data-nginx

+ 462 - 0
docs/LOCAL_DEV_SETUP.md

@@ -0,0 +1,462 @@
1
+# Local Development Setup Guide
2
+
3
+This guide will help you set up the Market Data Service for local development with MT5 EA connectivity.
4
+
5
+## Quick Start (5 minutes)
6
+
7
+### Prerequisites
8
+- Docker and Docker Compose installed
9
+- Git installed
10
+- Administrator access (script will prompt for password automatically on macOS)
11
+
12
+### Step 1: Clone and Setup Hosts File
13
+
14
+```bash
15
+# Clone the repository
16
+git clone https://git.mqldevelopment.com/muhammad.uzair/market-data-service.git
17
+cd market-data-service
18
+
19
+# Run the automated setup script (fully automated - will prompt for password on macOS)
20
+# On Linux, you may need: sudo bash scripts/setup-local-dev.sh
21
+bash scripts/setup-local-dev.sh
22
+```
23
+
24
+**Note:** On macOS, the script automatically prompts for your administrator password using a system dialog. On Linux, you may need to run with `sudo`.
25
+
26
+**Alternative (Manual Setup):**
27
+If you prefer to do it manually:
28
+```bash
29
+# Add market-data.local to your hosts file
30
+echo "127.0.0.1 market-data.local" | sudo tee -a /etc/hosts
31
+```
32
+
33
+### Step 2: Start Docker Services
34
+
35
+```bash
36
+# Start all services (database, API, Nginx)
37
+docker-compose up -d
38
+
39
+# Verify services are running
40
+docker-compose ps
41
+```
42
+
43
+### Step 3: Verify Setup
44
+
45
+```bash
46
+# Test health endpoint
47
+curl http://market-data.local/health
48
+
49
+# Test API endpoint
50
+curl http://market-data.local/api/symbols
51
+```
52
+
53
+You should see JSON responses. If you do, your setup is complete! ✅
54
+
55
+---
56
+
57
+## Why market-data.local?
58
+
59
+MT5 Expert Advisors require valid domain URLs in their "Allowed URLs" list. MT5 **does not allow**:
60
+- ❌ `localhost`
61
+- ❌ `127.0.0.1`
62
+- ❌ `http://localhost:80`
63
+
64
+MT5 **requires** a domain-like URL:
65
+- ✅ `http://market-data.local`
66
+
67
+The `market-data.local` domain resolves to your local Docker setup via the `/etc/hosts` file, allowing MT5 EA to connect to your local development server.
68
+
69
+---
70
+
71
+## Complete Setup Instructions
72
+
73
+### 1. Hosts File Configuration
74
+
75
+The setup script (`bash scripts/setup-local-dev.sh`) automatically adds the required entry. On macOS, it will prompt for your administrator password using a system dialog. On Linux, you may need to run it with `sudo`.
76
+
77
+**Verify the entry:**
78
+```bash
79
+# View current entry
80
+cat /etc/hosts | grep market-data.local
81
+```
82
+
83
+**Manual setup (if needed):**
84
+```bash
85
+# Add entry manually (macOS/Linux)
86
+echo "127.0.0.1 market-data.local" | sudo tee -a /etc/hosts
87
+```
88
+
89
+**Windows:**
90
+1. Open Notepad as Administrator
91
+2. Open `C:\Windows\System32\drivers\etc\hosts`
92
+3. Add line: `127.0.0.1 market-data.local`
93
+4. Save and close
94
+
95
+### 2. Docker Services
96
+
97
+**Start Services:**
98
+```bash
99
+docker-compose up -d
100
+```
101
+
102
+**View Logs:**
103
+```bash
104
+# All services
105
+docker-compose logs -f
106
+
107
+# Specific service
108
+docker-compose logs -f api
109
+docker-compose logs -f nginx
110
+docker-compose logs -f db
111
+```
112
+
113
+**Stop Services:**
114
+```bash
115
+docker-compose down
116
+```
117
+
118
+**Restart Services:**
119
+```bash
120
+docker-compose restart
121
+```
122
+
123
+### 3. Verify Services
124
+
125
+**Check Service Status:**
126
+```bash
127
+docker-compose ps
128
+```
129
+
130
+All services should show "Up" status.
131
+
132
+**Test Endpoints:**
133
+```bash
134
+# Health check
135
+curl http://market-data.local/health
136
+
137
+# API endpoints
138
+curl http://market-data.local/api/symbols
139
+curl http://market-data.local/api/health
140
+
141
+# Test with browser
142
+open http://market-data.local/health  # macOS
143
+# or visit http://market-data.local/health in your browser
144
+```
145
+
146
+---
147
+
148
+## MT5 EA Configuration
149
+
150
+### Step 1: Add URL to MT5 Allowed List
151
+
152
+1. Open MT5
153
+2. Go to: **Tools → Options → Expert Advisors**
154
+3. Click the **"Allowed URLs"** tab
155
+4. Click **"Add"**
156
+5. Enter: `http://market-data.local`
157
+6. Click **"OK"**
158
+
159
+### Step 2: Configure EA
160
+
161
+1. Attach `MarketDataSender.mq5` to any chart
162
+2. In EA Inputs, set:
163
+   - **ApiBaseUrl**: `http://market-data.local`
164
+   - **HistoricalCandleCount**: `1000` (or your preference)
165
+   - **LivePriceIntervalSeconds**: `5` (or your preference)
166
+3. Click **"OK"**
167
+
168
+### Step 3: Verify Connection
169
+
170
+1. Check the **Experts** tab in MT5 Terminal
171
+2. Look for EA logs:
172
+   - ✅ `✅ Symbols initialized: X`
173
+   - ✅ `✅ Successfully sent live prices to API`
174
+   - ❌ If you see errors, check troubleshooting section below
175
+
176
+---
177
+
178
+## Testing the Setup
179
+
180
+### Test 1: Basic Connectivity
181
+
182
+```bash
183
+# Health check
184
+curl http://market-data.local/health
185
+
186
+# Expected response:
187
+# {"success":true,"message":"Market Data Service is running",...}
188
+```
189
+
190
+### Test 2: API Endpoints
191
+
192
+```bash
193
+# Get symbols
194
+curl http://market-data.local/api/symbols
195
+
196
+# Get health
197
+curl http://market-data.local/api/health
198
+```
199
+
200
+### Test 3: MT5 EA Connection
201
+
202
+1. Start MT5 EA with `ApiBaseUrl = "http://market-data.local"`
203
+2. Check MT5 Experts tab for logs
204
+3. Verify symbols are being synced
205
+4. Check that live prices are being sent
206
+
207
+### Test 4: Database Verification
208
+
209
+```bash
210
+# Connect to database
211
+docker-compose exec db psql -U postgres -d financial_data
212
+
213
+# Check tables
214
+\dt
215
+
216
+# Check symbols
217
+SELECT * FROM symbols LIMIT 5;
218
+
219
+# Check live prices
220
+SELECT * FROM live_prices LIMIT 5;
221
+
222
+# Exit
223
+\q
224
+```
225
+
226
+---
227
+
228
+## Troubleshooting
229
+
230
+### Issue: "curl: Could not resolve host: market-data.local"
231
+
232
+**Solution:**
233
+```bash
234
+# Verify hosts entry exists
235
+cat /etc/hosts | grep market-data.local
236
+
237
+# If missing, add it
238
+echo "127.0.0.1 market-data.local" | sudo tee -a /etc/hosts
239
+
240
+# Flush DNS cache (macOS)
241
+sudo dscacheutil -flushcache
242
+
243
+# Flush DNS cache (Linux)
244
+sudo systemd-resolve --flush-caches
245
+```
246
+
247
+### Issue: "502 Bad Gateway" or "Connection refused"
248
+
249
+**Solution:**
250
+```bash
251
+# Check if services are running
252
+docker-compose ps
253
+
254
+# If not running, start them
255
+docker-compose up -d
256
+
257
+# Check service logs
258
+docker-compose logs api
259
+docker-compose logs nginx
260
+
261
+# Restart services
262
+docker-compose restart
263
+```
264
+
265
+### Issue: MT5 EA shows "WebRequest connection error"
266
+
267
+**Solution:**
268
+1. Verify URL is in MT5 Allowed URLs list
269
+2. Check URL format: `http://market-data.local` (no trailing slash)
270
+3. Test URL manually: `curl http://market-data.local/health`
271
+4. Verify Docker services are running: `docker-compose ps`
272
+5. Check Nginx logs: `docker-compose logs nginx`
273
+
274
+### Issue: "Failed to fetch symbols from API: HTTP 502"
275
+
276
+**Solution:**
277
+```bash
278
+# Check API service
279
+docker-compose logs api
280
+
281
+# Check database connection
282
+docker-compose exec db pg_isready -U postgres
283
+
284
+# Restart API service
285
+docker-compose restart api
286
+```
287
+
288
+### Issue: Database connection errors
289
+
290
+**Solution:**
291
+```bash
292
+# Check database is running
293
+docker-compose ps db
294
+
295
+# Check database logs
296
+docker-compose logs db
297
+
298
+# Restart database
299
+docker-compose restart db
300
+
301
+# Wait for database to be ready
302
+docker-compose exec db pg_isready -U postgres
303
+```
304
+
305
+### Issue: Port 80 already in use
306
+
307
+**Solution:**
308
+```bash
309
+# Check what's using port 80
310
+sudo lsof -i :80
311
+
312
+# Stop conflicting service or change Nginx port in docker-compose.yml
313
+# Edit docker-compose.yml, change "80:80" to "8080:80"
314
+# Then use: http://market-data.local:8080
315
+```
316
+
317
+---
318
+
319
+## Development Workflow
320
+
321
+### Making Code Changes
322
+
323
+The API service uses volume mounts for live code reloading:
324
+
325
+```bash
326
+# Code changes in src/ are automatically reloaded
327
+# No need to restart containers
328
+
329
+# View API logs to see changes
330
+docker-compose logs -f api
331
+```
332
+
333
+### Database Migrations
334
+
335
+Migrations run automatically on container start. To run manually:
336
+
337
+```bash
338
+# Run migrations
339
+docker-compose exec api npx sequelize-cli db:migrate
340
+
341
+# Check migration status
342
+docker-compose exec api npx sequelize-cli db:migrate:status
343
+```
344
+
345
+### Viewing Logs
346
+
347
+```bash
348
+# All services
349
+docker-compose logs -f
350
+
351
+# Specific service
352
+docker-compose logs -f api
353
+docker-compose logs -f nginx
354
+docker-compose logs -f db
355
+
356
+# Last 100 lines
357
+docker-compose logs --tail=100 api
358
+```
359
+
360
+---
361
+
362
+## Switching Between Local and Production
363
+
364
+### Local Development
365
+- **URL**: `http://market-data.local`
366
+- **MT5 EA**: `ApiBaseUrl = "http://market-data.local"`
367
+
368
+### Production
369
+- **URL**: `http://market-price.insightbull.io`
370
+- **MT5 EA**: `ApiBaseUrl = "http://market-price.insightbull.io"`
371
+
372
+You can easily switch by changing the `ApiBaseUrl` input in MT5 EA settings.
373
+
374
+---
375
+
376
+## Cleanup
377
+
378
+### Remove Hosts Entry (if needed)
379
+
380
+**macOS/Linux:**
381
+```bash
382
+# Remove the entry
383
+sudo sed -i '' '/market-data.local/d' /etc/hosts  # macOS
384
+sudo sed -i '/market-data.local/d' /etc/hosts     # Linux
385
+```
386
+
387
+**Windows:**
388
+1. Open Notepad as Administrator
389
+2. Open `C:\Windows\System32\drivers\etc\hosts`
390
+3. Remove line: `127.0.0.1 market-data.local`
391
+4. Save and close
392
+
393
+### Stop Docker Services
394
+
395
+```bash
396
+# Stop and remove containers
397
+docker-compose down
398
+
399
+# Stop and remove containers + volumes (⚠️ deletes database data)
400
+docker-compose down -v
401
+```
402
+
403
+---
404
+
405
+## Additional Resources
406
+
407
+- **Main README**: See [README.md](../README.md) for general project information
408
+- **Docker Guide**: See [DOCKER.md](../DOCKER.md) for Docker-specific details
409
+- **API Documentation**: See [docs/API_CONTRACT.md](./API_CONTRACT.md)
410
+- **MT5 Operation**: See [docs/MT5_OPERATION.md](./MT5_OPERATION.md)
411
+
412
+---
413
+
414
+## Quick Reference
415
+
416
+### Essential Commands
417
+
418
+```bash
419
+# Setup (one-time, fully automated on macOS)
420
+bash scripts/setup-local-dev.sh
421
+# On Linux, you may need: sudo bash scripts/setup-local-dev.sh
422
+
423
+# Start services
424
+docker-compose up -d
425
+
426
+# Stop services
427
+docker-compose down
428
+
429
+# View logs
430
+docker-compose logs -f api
431
+
432
+# Test endpoint
433
+curl http://market-data.local/health
434
+
435
+# Check services
436
+docker-compose ps
437
+```
438
+
439
+### URLs
440
+
441
+- **Local API**: `http://market-data.local`
442
+- **Health Check**: `http://market-data.local/health`
443
+- **API Endpoints**: `http://market-data.local/api/*`
444
+
445
+### MT5 Configuration
446
+
447
+- **Allowed URL**: `http://market-data.local`
448
+- **EA ApiBaseUrl**: `http://market-data.local`
449
+
450
+---
451
+
452
+## Need Help?
453
+
454
+If you encounter issues not covered in this guide:
455
+
456
+1. Check the troubleshooting section above
457
+2. Review service logs: `docker-compose logs`
458
+3. Verify hosts entry: `cat /etc/hosts | grep market-data.local`
459
+4. Test connectivity: `curl http://market-data.local/health`
460
+
461
+For additional support, check the main [README.md](../README.md) or project documentation.
462
+

+ 3 - 1
nginx/nginx.conf

@@ -41,7 +41,9 @@ http {
41 41
 
42 42
     server {
43 43
         listen 80;
44
-        server_name localhost;
44
+        # Accept both localhost and market-data.local for local development
45
+        # market-data.local is required for MT5 EA connectivity (MT5 doesn't allow localhost/127.0.0.1)
46
+        server_name localhost market-data.local;
45 47
 
46 48
         # Health check endpoint
47 49
         location /health {

+ 90 - 0
scripts/setup-local-dev.sh

@@ -0,0 +1,90 @@
1
+#!/bin/bash
2
+# Setup script for local development with market-data.local domain
3
+# This script automates the hosts file entry required for MT5 EA connectivity
4
+
5
+set -e
6
+
7
+DOMAIN="market-data.local"
8
+HOSTS_ENTRY="127.0.0.1 ${DOMAIN}"
9
+HOSTS_FILE="/etc/hosts"
10
+
11
+echo "🚀 Setting up local development environment for Market Data Service"
12
+echo ""
13
+
14
+# Check if running on macOS
15
+if [[ "$OSTYPE" == "darwin"* ]]; then
16
+    OS="macOS"
17
+elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
18
+    OS="Linux"
19
+else
20
+    echo "⚠️  Warning: Unsupported OS. Please manually add to hosts file:"
21
+    echo "   ${HOSTS_ENTRY}"
22
+    exit 1
23
+fi
24
+
25
+# Check if entry already exists
26
+if grep -q "${DOMAIN}" "${HOSTS_FILE}" 2>/dev/null; then
27
+    echo "✅ Hosts entry for ${DOMAIN} already exists"
28
+    echo ""
29
+    echo "Current entry:"
30
+    grep "${DOMAIN}" "${HOSTS_FILE}" | head -1
31
+    echo ""
32
+    echo "✅ Setup complete! You can now use http://${DOMAIN}"
33
+    exit 0
34
+fi
35
+
36
+# Check if running as root (required for hosts file modification)
37
+if [ "$EUID" -ne 0 ]; then
38
+    echo "📝 Adding ${DOMAIN} to hosts file requires administrator privileges"
39
+    echo ""
40
+    
41
+    # Try to use osascript on macOS for interactive password prompt
42
+    if [[ "$OSTYPE" == "darwin"* ]] && command -v osascript >/dev/null 2>&1; then
43
+        echo "   Prompting for administrator password..."
44
+        echo ""
45
+        
46
+        # Use osascript to run the command with admin privileges
47
+        osascript -e "do shell script \"echo '${HOSTS_ENTRY}' >> ${HOSTS_FILE}\" with administrator privileges" 2>&1
48
+        
49
+        if [ $? -ne 0 ]; then
50
+            echo ""
51
+            echo "❌ Failed to add entry. Please run manually:"
52
+            echo "   sudo bash scripts/setup-local-dev.sh"
53
+            echo ""
54
+            echo "Or manually add this line to ${HOSTS_FILE}:"
55
+            echo "   ${HOSTS_ENTRY}"
56
+            exit 1
57
+        fi
58
+    else
59
+        # Not macOS or osascript not available, require manual sudo
60
+        echo "Please run this script with sudo:"
61
+        echo "   sudo bash scripts/setup-local-dev.sh"
62
+        echo ""
63
+        echo "Or manually add this line to ${HOSTS_FILE}:"
64
+        echo "   ${HOSTS_ENTRY}"
65
+        exit 1
66
+    fi
67
+else
68
+    # Already running as root, just add the entry
69
+    echo "📝 Adding ${DOMAIN} to ${HOSTS_FILE}..."
70
+    echo "${HOSTS_ENTRY}" >> "${HOSTS_FILE}"
71
+fi
72
+
73
+# Verify the entry was added
74
+if grep -q "${DOMAIN}" "${HOSTS_FILE}"; then
75
+    echo "✅ Successfully added ${DOMAIN} to hosts file"
76
+    echo ""
77
+    echo "Added entry:"
78
+    grep "${DOMAIN}" "${HOSTS_FILE}" | tail -1
79
+    echo ""
80
+    echo "✅ Setup complete!"
81
+    echo ""
82
+    echo "Next steps:"
83
+    echo "  1. Start Docker services: docker-compose up -d"
84
+    echo "  2. Test the domain: curl http://${DOMAIN}/health"
85
+    echo "  3. Configure MT5 EA with: ApiBaseUrl = \"http://${DOMAIN}\""
86
+else
87
+    echo "❌ Failed to add entry to hosts file"
88
+    exit 1
89
+fi
90
+