diff --git a/Webserver/src/routes/+page.svelte b/Webserver/src/routes/admin/+page.svelte similarity index 96% rename from Webserver/src/routes/+page.svelte rename to Webserver/src/routes/admin/+page.svelte index e4c3642..cfb22e8 100644 --- a/Webserver/src/routes/+page.svelte +++ b/Webserver/src/routes/admin/+page.svelte @@ -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; diff --git a/Webserver/src/routes/api/redirect/+server.ts b/Webserver/src/routes/api/redirect/+server.ts new file mode 100644 index 0000000..272271b --- /dev/null +++ b/Webserver/src/routes/api/redirect/+server.ts @@ -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); +}; \ No newline at end of file