Fix API URL handling for Docker deployment

- Replace hardcoded localhost URLs with dynamic host detection from request headers
- Supports both development (localhost:3001) and Docker (localhost:9000 -> 3000) environments
- Uses host header to determine correct protocol and port for internal API calls
- Updated execute-dex, validate, and orders APIs to use dynamic baseUrl
- Ensures proper API communication in containerized environments
This commit is contained in:
mindesbunister
2025-07-16 16:24:26 +02:00
parent fb8d361020
commit befe860188
4 changed files with 25 additions and 6 deletions

View File

@@ -18,6 +18,11 @@ export async function POST(request) {
toCoin toCoin
} = body } = body
// Get the base URL from the request
const host = request.headers.get('host') || 'localhost:3000'
const protocol = host.includes('localhost') ? 'http' : 'https'
const baseUrl = `${protocol}://${host}`
console.log('🔄 Execute DEX trade request:', { console.log('🔄 Execute DEX trade request:', {
symbol, symbol,
side, side,
@@ -66,7 +71,7 @@ export async function POST(request) {
console.log('🔍 Validating wallet balance before DEX trade...') console.log('🔍 Validating wallet balance before DEX trade...')
try { try {
const validationResponse = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/trading/validate`, { const validationResponse = await fetch(`${baseUrl}/api/trading/validate`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
@@ -197,7 +202,7 @@ export async function POST(request) {
// Add trade to history with clear spot swap indication // Add trade to history with clear spot swap indication
try { try {
// Use localhost for internal container communication // Use localhost for internal container communication
await fetch('http://localhost:3001/api/trading/history', { await fetch(`${baseUrl}/api/trading/history`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({

View File

@@ -106,6 +106,11 @@ export async function POST(request) {
const body = await request.json() const body = await request.json()
const { action, orderId, ...orderData } = body const { action, orderId, ...orderData } = body
// Get the base URL from the request
const host = request.headers.get('host') || 'localhost:3000'
const protocol = host.includes('localhost') ? 'http' : 'https'
const baseUrl = `${protocol}://${host}`
if (action === 'add') { if (action === 'add') {
// Load existing orders // Load existing orders
const pendingOrders = loadPendingOrders() const pendingOrders = loadPendingOrders()
@@ -187,7 +192,7 @@ export async function POST(request) {
try { try {
// Execute the trade by calling the trading API // Execute the trade by calling the trading API
const tradeResponse = await fetch('http://localhost:3000/api/trading', { const tradeResponse = await fetch(`${baseUrl}/api/trading`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({

View File

@@ -7,10 +7,15 @@ export async function POST(request) {
console.log(`🔍 Validating trade: ${side} ${amount} ${symbol} (USD: ${amountUSD})`) console.log(`🔍 Validating trade: ${side} ${amount} ${symbol} (USD: ${amountUSD})`)
// Get the base URL from the request or use localhost for development
const host = request.headers.get('host') || 'localhost:3000'
const protocol = host.includes('localhost') ? 'http' : 'https'
const baseUrl = `${protocol}://${host}`
// Fetch real wallet balance from the wallet API // Fetch real wallet balance from the wallet API
let walletBalance let walletBalance
try { try {
const walletResponse = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/wallet/balance`) const walletResponse = await fetch(`${baseUrl}/api/wallet/balance`)
const walletData = await walletResponse.json() const walletData = await walletResponse.json()
if (walletData.success && walletData.wallet) { if (walletData.success && walletData.wallet) {

View File

@@ -2,6 +2,9 @@
const testTradeValidation = async () => { const testTradeValidation = async () => {
console.log('🧪 Testing trade validation with fixed amountUSD...') console.log('🧪 Testing trade validation with fixed amountUSD...')
// Use port 9000 for Docker or 3001 for local dev
const apiUrl = process.env.DOCKER_MODE ? 'http://localhost:9000' : 'http://localhost:3001'
const tradeData = { const tradeData = {
symbol: 'USDCUSD', symbol: 'USDCUSD',
side: 'BUY', side: 'BUY',
@@ -11,10 +14,11 @@ const testTradeValidation = async () => {
tradingPair: 'USDCUSD/USDC' tradingPair: 'USDCUSD/USDC'
} }
console.log('🚀 Sending test trade:', tradeData) console.log('🚀 Sending test trade to:', apiUrl)
console.log('🚀 Trade data:', tradeData)
try { try {
const response = await fetch('http://localhost:3001/api/trading/execute-dex', { const response = await fetch(`${apiUrl}/api/trading/execute-dex`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(tradeData) body: JSON.stringify(tradeData)