Spaces:
Sleeping
Sleeping
Caner Giden commited on
Commit ·
ee7a049
1
Parent(s): e20442d
B1: Core bağlantısını sağlamlaştır - Server-side proxy pattern ve güvenlik güncellemeleri
Browse files- /api/core-health endpoint'ini güçlendirildi (timeout, error handling)
- Client-side'dan server-side API route üzerinden proxy yapısı
- Next.js güvenlik açığı giderildi (14.2.5 -> 14.2.18)
- TypeScript type tanımları eklendi (@types /node, @types /react, etc.)
- React import düzeltmesi (app/layout.tsx)
- tsconfig.json Next.js 14 için optimize edildi
Files changed (6)
- app/api/core-health/route.ts+72-9
- app/layout.tsx+3-1
- next-env.d.tsAdded+6
- package-lock.json+72
- package.json+9-3
- tsconfig.json+16-6
1234567891011121314151617181920212223242526272829303132import { NextResponse } from "next/server";export async function GET() {const base = process.env.NEXT_PUBLIC_API_BASE; // HF Space variableif (!base) {return NextResponse.json({ ok: false, error: "NEXT_PUBLIC_API_BASE missing" },{ status: 500 });}try {const res = await fetch(`${base.replace(/\/$/, "")}/health`, {cache: "no-store",});const text = await res.text(); // json parse fail olursa görmek içinlet data: any;try {data = JSON.parse(text);} catch {data = { raw: text };}return NextResponse.json({ ok: res.ok, status: res.status, data });} catch (e: any) {return NextResponse.json({ ok: false, error: e?.message ?? "proxy fetch failed" },{ status: 500 });}}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import { NextResponse } from "next/server";/*** Server-side proxy endpoint for Core health check* /api/core-health → Core /health*/export async function GET() {// Support both NEXT_PUBLIC_API_BASE (for compatibility) and CORE_API_BASE (server-only)const base = process.env.CORE_API_BASE || process.env.NEXT_PUBLIC_API_BASE;if (!base) {return NextResponse.json({ok: false,error: "Core API base URL not configured. Set CORE_API_BASE or NEXT_PUBLIC_API_BASE environment variable."},{ status: 500 });}const coreUrl = `${base.replace(/\/$/, "")}/health`;const timeoutMs = 5000; // 5 second timeouttry {// Create abort controller for timeoutconst controller = new AbortController();const timeoutId = setTimeout(() => controller.abort(), timeoutMs);const res = await fetch(coreUrl, {cache: "no-store",signal: controller.signal,headers: {"Content-Type": "application/json",},});clearTimeout(timeoutId);if (!res.ok) {return NextResponse.json({ok: false,error: `Core health check failed with status ${res.status}`,status: res.status,},{ status: 502 });}// Try to parse JSON responselet data: any;const text = await res.text();try {data = JSON.parse(text);} catch (parseError) {// If JSON parse fails, return raw textreturn NextResponse.json({ok: false,error: "Core returned invalid JSON response",raw: text,},{ status: 502 });}// Return normalized response// If Core returns { ok: true, db_ok: true }, it will be in datareturn NextResponse.json({ok: true,status: res.status,data,});} catch (e: any) {// Handle timeout and other errorsif (e.name === "AbortError") {return NextResponse.json({ok: false,error: `Core health check timed out after ${timeoutMs}ms`,},{ status: 504 });}return NextResponse.json({ok: false,error: e?.message ?? "Failed to connect to Core service",details: process.env.NODE_ENV === "development" ? String(e) : undefined,},{ status: 502 });}}
123456789export const metadata = {title: "ForgeFlow Studio",description: "ForgeFlow UI"};export default function RootLayout({ children }: { children: React.ReactNode }) {return (<html lang="tr"><body style={{ margin: 0, fontFamily: "ui-sans-serif, system-ui" }}>1234567891011import { ReactNode } from "react";export const metadata = {title: "ForgeFlow Studio",description: "ForgeFlow UI"};export default function RootLayout({ children }: { children: ReactNode }) {return (<html lang="tr"><body style={{ margin: 0, fontFamily: "ui-sans-serif, system-ui" }}>
123456/// <reference types="next" />/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited// see https://nextjs.org/docs/basic-features/typescript for more information.
10 unmodified lines111213141516162 unmodified lines17918018118218318431 unmodified lines216217218219220221200 unmodified lines42242342442542642710 unmodified lines"next": "14.2.5","react": "18.3.1","react-dom": "18.3.1"}},"node_modules/@next/env": {162 unmodified lines"tslib": "^2.4.0"}},"node_modules/busboy": {"version": "1.6.0","resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz",31 unmodified lines"integrity": "sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==","license": "MIT"},"node_modules/graceful-fs": {"version": "4.2.11","resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",200 unmodified lines"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz","integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==","license": "0BSD"}}}10 unmodified lines111213141516171819202122162 unmodified lines18518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722831 unmodified lines260261262263264265266267268269270271272200 unmodified lines47347447547647747847948048148248348448548648748848949049149249349449549649749849910 unmodified lines"next": "14.2.5","react": "18.3.1","react-dom": "18.3.1"},"devDependencies": {"@types/node": "^20.0.0","@types/react": "^18.3.0","@types/react-dom": "^18.3.0","typescript": "^5.0.0"}},"node_modules/@next/env": {162 unmodified lines"tslib": "^2.4.0"}},"node_modules/@types/node": {"version": "20.19.27","resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.27.tgz","integrity": "sha512-N2clP5pJhB2YnZJ3PIHFk5RkygRX5WO/5f0WC08tp0wd+sv0rsJk3MqWn3CbNmT2J505a5336jaQj4ph1AdMug==","dev": true,"license": "MIT","dependencies": {"undici-types": "~6.21.0"}},"node_modules/@types/prop-types": {"version": "15.7.15","resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.15.tgz","integrity": "sha512-F6bEyamV9jKGAFBEmlQnesRPGOQqS2+Uwi0Em15xenOxHaf2hv6L8YCVn3rPdPJOiJfPiCnLIRyvwVaqMY3MIw==","dev": true,"license": "MIT"},"node_modules/@types/react": {"version": "18.3.27","resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.27.tgz","integrity": "sha512-cisd7gxkzjBKU2GgdYrTdtQx1SORymWyaAFhaxQPK9bYO9ot3Y5OikQRvY0VYQtvwjeQnizCINJAenh/V7MK2w==","dev": true,"license": "MIT","dependencies": {"@types/prop-types": "*","csstype": "^3.2.2"}},"node_modules/@types/react-dom": {"version": "18.3.7","resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-18.3.7.tgz","integrity": "sha512-MEe3UeoENYVFXzoXEWsvcpg6ZvlrFNlOQ7EOsvhI3CfAXwzPfO8Qwuxd40nepsYKqyyVQnTdEfv68q91yLcKrQ==","dev": true,"license": "MIT","peerDependencies": {"@types/react": "^18.0.0"}},"node_modules/busboy": {"version": "1.6.0","resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz",31 unmodified lines"integrity": "sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==","license": "MIT"},"node_modules/csstype": {"version": "3.2.3","resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz","integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==","dev": true,"license": "MIT"},"node_modules/graceful-fs": {"version": "4.2.11","resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",200 unmodified lines"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz","integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==","license": "0BSD"},"node_modules/typescript": {"version": "5.9.3","resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz","integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==","dev": true,"license": "Apache-2.0","bin": {"tsc": "bin/tsc","tsserver": "bin/tsserver"},"engines": {"node": ">=14.17"}},"node_modules/undici-types": {"version": "6.21.0","resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz","integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==","dev": true,"license": "MIT"}}}
7 unmodified lines891011121314157 unmodified lines"start": "next start"},"dependencies": {"next": "14.2.5","react": "18.3.1","react-dom": "18.3.1"}}7 unmodified lines891011121314151617181920217 unmodified lines"start": "next start"},"dependencies": {"next": "^14.2.18","react": "^18.3.1","react-dom": "^18.3.1"},"devDependencies": {"@types/node": "^20.0.0","@types/react": "^18.3.0","@types/react-dom": "^18.3.0","typescript": "^5.0.0"}}
123456789101112131415161718{"compilerOptions": {"target": "ES2022","lib": ["dom", "dom.iterable", "es2022"],"allowJs": false,"skipLibCheck": true,"strict": true,"noEmit": true,"module": "ESNext","moduleResolution": "Bundler","resolveJsonModule": true,"isolatedModules": true,"jsx": "preserve","incremental": true},"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],"exclude": ["node_modules"]}12345678910111213141516171819202122232425262728{"compilerOptions": {"target": "ES2022","lib": ["dom", "dom.iterable", "esnext"],"allowJs": true,"skipLibCheck": true,"strict": true,"noEmit": true,"esModuleInterop": true,"module": "esnext","moduleResolution": "bundler","resolveJsonModule": true,"isolatedModules": true,"jsx": "preserve","incremental": true,"plugins": [{"name": "next"}],"paths": {"@/*": ["./*"]},"forceConsistentCasingInFileNames": true},"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],"exclude": ["node_modules"]}
app/api/core-health/route.ts+72-9
app/layout.tsx+3-1
next-env.d.tsAdded+6
package-lock.json+72
package.json+9-3
tsconfig.json+16-6