api server access in env

This commit is contained in:
waltem01 2023-06-18 18:44:28 +02:00
parent 380e7191ed
commit 044bad3f60
3 changed files with 10 additions and 2 deletions

2
Webserver/.env.example Normal file
View File

@ -0,0 +1,2 @@
API_SERVER_IP=
API_SERVER_PORT=

View File

@ -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}`;
}

View File

@ -1,4 +1,5 @@
import type { APIResponse } from '$lib/interfaces'; import type { APIResponse } from '$lib/interfaces';
import { buildAPIStr } from '$lib/server/buildAPIStr';
import { json, type RequestHandler } from '@sveltejs/kit'; import { json, type RequestHandler } from '@sveltejs/kit';
// GET Endpoint for redirection API // GET Endpoint for redirection API
@ -10,7 +11,7 @@ export const GET: RequestHandler = async ({ url }) => {
if (endpoint === null) return json({ success: false } as APIResponse); if (endpoint === null) return json({ success: false } as APIResponse);
// Call API and respond with new data // 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; const data = (await api_call.json()) as APIResponse;
return json(data); return json(data);
}; };
@ -26,7 +27,7 @@ export const POST: RequestHandler = async ({ request }) => {
// Remove endpoint to save on datatransfer // Remove endpoint to save on datatransfer
formData.delete('endpoint'); formData.delete('endpoint');
// Call API and respond with new data // 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', method: 'POST',
mode: 'cors', mode: 'cors',
body: formData body: formData