From 044bad3f6026d2b710c64f490472d9338513485e Mon Sep 17 00:00:00 2001 From: waltem01 Date: Sun, 18 Jun 2023 18:44:28 +0200 Subject: [PATCH] api server access in env --- Webserver/.env.example | 2 ++ Webserver/src/lib/server/buildAPIStr.ts | 5 +++++ Webserver/src/routes/api/redirect/+server.ts | 5 +++-- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 Webserver/.env.example create mode 100644 Webserver/src/lib/server/buildAPIStr.ts diff --git a/Webserver/.env.example b/Webserver/.env.example new file mode 100644 index 0000000..ee9cb4c --- /dev/null +++ b/Webserver/.env.example @@ -0,0 +1,2 @@ +API_SERVER_IP= +API_SERVER_PORT= \ No newline at end of file diff --git a/Webserver/src/lib/server/buildAPIStr.ts b/Webserver/src/lib/server/buildAPIStr.ts new file mode 100644 index 0000000..603c400 --- /dev/null +++ b/Webserver/src/lib/server/buildAPIStr.ts @@ -0,0 +1,5 @@ +import { API_SERVER_IP, API_SERVER_PORT } from '$env/static/private'; + +export function buildAPIStr(endpoint: string) { + return `http://${API_SERVER_IP ?? 'localhost'}:${API_SERVER_PORT ?? '8080'}/${endpoint}`; +} \ No newline at end of file diff --git a/Webserver/src/routes/api/redirect/+server.ts b/Webserver/src/routes/api/redirect/+server.ts index f99b621..e3152f5 100644 --- a/Webserver/src/routes/api/redirect/+server.ts +++ b/Webserver/src/routes/api/redirect/+server.ts @@ -1,4 +1,5 @@ import type { APIResponse } from '$lib/interfaces'; +import { buildAPIStr } from '$lib/server/buildAPIStr'; import { json, type RequestHandler } from '@sveltejs/kit'; // GET Endpoint for redirection API @@ -10,7 +11,7 @@ export const GET: RequestHandler = async ({ url }) => { if (endpoint === null) return json({ success: false } as APIResponse); // Call API and respond with new data - const api_call = await fetch(`http://localhost:8080/${endpoint}`); + const api_call = await fetch(buildAPIStr(endpoint)); const data = (await api_call.json()) as APIResponse; return json(data); }; @@ -26,7 +27,7 @@ export const POST: RequestHandler = async ({ request }) => { // Remove endpoint to save on datatransfer formData.delete('endpoint'); // Call API and respond with new data - const api_call = await fetch(`http://localhost:8080/${endpoint}`, { + const api_call = await fetch(buildAPIStr(endpoint.toString()), { method: 'POST', mode: 'cors', body: formData