aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/routes/(admin)/admin/+page.server.js
blob: f0a6f4426fd1cb82b621f4450d3a2f52ffb36493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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_BASE}/services`, { headers });
	if (!res.ok) {
		return { services: [], error: 'Falha ao carregar serviços', token: token ?? '' };
	}
	const services = await res.json();

	const servicesWithStats = await Promise.all(
		services.map(async (svc) => {
			try {
				const statsRes = await fetch(`${API_BASE}/services/${svc.id}/stats`, { headers });
				if (statsRes.ok) {
					svc.stats = await statsRes.json();
				}
			} catch { /* ignore */ }
			return svc;
		})
	);

	return { services: servicesWithStats, error: null, token: token ?? '' };
}