|
| 1 | +import { fetchWithSentry } from '@/utils' |
| 2 | +import { NextResponse } from 'next/server' |
| 3 | + |
| 4 | +const MOBULA_API_URL = process.env.MOBULA_API_URL! |
| 5 | +const MOBULA_API_KEY = process.env.MOBULA_API_KEY! |
| 6 | + |
| 7 | +/** |
| 8 | + * Health check for Mobula API |
| 9 | + * Tests both asset price endpoint and portfolio endpoint |
| 10 | + */ |
| 11 | +export async function GET() { |
| 12 | + const startTime = Date.now() |
| 13 | + |
| 14 | + try { |
| 15 | + if (!MOBULA_API_KEY) { |
| 16 | + return NextResponse.json( |
| 17 | + { |
| 18 | + status: 'unhealthy', |
| 19 | + service: 'mobula', |
| 20 | + timestamp: new Date().toISOString(), |
| 21 | + error: 'MOBULA_API_KEY not configured', |
| 22 | + responseTime: Date.now() - startTime, |
| 23 | + }, |
| 24 | + { status: 500 } |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + // Test 1: Asset price endpoint (using USDC on Ethereum as test) |
| 29 | + const priceTestStart = Date.now() |
| 30 | + const priceResponse = await fetchWithSentry( |
| 31 | + `${MOBULA_API_URL}/api/1/market/data?asset=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&blockchain=1`, |
| 32 | + { |
| 33 | + headers: { |
| 34 | + 'Content-Type': 'application/json', |
| 35 | + authorization: MOBULA_API_KEY, |
| 36 | + }, |
| 37 | + } |
| 38 | + ) |
| 39 | + const priceResponseTime = Date.now() - priceTestStart |
| 40 | + |
| 41 | + if (!priceResponse.ok) { |
| 42 | + throw new Error(`Price API returned ${priceResponse.status}`) |
| 43 | + } |
| 44 | + |
| 45 | + const priceData = await priceResponse.json() |
| 46 | + if (!priceData?.data?.price) { |
| 47 | + throw new Error('Invalid price data structure') |
| 48 | + } |
| 49 | + |
| 50 | + // Test 2: Portfolio endpoint (using a known address with likely balance) |
| 51 | + const portfolioTestStart = Date.now() |
| 52 | + const portfolioResponse = await fetchWithSentry( |
| 53 | + `${MOBULA_API_URL}/api/1/wallet/portfolio?wallet=0x9647BB6a598c2675310c512e0566B60a5aEE6261`, |
| 54 | + { |
| 55 | + headers: { |
| 56 | + 'Content-Type': 'application/json', |
| 57 | + authorization: MOBULA_API_KEY, |
| 58 | + }, |
| 59 | + } |
| 60 | + ) |
| 61 | + const portfolioResponseTime = Date.now() - portfolioTestStart |
| 62 | + |
| 63 | + const portfolioHealthy = portfolioResponse.ok |
| 64 | + |
| 65 | + // If portfolio API is down, throw error to return HTTP 500 |
| 66 | + if (!portfolioHealthy) { |
| 67 | + throw new Error(`Portfolio API returned ${portfolioResponse.status}`) |
| 68 | + } |
| 69 | + |
| 70 | + const totalResponseTime = Date.now() - startTime |
| 71 | + |
| 72 | + return NextResponse.json({ |
| 73 | + status: 'healthy', |
| 74 | + service: 'mobula', |
| 75 | + timestamp: new Date().toISOString(), |
| 76 | + responseTime: totalResponseTime, |
| 77 | + details: { |
| 78 | + priceApi: { |
| 79 | + status: 'healthy', |
| 80 | + responseTime: priceResponseTime, |
| 81 | + testAsset: 'USDC', |
| 82 | + price: priceData.data.price, |
| 83 | + }, |
| 84 | + portfolioApi: { |
| 85 | + status: 'healthy', |
| 86 | + responseTime: portfolioResponseTime, |
| 87 | + httpStatus: portfolioResponse.status, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }) |
| 91 | + } catch (error) { |
| 92 | + const totalResponseTime = Date.now() - startTime |
| 93 | + |
| 94 | + return NextResponse.json( |
| 95 | + { |
| 96 | + status: 'unhealthy', |
| 97 | + service: 'mobula', |
| 98 | + timestamp: new Date().toISOString(), |
| 99 | + error: error instanceof Error ? error.message : 'Unknown error', |
| 100 | + responseTime: totalResponseTime, |
| 101 | + }, |
| 102 | + { status: 500 } |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
0 commit comments