|
| 1 | +import { defineEventHandler, createError, getRouterParam, readBody } from 'h3' |
| 2 | + |
| 3 | +export default defineEventHandler(async (event) => { |
| 4 | + // #region agent log |
| 5 | + const logData = { |
| 6 | + location: 'server/api/cpus/[id]/cores.js:3', |
| 7 | + message: 'Cores API route entry', |
| 8 | + data: { |
| 9 | + url: event.node.req.url, |
| 10 | + method: event.node.req.method, |
| 11 | + hasAuthHeader: !!event.node.req.headers.authorization, |
| 12 | + nodeEnv: process.env.NODE_ENV, |
| 13 | + isPrerender: import.meta.prerender |
| 14 | + }, |
| 15 | + timestamp: Date.now(), |
| 16 | + sessionId: 'debug-session', |
| 17 | + runId: 'run1', |
| 18 | + hypothesisId: 'A' |
| 19 | + } |
| 20 | + try { |
| 21 | + await fetch('http://127.0.0.1:7242/ingest/a2e5b876-28c3-4b64-9549-c4e9792dd0b0', { |
| 22 | + method: 'POST', |
| 23 | + headers: { 'Content-Type': 'application/json' }, |
| 24 | + body: JSON.stringify(logData) |
| 25 | + }).catch(() => {}) |
| 26 | + } catch {} |
| 27 | + // #endregion |
| 28 | + |
| 29 | + if (import.meta.prerender) { |
| 30 | + throw createError({ |
| 31 | + statusCode: 404, |
| 32 | + statusMessage: 'Not available during build' |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + const cpuId = getRouterParam(event, 'id') |
| 38 | + const method = event.node.req.method |
| 39 | + |
| 40 | + if (!cpuId) { |
| 41 | + throw createError({ |
| 42 | + statusCode: 400, |
| 43 | + statusMessage: 'CPU ID is required' |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + const config = useRuntimeConfig() |
| 48 | + let backendUrl = config.public.backendUrl || 'http://localhost:3001' |
| 49 | + backendUrl = backendUrl.replace(/\/$/, '') |
| 50 | + const apiPrefix = backendUrl.endsWith('/api') ? '' : '/api' |
| 51 | + const url = `${backendUrl}${apiPrefix}/cpus/${cpuId}/cores` |
| 52 | + |
| 53 | + // #region agent log |
| 54 | + const logData2 = { |
| 55 | + location: 'server/api/cpus/[id]/cores.js:26', |
| 56 | + message: 'Backend URL constructed', |
| 57 | + data: { |
| 58 | + cpuId, |
| 59 | + method, |
| 60 | + backendUrl: config.public.backendUrl, |
| 61 | + constructedUrl: url, |
| 62 | + apiPrefix, |
| 63 | + hasBackendUrl: !!config.public.backendUrl |
| 64 | + }, |
| 65 | + timestamp: Date.now(), |
| 66 | + sessionId: 'debug-session', |
| 67 | + runId: 'run1', |
| 68 | + hypothesisId: 'B' |
| 69 | + } |
| 70 | + try { |
| 71 | + await fetch('http://127.0.0.1:7242/ingest/a2e5b876-28c3-4b64-9549-c4e9792dd0b0', { |
| 72 | + method: 'POST', |
| 73 | + headers: { 'Content-Type': 'application/json' }, |
| 74 | + body: JSON.stringify(logData2) |
| 75 | + }).catch(() => {}) |
| 76 | + } catch {} |
| 77 | + // #endregion |
| 78 | + |
| 79 | + // Get auth token from headers |
| 80 | + const authHeader = event.node.req.headers.authorization |
| 81 | + |
| 82 | + // #region agent log |
| 83 | + const logData3 = { |
| 84 | + location: 'server/api/cpus/[id]/cores.js:38', |
| 85 | + message: 'Auth header check', |
| 86 | + data: { |
| 87 | + hasAuthHeader: !!authHeader, |
| 88 | + authHeaderPrefix: authHeader ? authHeader.substring(0, 20) : null, |
| 89 | + method |
| 90 | + }, |
| 91 | + timestamp: Date.now(), |
| 92 | + sessionId: 'debug-session', |
| 93 | + runId: 'run1', |
| 94 | + hypothesisId: 'C' |
| 95 | + } |
| 96 | + try { |
| 97 | + await fetch('http://127.0.0.1:7242/ingest/a2e5b876-28c3-4b64-9549-c4e9792dd0b0', { |
| 98 | + method: 'POST', |
| 99 | + headers: { 'Content-Type': 'application/json' }, |
| 100 | + body: JSON.stringify(logData3) |
| 101 | + }).catch(() => {}) |
| 102 | + } catch {} |
| 103 | + // #endregion |
| 104 | + |
| 105 | + const options = { |
| 106 | + method, |
| 107 | + headers: { |
| 108 | + 'Content-Type': 'application/json', |
| 109 | + 'Accept': 'application/json' |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (authHeader) { |
| 114 | + options.headers['Authorization'] = authHeader |
| 115 | + } |
| 116 | + |
| 117 | + // Handle request body for POST/PUT |
| 118 | + if (method === 'POST' || method === 'PUT') { |
| 119 | + const body = await readBody(event) |
| 120 | + options.body = JSON.stringify(body) |
| 121 | + } |
| 122 | + |
| 123 | + // #region agent log |
| 124 | + const logData4 = { |
| 125 | + location: 'server/api/cpus/[id]/cores.js:60', |
| 126 | + message: 'Before backend fetch', |
| 127 | + data: { |
| 128 | + url, |
| 129 | + method, |
| 130 | + hasAuth: !!options.headers['Authorization'], |
| 131 | + hasBody: !!options.body |
| 132 | + }, |
| 133 | + timestamp: Date.now(), |
| 134 | + sessionId: 'debug-session', |
| 135 | + runId: 'run1', |
| 136 | + hypothesisId: 'D' |
| 137 | + } |
| 138 | + try { |
| 139 | + await fetch('http://127.0.0.1:7242/ingest/a2e5b876-28c3-4b64-9549-c4e9792dd0b0', { |
| 140 | + method: 'POST', |
| 141 | + headers: { 'Content-Type': 'application/json' }, |
| 142 | + body: JSON.stringify(logData4) |
| 143 | + }).catch(() => {}) |
| 144 | + } catch {} |
| 145 | + // #endregion |
| 146 | + |
| 147 | + const response = await fetch(url, options) |
| 148 | + |
| 149 | + // #region agent log |
| 150 | + const logData5 = { |
| 151 | + location: 'server/api/cpus/[id]/cores.js:68', |
| 152 | + message: 'Backend response received', |
| 153 | + data: { |
| 154 | + status: response.status, |
| 155 | + statusText: response.statusText, |
| 156 | + ok: response.ok, |
| 157 | + url |
| 158 | + }, |
| 159 | + timestamp: Date.now(), |
| 160 | + sessionId: 'debug-session', |
| 161 | + runId: 'run1', |
| 162 | + hypothesisId: 'E' |
| 163 | + } |
| 164 | + try { |
| 165 | + await fetch('http://127.0.0.1:7242/ingest/a2e5b876-28c3-4b64-9549-c4e9792dd0b0', { |
| 166 | + method: 'POST', |
| 167 | + headers: { 'Content-Type': 'application/json' }, |
| 168 | + body: JSON.stringify(logData5) |
| 169 | + }).catch(() => {}) |
| 170 | + } catch {} |
| 171 | + // #endregion |
| 172 | + |
| 173 | + if (!response.ok) { |
| 174 | + const errorText = await response.text() |
| 175 | + throw createError({ |
| 176 | + statusCode: response.status, |
| 177 | + statusMessage: `Backend API error: ${response.statusText}` |
| 178 | + }) |
| 179 | + } |
| 180 | + |
| 181 | + const data = await response.json() |
| 182 | + return data |
| 183 | + } catch (error) { |
| 184 | + // #region agent log |
| 185 | + const logData6 = { |
| 186 | + location: 'server/api/cpus/[id]/cores.js:85', |
| 187 | + message: 'Cores API error', |
| 188 | + data: { |
| 189 | + errorMessage: error.message, |
| 190 | + errorStatus: error.statusCode, |
| 191 | + errorStatusMessage: error.statusMessage, |
| 192 | + stack: error.stack?.substring(0, 200) |
| 193 | + }, |
| 194 | + timestamp: Date.now(), |
| 195 | + sessionId: 'debug-session', |
| 196 | + runId: 'run1', |
| 197 | + hypothesisId: 'F' |
| 198 | + } |
| 199 | + try { |
| 200 | + await fetch('http://127.0.0.1:7242/ingest/a2e5b876-28c3-4b64-9549-c4e9792dd0b0', { |
| 201 | + method: 'POST', |
| 202 | + headers: { 'Content-Type': 'application/json' }, |
| 203 | + body: JSON.stringify(logData6) |
| 204 | + }).catch(() => {}) |
| 205 | + } catch {} |
| 206 | + // #endregion |
| 207 | + |
| 208 | + console.error('Error in cores API:', error) |
| 209 | + throw createError({ |
| 210 | + statusCode: error.statusCode || 500, |
| 211 | + statusMessage: error.statusMessage || 'Failed to fetch cores data' |
| 212 | + }) |
| 213 | + } |
| 214 | +}) |
| 215 | + |
0 commit comments