diff options
| author | zwlucas <lucas.fariamo08@gmail.com> | 2026-05-29 21:24:59 +0000 |
|---|---|---|
| committer | zwlucas <lucas.fariamo08@gmail.com> | 2026-05-29 21:24:59 +0000 |
| commit | bf8b1d0d49703e82cf8162399405984bd0b3fe88 (patch) | |
| tree | af4ed5fb7feae49cb3ebe1c8c3f87831e53b8273 /frontend/src/routes/(admin) | |
| parent | 17b159c563278c3b3b8b3884be37f8d5025ce0c7 (diff) | |
| download | yaum-bf8b1d0d49703e82cf8162399405984bd0b3fe88.tar.gz yaum-bf8b1d0d49703e82cf8162399405984bd0b3fe88.zip | |
feat: implement static file serving and Docker support, update frontend build process
Signed-off-by: zwlucas <lucas.fariamo08@gmail.com>
Diffstat (limited to 'frontend/src/routes/(admin)')
| -rw-r--r-- | frontend/src/routes/(admin)/admin/+page.server.js | 8 | ||||
| -rw-r--r-- | frontend/src/routes/(admin)/admin/+page.svelte | 49 |
2 files changed, 45 insertions, 12 deletions
diff --git a/frontend/src/routes/(admin)/admin/+page.server.js b/frontend/src/routes/(admin)/admin/+page.server.js index bdf498b..f0a6f44 100644 --- a/frontend/src/routes/(admin)/admin/+page.server.js +++ b/frontend/src/routes/(admin)/admin/+page.server.js @@ -1,11 +1,13 @@ -const API = 'http://localhost:8080/api'; +import { building } from '$app/environment'; + +const API_BASE = building ? 'http://localhost:8080/api' : '/api'; export async function load({ locals }) { const token = locals.token; const headers = token ? { Authorization: `Bearer ${token}` } : {}; - const res = await fetch(`${API}/services`, { headers }); + const res = await fetch(`${API_BASE}/services`, { headers }); if (!res.ok) { return { services: [], error: 'Falha ao carregar serviços', token: token ?? '' }; } @@ -14,7 +16,7 @@ export async function load({ locals }) { const servicesWithStats = await Promise.all( services.map(async (svc) => { try { - const statsRes = await fetch(`${API}/services/${svc.id}/stats`, { headers }); + const statsRes = await fetch(`${API_BASE}/services/${svc.id}/stats`, { headers }); if (statsRes.ok) { svc.stats = await statsRes.json(); } diff --git a/frontend/src/routes/(admin)/admin/+page.svelte b/frontend/src/routes/(admin)/admin/+page.svelte index 7fa361d..9a48ba0 100644 --- a/frontend/src/routes/(admin)/admin/+page.svelte +++ b/frontend/src/routes/(admin)/admin/+page.svelte @@ -1,5 +1,7 @@ <script lang="ts"> - const API = 'http://localhost:8080/api'; + import { onMount } from 'svelte'; + + const API = '/api'; let { data } = $props(); @@ -8,15 +10,42 @@ let services = $state<any[]>([]); let showForm = $state(false); - $effect(() => { - if (!initialized && data) { - token = data.token; - services = data.services ?? []; - initialized = true; - fetchMaintenances(); - fetchServerStats(); + function getCookie(name: string) { + return document.cookie.split('; ').find(r => r.startsWith(name + '='))?.split('=')[1] ?? ''; + } + + async function bootstrap() { + if (initialized) return; + initialized = true; + token = data?.token || getCookie('auth_token') || ''; + if (data?.services && data.services.length > 0) { + services = data.services; + } else { + await fetchServices(); } - }); + fetchMaintenances(); + fetchServerStats(); + } + + async function fetchServices() { + try { + const res = await fetch(`${API}/services`, { headers: { Authorization: `Bearer ${token}` } }); + if (await handleUnauthorized(res)) return; + if (res.ok) { + const svcs = await res.json(); + const withStats = await Promise.all( + svcs.map(async (svc: any) => { + try { + const sr = await fetch(`${API}/services/${svc.id}/stats`, { headers: { Authorization: `Bearer ${token}` } }); + if (sr.ok) svc.stats = await sr.json(); + } catch { /* ignore */ } + return svc; + }) + ); + services = withStats; + } + } catch { /* ignore */ } + } let editingSvc: any = $state(null); let saving = $state(false); let formError = $state(''); @@ -24,6 +53,8 @@ let toggling = $state<Record<number, boolean>>({}); // maintenance state + onMount(bootstrap); + let maintenances = $state<any[]>([]); let showMtnForm = $state(false); let editingMtn: any = $state(null); |