Implementiere externes Backend auf 192.168.0.105:5000

- Erstelle API-Proxy-Routen für die Kommunikation mit dem externen Backend
- Füge API-Konfiguration mit 192.168.0.105:5000 als Backend-URL hinzu
- Erstelle Hilfsfunktionen für die externe API-Kommunikation
- Füge Skript zum Konfigurieren der Backend-URL hinzu
- Aktualisiere Docker-Compose mit der Backend-URL-Umgebungsvariable

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-04-01 13:25:15 +02:00
parent b53ffaec44
commit 273b78f12a
7 changed files with 342 additions and 4 deletions

View File

@@ -0,0 +1,99 @@
import { API_ENDPOINTS } from "@/utils/api-config";
export const dynamic = "force-dynamic";
export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const id = params.id;
// Rufe einzelnen Job vom externen Backend ab
const response = await fetch(`${API_ENDPOINTS.JOBS}/${id}`);
if (!response.ok) {
console.error(`Backend-Fehler: ${response.status} ${response.statusText}`);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 502,
headers: { 'Content-Type': 'application/json' }
});
}
const job = await response.json();
return Response.json(job);
} catch (error) {
console.error('Fehler beim Abrufen des Jobs vom Backend:', error);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}
export async function PUT(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const id = params.id;
const body = await request.json();
// Sende Job-Aktualisierung an das externe Backend
const response = await fetch(`${API_ENDPOINTS.JOBS}/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
console.error(`Backend-Fehler: ${response.status} ${response.statusText}`);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 502,
headers: { 'Content-Type': 'application/json' }
});
}
const result = await response.json();
return Response.json(result);
} catch (error) {
console.error('Fehler beim Aktualisieren des Jobs:', error);
return new Response(JSON.stringify({ error: 'Fehler beim Aktualisieren des Jobs' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}
export async function DELETE(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const id = params.id;
// Sende Job-Löschung an das externe Backend
const response = await fetch(`${API_ENDPOINTS.JOBS}/${id}`, {
method: 'DELETE'
});
if (!response.ok) {
console.error(`Backend-Fehler: ${response.status} ${response.statusText}`);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 502,
headers: { 'Content-Type': 'application/json' }
});
}
const result = await response.json();
return Response.json(result);
} catch (error) {
console.error('Fehler beim Löschen des Jobs:', error);
return new Response(JSON.stringify({ error: 'Fehler beim Löschen des Jobs' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}

View File

@@ -0,0 +1,59 @@
import { API_ENDPOINTS } from "@/utils/api-config";
export const dynamic = "force-dynamic";
export async function GET() {
try {
// Rufe Jobs vom externen Backend ab
const response = await fetch(API_ENDPOINTS.JOBS);
if (!response.ok) {
console.error(`Backend-Fehler: ${response.status} ${response.statusText}`);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 502,
headers: { 'Content-Type': 'application/json' }
});
}
const jobs = await response.json();
return Response.json(jobs);
} catch (error) {
console.error('Fehler beim Abrufen der Jobs vom Backend:', error);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}
export async function POST(request: Request) {
try {
const body = await request.json();
// Sende Job-Erstellung an das externe Backend
const response = await fetch(API_ENDPOINTS.JOBS, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
console.error(`Backend-Fehler: ${response.status} ${response.statusText}`);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 502,
headers: { 'Content-Type': 'application/json' }
});
}
const result = await response.json();
return Response.json(result);
} catch (error) {
console.error('Fehler beim Erstellen des Jobs:', error);
return new Response(JSON.stringify({ error: 'Fehler beim Erstellen des Jobs' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}

View File

@@ -1,9 +1,27 @@
import { getPrinters } from "@/server/actions/printers";
import { API_ENDPOINTS } from "@/utils/api-config";
export const dynamic = "force-dynamic";
export async function GET() {
const printers = await getPrinters();
return Response.json(printers);
try {
// Rufe Drucker vom externen Backend ab statt von der lokalen Datenbank
const response = await fetch(API_ENDPOINTS.PRINTERS);
if (!response.ok) {
console.error(`Backend-Fehler: ${response.status} ${response.statusText}`);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 502,
headers: { 'Content-Type': 'application/json' }
});
}
const printers = await response.json();
return Response.json(printers);
} catch (error) {
console.error('Fehler beim Abrufen der Drucker vom Backend:', error);
return new Response(JSON.stringify({ error: 'Backend nicht erreichbar' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}

View File

@@ -0,0 +1,9 @@
// Basis-URL für Backend-API
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://192.168.0.105:5000";
// Endpunkte für die verschiedenen Ressourcen
export const API_ENDPOINTS = {
PRINTERS: `${API_BASE_URL}/api/printers`,
JOBS: `${API_BASE_URL}/api/jobs`,
USERS: `${API_BASE_URL}/api/users`,
};

View File

@@ -0,0 +1,78 @@
import { API_ENDPOINTS } from './api-config';
// Typdefinitionen für API-Responses
export interface Printer {
id: string;
name: string;
ip: string;
status: string;
is_enabled: boolean;
}
export interface Job {
id: string;
printer_id: string;
user_id: string;
start_time: string;
end_time: string;
status: string;
}
// Fetcher für SWR mit Fehlerbehandlung
const fetchWithErrorHandling = async (url: string) => {
const response = await fetch(url);
if (!response.ok) {
const error = new Error('Ein Fehler ist bei der API-Anfrage aufgetreten');
throw error;
}
return response.json();
};
// API-Funktionen
export const api = {
// Drucker-Endpunkte
printers: {
getAll: () => fetchWithErrorHandling(API_ENDPOINTS.PRINTERS),
getById: (id: string) => fetchWithErrorHandling(`${API_ENDPOINTS.PRINTERS}/${id}`),
create: (data: Partial<Printer>) =>
fetch(API_ENDPOINTS.PRINTERS, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
}).then(res => res.json()),
update: (id: string, data: Partial<Printer>) =>
fetch(`${API_ENDPOINTS.PRINTERS}/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
}).then(res => res.json()),
delete: (id: string) =>
fetch(`${API_ENDPOINTS.PRINTERS}/${id}`, {
method: 'DELETE',
}).then(res => res.json()),
},
// Jobs-Endpunkte
jobs: {
getAll: () => fetchWithErrorHandling(API_ENDPOINTS.JOBS),
getById: (id: string) => fetchWithErrorHandling(`${API_ENDPOINTS.JOBS}/${id}`),
create: (data: Partial<Job>) =>
fetch(API_ENDPOINTS.JOBS, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
}).then(res => res.json()),
update: (id: string, data: Partial<Job>) =>
fetch(`${API_ENDPOINTS.JOBS}/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
}).then(res => res.json()),
delete: (id: string) =>
fetch(`${API_ENDPOINTS.JOBS}/${id}`, {
method: 'DELETE',
}).then(res => res.json()),
},
};