diff --git a/apps/backoffice/app/api/books/[bookId]/pages/[pageNum]/route.ts b/apps/backoffice/app/api/books/[bookId]/pages/[pageNum]/route.ts index efc4702..12f17f2 100644 --- a/apps/backoffice/app/api/books/[bookId]/pages/[pageNum]/route.ts +++ b/apps/backoffice/app/api/books/[bookId]/pages/[pageNum]/route.ts @@ -1,35 +1,25 @@ import { NextRequest, NextResponse } from "next/server"; +import { config } from "@/lib/api"; export async function GET( request: NextRequest, { params }: { params: Promise<{ bookId: string; pageNum: string }> } ) { const { bookId, pageNum } = await params; - - // Récupérer les query params (format, width, quality) - const { searchParams } = new URL(request.url); - const format = searchParams.get("format") || "webp"; - const width = searchParams.get("width") || ""; - const quality = searchParams.get("quality") || ""; - - // Construire l'URL vers l'API backend - const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; - const apiUrl = new URL(`${apiBaseUrl}/books/${bookId}/pages/${pageNum}`); - apiUrl.searchParams.set("format", format); - if (width) apiUrl.searchParams.set("width", width); - if (quality) apiUrl.searchParams.set("quality", quality); - - // Faire la requête à l'API - const token = process.env.API_BOOTSTRAP_TOKEN; - if (!token) { - return new NextResponse("API token not configured", { status: 500 }); - } - try { + const { baseUrl, token } = config(); + const { searchParams } = new URL(request.url); + const format = searchParams.get("format") || "webp"; + const width = searchParams.get("width") || ""; + const quality = searchParams.get("quality") || ""; + + const apiUrl = new URL(`${baseUrl}/books/${bookId}/pages/${pageNum}`); + apiUrl.searchParams.set("format", format); + if (width) apiUrl.searchParams.set("width", width); + if (quality) apiUrl.searchParams.set("quality", quality); + const response = await fetch(apiUrl.toString(), { - headers: { - Authorization: `Bearer ${token}`, - }, + headers: { Authorization: `Bearer ${token}` }, }); if (!response.ok) { diff --git a/apps/backoffice/app/api/books/[bookId]/thumbnail/route.ts b/apps/backoffice/app/api/books/[bookId]/thumbnail/route.ts index 22c2a96..c412c3e 100644 --- a/apps/backoffice/app/api/books/[bookId]/thumbnail/route.ts +++ b/apps/backoffice/app/api/books/[bookId]/thumbnail/route.ts @@ -1,4 +1,5 @@ import { NextRequest, NextResponse } from "next/server"; +import { config } from "@/lib/api"; export async function GET( request: NextRequest, @@ -6,19 +7,10 @@ export async function GET( ) { const { bookId } = await params; - const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; - const apiUrl = `${apiBaseUrl}/books/${bookId}/thumbnail`; - - const token = process.env.API_BOOTSTRAP_TOKEN; - if (!token) { - return new NextResponse("API token not configured", { status: 500 }); - } - try { - const response = await fetch(apiUrl, { - headers: { - Authorization: `Bearer ${token}`, - }, + const { baseUrl, token } = config(); + const response = await fetch(`${baseUrl}/books/${bookId}/thumbnail`, { + headers: { Authorization: `Bearer ${token}` }, }); if (!response.ok) { diff --git a/apps/backoffice/app/api/folders/route.ts b/apps/backoffice/app/api/folders/route.ts index 37fb8cb..7f55d09 100644 --- a/apps/backoffice/app/api/folders/route.ts +++ b/apps/backoffice/app/api/folders/route.ts @@ -1,39 +1,13 @@ import { NextRequest, NextResponse } from "next/server"; +import { listFolders } from "@/lib/api"; export async function GET(request: NextRequest) { - const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; - const apiToken = process.env.API_BOOTSTRAP_TOKEN; - - if (!apiToken) { - return NextResponse.json({ error: "API token not configured" }, { status: 500 }); - } - try { const { searchParams } = new URL(request.url); - const path = searchParams.get("path"); - - let apiUrl = `${apiBaseUrl}/folders`; - if (path) { - apiUrl += `?path=${encodeURIComponent(path)}`; - } - - const response = await fetch(apiUrl, { - headers: { - Authorization: `Bearer ${apiToken}`, - }, - }); - - if (!response.ok) { - return NextResponse.json( - { error: `API error: ${response.status}` }, - { status: response.status } - ); - } - - const data = await response.json(); + const path = searchParams.get("path") || undefined; + const data = await listFolders(path); return NextResponse.json(data); } catch (error) { - console.error("Proxy error:", error); return NextResponse.json({ error: "Failed to fetch folders" }, { status: 500 }); } } diff --git a/apps/backoffice/app/api/jobs/[id]/cancel/route.ts b/apps/backoffice/app/api/jobs/[id]/cancel/route.ts index 7d47f12..edbbeed 100644 --- a/apps/backoffice/app/api/jobs/[id]/cancel/route.ts +++ b/apps/backoffice/app/api/jobs/[id]/cancel/route.ts @@ -1,36 +1,15 @@ import { NextRequest, NextResponse } from "next/server"; +import { cancelJob } from "@/lib/api"; export async function POST( - request: NextRequest, + _request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { const { id } = await params; - const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; - const apiToken = process.env.API_BOOTSTRAP_TOKEN; - - if (!apiToken) { - return NextResponse.json({ error: "API token not configured" }, { status: 500 }); - } - try { - const response = await fetch(`${apiBaseUrl}/index/cancel/${id}`, { - method: "POST", - headers: { - Authorization: `Bearer ${apiToken}`, - }, - }); - - if (!response.ok) { - return NextResponse.json( - { error: `API error: ${response.status}` }, - { status: response.status } - ); - } - - const data = await response.json(); + const data = await cancelJob(id); return NextResponse.json(data); } catch (error) { - console.error("Proxy error:", error); return NextResponse.json({ error: "Failed to cancel job" }, { status: 500 }); } } diff --git a/apps/backoffice/app/api/jobs/[id]/route.ts b/apps/backoffice/app/api/jobs/[id]/route.ts index eee6fc0..e85fee3 100644 --- a/apps/backoffice/app/api/jobs/[id]/route.ts +++ b/apps/backoffice/app/api/jobs/[id]/route.ts @@ -1,35 +1,15 @@ import { NextRequest, NextResponse } from "next/server"; +import { apiFetch, IndexJobDto } from "@/lib/api"; export async function GET( - request: NextRequest, + _request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { const { id } = await params; - const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; - const apiToken = process.env.API_BOOTSTRAP_TOKEN; - - if (!apiToken) { - return NextResponse.json({ error: "API token not configured" }, { status: 500 }); - } - try { - const response = await fetch(`${apiBaseUrl}/index/jobs/${id}`, { - headers: { - Authorization: `Bearer ${apiToken}`, - }, - }); - - if (!response.ok) { - return NextResponse.json( - { error: `API error: ${response.status}` }, - { status: response.status } - ); - } - - const data = await response.json(); + const data = await apiFetch(`/index/jobs/${id}`); return NextResponse.json(data); } catch (error) { - console.error("Proxy error:", error); return NextResponse.json({ error: "Failed to fetch job" }, { status: 500 }); } } diff --git a/apps/backoffice/app/api/jobs/[id]/stream/route.ts b/apps/backoffice/app/api/jobs/[id]/stream/route.ts index 33617e6..a9bae3e 100644 --- a/apps/backoffice/app/api/jobs/[id]/stream/route.ts +++ b/apps/backoffice/app/api/jobs/[id]/stream/route.ts @@ -1,20 +1,13 @@ import { NextRequest } from "next/server"; +import { config } from "@/lib/api"; export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { const { id } = await params; - const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; - const apiToken = process.env.API_BOOTSTRAP_TOKEN; - - if (!apiToken) { - return new Response( - `data: ${JSON.stringify({ error: "API token not configured" })}\n\n`, - { status: 500, headers: { "Content-Type": "text/event-stream" } } - ); - } - + const { baseUrl, token } = config(); + const stream = new ReadableStream({ async start(controller) { // Send initial headers for SSE @@ -27,10 +20,8 @@ export async function GET( if (!isActive) return; try { - const response = await fetch(`${apiBaseUrl}/index/jobs/${id}`, { - headers: { - Authorization: `Bearer ${apiToken}`, - }, + const response = await fetch(`${baseUrl}/index/jobs/${id}`, { + headers: { Authorization: `Bearer ${token}` }, }); if (response.ok && isActive) { diff --git a/apps/backoffice/app/api/jobs/active/route.ts b/apps/backoffice/app/api/jobs/active/route.ts new file mode 100644 index 0000000..4b22096 --- /dev/null +++ b/apps/backoffice/app/api/jobs/active/route.ts @@ -0,0 +1,11 @@ +import { NextResponse } from "next/server"; +import { apiFetch, IndexJobDto } from "@/lib/api"; + +export async function GET() { + try { + const data = await apiFetch("/index/jobs/active"); + return NextResponse.json(data); + } catch (error) { + return NextResponse.json({ error: "Failed to fetch active jobs" }, { status: 500 }); + } +} diff --git a/apps/backoffice/app/api/jobs/route.ts b/apps/backoffice/app/api/jobs/route.ts deleted file mode 100644 index 15c2cb1..0000000 --- a/apps/backoffice/app/api/jobs/route.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; - -export async function GET(request: NextRequest) { - const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; - const apiToken = process.env.API_BOOTSTRAP_TOKEN; - - if (!apiToken) { - return NextResponse.json({ error: "API token not configured" }, { status: 500 }); - } - - try { - const response = await fetch(`${apiBaseUrl}/index/status`, { - headers: { - Authorization: `Bearer ${apiToken}`, - }, - }); - - if (!response.ok) { - return NextResponse.json( - { error: `API error: ${response.status}` }, - { status: response.status } - ); - } - - const data = await response.json(); - return NextResponse.json(data); - } catch (error) { - console.error("Proxy error:", error); - return NextResponse.json({ error: "Failed to fetch jobs" }, { status: 500 }); - } -} diff --git a/apps/backoffice/app/api/jobs/stream/route.ts b/apps/backoffice/app/api/jobs/stream/route.ts index 4e4e282..f0a4415 100644 --- a/apps/backoffice/app/api/jobs/stream/route.ts +++ b/apps/backoffice/app/api/jobs/stream/route.ts @@ -1,16 +1,9 @@ import { NextRequest } from "next/server"; +import { config } from "@/lib/api"; export async function GET(request: NextRequest) { - const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; - const apiToken = process.env.API_BOOTSTRAP_TOKEN; - - if (!apiToken) { - return new Response( - `data: ${JSON.stringify({ error: "API token not configured" })}\n\n`, - { status: 500, headers: { "Content-Type": "text/event-stream" } } - ); - } - + const { baseUrl, token } = config(); + const stream = new ReadableStream({ async start(controller) { controller.enqueue(new TextEncoder().encode("")); @@ -22,10 +15,8 @@ export async function GET(request: NextRequest) { if (!isActive) return; try { - const response = await fetch(`${apiBaseUrl}/index/status`, { - headers: { - Authorization: `Bearer ${apiToken}`, - }, + const response = await fetch(`${baseUrl}/index/status`, { + headers: { Authorization: `Bearer ${token}` }, }); if (response.ok && isActive) { diff --git a/apps/backoffice/app/api/settings/[key]/route.ts b/apps/backoffice/app/api/settings/[key]/route.ts index a3891b1..48fb645 100644 --- a/apps/backoffice/app/api/settings/[key]/route.ts +++ b/apps/backoffice/app/api/settings/[key]/route.ts @@ -1,29 +1,16 @@ import { NextRequest, NextResponse } from "next/server"; +import { apiFetch, updateSetting } from "@/lib/api"; export async function GET( - request: NextRequest, + _request: NextRequest, { params }: { params: Promise<{ key: string }> } ) { + const { key } = await params; try { - const { key } = await params; - const baseUrl = process.env.API_BASE_URL || "http://api:7080"; - const token = process.env.API_BOOTSTRAP_TOKEN; - - const response = await fetch(`${baseUrl}/settings/${key}`, { - headers: { - Authorization: `Bearer ${token}`, - }, - cache: "no-store" - }); - - if (!response.ok) { - return NextResponse.json({ error: "Failed to fetch setting" }, { status: response.status }); - } - - const data = await response.json(); + const data = await apiFetch(`/settings/${key}`); return NextResponse.json(data); } catch (error) { - return NextResponse.json({ error: "Internal server error" }, { status: 500 }); + return NextResponse.json({ error: "Failed to fetch setting" }, { status: 500 }); } } @@ -31,29 +18,12 @@ export async function POST( request: NextRequest, { params }: { params: Promise<{ key: string }> } ) { + const { key } = await params; try { - const { key } = await params; - const baseUrl = process.env.API_BASE_URL || "http://api:7080"; - const token = process.env.API_BOOTSTRAP_TOKEN; - const body = await request.json(); - - const response = await fetch(`${baseUrl}/settings/${key}`, { - method: "POST", - headers: { - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }, - body: JSON.stringify(body), - cache: "no-store" - }); - - if (!response.ok) { - return NextResponse.json({ error: "Failed to update setting" }, { status: response.status }); - } - - const data = await response.json(); + const { value } = await request.json(); + const data = await updateSetting(key, value); return NextResponse.json(data); } catch (error) { - return NextResponse.json({ error: "Internal server error" }, { status: 500 }); + return NextResponse.json({ error: "Failed to update setting" }, { status: 500 }); } } diff --git a/apps/backoffice/app/api/settings/cache/clear/route.ts b/apps/backoffice/app/api/settings/cache/clear/route.ts index a462dfd..a7eb71a 100644 --- a/apps/backoffice/app/api/settings/cache/clear/route.ts +++ b/apps/backoffice/app/api/settings/cache/clear/route.ts @@ -1,25 +1,11 @@ -import { NextRequest, NextResponse } from "next/server"; +import { NextResponse } from "next/server"; +import { clearCache } from "@/lib/api"; -export async function POST(request: NextRequest) { +export async function POST() { try { - const baseUrl = process.env.API_BASE_URL || "http://api:7080"; - const token = process.env.API_BOOTSTRAP_TOKEN; - - const response = await fetch(`${baseUrl}/settings/cache/clear`, { - method: "POST", - headers: { - Authorization: `Bearer ${token}`, - }, - cache: "no-store" - }); - - if (!response.ok) { - return NextResponse.json({ error: "Failed to clear cache" }, { status: response.status }); - } - - const data = await response.json(); + const data = await clearCache(); return NextResponse.json(data); } catch (error) { - return NextResponse.json({ error: "Internal server error" }, { status: 500 }); + return NextResponse.json({ error: "Failed to clear cache" }, { status: 500 }); } } diff --git a/apps/backoffice/app/api/settings/cache/stats/route.ts b/apps/backoffice/app/api/settings/cache/stats/route.ts index 6db496a..8e888a5 100644 --- a/apps/backoffice/app/api/settings/cache/stats/route.ts +++ b/apps/backoffice/app/api/settings/cache/stats/route.ts @@ -1,24 +1,11 @@ -import { NextRequest, NextResponse } from "next/server"; +import { NextResponse } from "next/server"; +import { getCacheStats } from "@/lib/api"; -export async function GET(request: NextRequest) { +export async function GET() { try { - const baseUrl = process.env.API_BASE_URL || "http://api:7080"; - const token = process.env.API_BOOTSTRAP_TOKEN; - - const response = await fetch(`${baseUrl}/settings/cache/stats`, { - headers: { - Authorization: `Bearer ${token}`, - }, - cache: "no-store" - }); - - if (!response.ok) { - return NextResponse.json({ error: "Failed to fetch cache stats" }, { status: response.status }); - } - - const data = await response.json(); + const data = await getCacheStats(); return NextResponse.json(data); } catch (error) { - return NextResponse.json({ error: "Internal server error" }, { status: 500 }); + return NextResponse.json({ error: "Failed to fetch cache stats" }, { status: 500 }); } } diff --git a/apps/backoffice/app/api/settings/route.ts b/apps/backoffice/app/api/settings/route.ts deleted file mode 100644 index 41b838a..0000000 --- a/apps/backoffice/app/api/settings/route.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; - -export async function GET(request: NextRequest) { - try { - const baseUrl = process.env.API_BASE_URL || "http://api:7080"; - const token = process.env.API_BOOTSTRAP_TOKEN; - - const response = await fetch(`${baseUrl}/settings`, { - headers: { - Authorization: `Bearer ${token}`, - }, - cache: "no-store" - }); - - if (!response.ok) { - return NextResponse.json({ error: "Failed to fetch settings" }, { status: response.status }); - } - - const data = await response.json(); - return NextResponse.json(data); - } catch (error) { - return NextResponse.json({ error: "Internal server error" }, { status: 500 }); - } -} diff --git a/apps/backoffice/lib/api.ts b/apps/backoffice/lib/api.ts index e75cf75..40e3f8e 100644 --- a/apps/backoffice/lib/api.ts +++ b/apps/backoffice/lib/api.ts @@ -89,7 +89,7 @@ export type SeriesDto = { first_book_id: string; }; -function config() { +export function config() { const baseUrl = process.env.API_BASE_URL || "http://api:7080"; const token = process.env.API_BOOTSTRAP_TOKEN; if (!token) { diff --git a/apps/backoffice/tsconfig.json b/apps/backoffice/tsconfig.json index e0c8e33..002f750 100644 --- a/apps/backoffice/tsconfig.json +++ b/apps/backoffice/tsconfig.json @@ -21,7 +21,10 @@ { "name": "next" } - ] + ], + "paths": { + "@/*": ["./*"] + } }, "include": [ "next-env.d.ts",