mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-25 03:31:44 +00:00
redirect endpoint for localhost python API
This commit is contained in:
parent
bde03f5da4
commit
f2f702099f
@ -10,10 +10,7 @@
|
||||
clearMatrix();
|
||||
const form = event.target as HTMLFormElement;
|
||||
|
||||
// TODO: Calling API via server-side
|
||||
const response = await fetch(`http://localhost:8080/${form.dataset.endpoint}`, {
|
||||
mode: 'cors'
|
||||
});
|
||||
const response = await fetch(`/api/redirect?endpoint=${form.dataset.endpoint}`);
|
||||
const mdata = (await response.json()) as APIResponse;
|
||||
if (!mdata.success) alert(`Error while processing '${form.dataset.endpoint}'!`);
|
||||
}
|
||||
@ -40,10 +37,9 @@
|
||||
// break;
|
||||
}
|
||||
|
||||
// TODO: Calling API via server-side
|
||||
const response = await fetch(`http://localhost:8080/${form.dataset.endpoint}`, {
|
||||
method: form.method,
|
||||
mode: 'cors',
|
||||
fdata.append('endpoint', form.dataset.endpoint ?? '');
|
||||
const response = await fetch('/api/redirect', {
|
||||
method: 'POST',
|
||||
body: fdata
|
||||
});
|
||||
const mdata = (await response.json()) as APIResponse;
|
||||
@ -116,11 +112,10 @@
|
||||
|
||||
fdata.append('x', x.toString());
|
||||
fdata.append('y', y.toString());
|
||||
fdata.append('endpoint', isScaled ? 'rectangle' : 'pixel');
|
||||
|
||||
// TODO: Calling API via server-side
|
||||
const response = await fetch(`http://localhost:8080/${isScaled ? 'rectangle' : 'pixel'}`, {
|
||||
const response = await fetch('/api/redirect', {
|
||||
method: 'POST',
|
||||
mode: 'cors',
|
||||
body: fdata
|
||||
});
|
||||
const mdata = (await response.json()) as APIResponse;
|
31
Webserver/src/routes/api/redirect/+server.ts
Normal file
31
Webserver/src/routes/api/redirect/+server.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import type { APIResponse } from "$lib/interfaces";
|
||||
import { json, type RequestHandler } from "@sveltejs/kit";
|
||||
|
||||
export const GET: RequestHandler = async ({ url }) => {
|
||||
const response = { success: false } as APIResponse;
|
||||
|
||||
const params = url.searchParams;
|
||||
const endpoint = params.get('endpoint');
|
||||
if (endpoint === null) return json(response);
|
||||
|
||||
const api_call = await fetch(`http://localhost:8080/${endpoint}`);
|
||||
const data = await api_call.json() as APIResponse;
|
||||
return json(data);
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
const response = { success: false } as APIResponse;
|
||||
|
||||
const formData = await request.formData();
|
||||
const endpoint = formData.get('endpoint');
|
||||
if (endpoint === null) return json(response);
|
||||
|
||||
formData.delete('endpoint');
|
||||
const api_call = await fetch(`http://localhost:8080/${endpoint}`, {
|
||||
method: 'POST',
|
||||
mode: 'cors',
|
||||
body: formData
|
||||
});
|
||||
const data = await api_call.json() as APIResponse;
|
||||
return json(data);
|
||||
};
|
Loading…
Reference in New Issue
Block a user