UNPKG

apploud-cli

Version:

CLI tool for Apploud container hosting management

158 lines (138 loc) 5.44 kB
#!/usr/bin/env node const axios = require('axios'); const chalk = require('chalk'); const fs = require('fs'); const path = require('path'); const os = require('os'); const WebSocket = require('ws'); // Get URLs from environment variables or use defaults const APP_URL = process.env.APP_URL || 'http://localhost:8000'; const WS_URL = process.env.WS_URL || 'ws://10.88.226.106:8001'; const API_URL = `${APP_URL}/api`; const tokenFilePath = path.join(os.homedir(), '.apploud', 'auth_token.json'); async function testEndpoints() { console.log(chalk.blue('CLI Debug Utility')); console.log('==================\n'); console.log(chalk.yellow('Configured URLs:')); console.log(`APP_URL: ${APP_URL}`); console.log(`API_URL: ${API_URL}`); console.log(`WS_URL: ${WS_URL}\n`); // Check if token file exists console.log(chalk.yellow('Authentication Status:')); if (fs.existsSync(tokenFilePath)) { console.log(`Token file exists at: ${tokenFilePath}`); try { const tokenData = JSON.parse(fs.readFileSync(tokenFilePath)); console.log('Token is present in file'); // Verify token try { console.log('Testing token validity...'); const verifyResponse = await axios.get(`${API_URL}/cli/verify`, { headers: { 'Authorization': `Bearer ${tokenData.token}` }, validateStatus: () => true }); console.log(`Token verification status: ${verifyResponse.status}`); console.log('Response:', verifyResponse.data); } catch (error) { console.error(chalk.red('Token verification error:'), error.message); } } catch (error) { console.error(chalk.red('Error reading token file:'), error.message); } } else { console.log('No token file found'); } console.log(''); // Test API endpoints with detailed request/response logging console.log(chalk.yellow('Testing API endpoints:')); // List routes endpoint (if available) try { console.log('\nTesting list-routes endpoint (if available)...'); const routesResponse = await axios.get(`${APP_URL}/list-routes`, { validateStatus: () => true, timeout: 5000 }); if (routesResponse.status === 200) { console.log(chalk.green('Routes retrieved successfully')); console.log('Available routes:', routesResponse.data); } else { console.log(chalk.yellow('Routes endpoint not available or requires authentication')); } } catch (error) { console.log(chalk.yellow('Routes endpoint not available')); } // Test endpoint try { console.log('\nTesting /api/cli/test endpoint...'); console.log('Request: GET ' + `${API_URL}/cli/test`); const testResponse = await axios.get(`${API_URL}/cli/test`, { validateStatus: () => true, timeout: 5000 }); console.log(`Status: ${testResponse.status}`); console.log('Response headers:', testResponse.headers); console.log('Response data:', testResponse.data); } catch (error) { console.error(chalk.red('Network error:'), error.message); } // Login endpoint try { console.log('\nTesting /api/cli/login endpoint...'); console.log('Request: POST ' + `${API_URL}/cli/login`); // First check with OPTIONS request to detect CORS issues console.log('Checking CORS with OPTIONS request...'); try { const optionsResponse = await axios({ method: 'OPTIONS', url: `${API_URL}/cli/login`, validateStatus: () => true }); console.log(`OPTIONS status: ${optionsResponse.status}`); console.log('OPTIONS headers:', optionsResponse.headers); } catch (error) { console.log(chalk.yellow('OPTIONS request failed, might not be a CORS issue')); } // Now try the actual login request const loginResponse = await axios.post(`${API_URL}/cli/login`, {}, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, validateStatus: () => true, timeout: 10000 }); console.log(`Status: ${loginResponse.status}`); console.log('Response headers:', loginResponse.headers); console.log('Response data:', loginResponse.data); } catch (error) { console.error(chalk.red('Network error:'), error.message); if (error.response) { console.log('Response status:', error.response.status); console.log('Response headers:', error.response.headers); console.log('Response data:', error.response.data); } } // Test WebSocket connection console.log('\n' + chalk.yellow('Testing WebSocket connection:')); try { console.log(`Connecting to WebSocket at ${WS_URL}...`); const ws = new WebSocket(WS_URL); ws.on('open', () => { console.log(chalk.green('WebSocket connection established successfully')); ws.close(); }); ws.on('error', (error) => { console.error(chalk.red('WebSocket connection error:'), error.message); }); // Wait for 3 seconds to let the WebSocket connection attempt complete await new Promise(resolve => setTimeout(resolve, 3000)); } catch (error) { console.error(chalk.red('WebSocket error:'), error.message); } console.log('\n' + chalk.green('Debug complete!')); console.log(chalk.blue('If you continue to have issues, check Laravel logs at:')); console.log(`${APP_URL}/storage/logs/laravel.log`); } testEndpoints().catch(err => { console.error(chalk.red('Fatal error:'), err); });