Rework to a svelte project
This commit is contained in:
45
src/lib/api.js
Normal file
45
src/lib/api.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const DAEMON_URL = 'http://127.0.0.1:12345';
|
||||
|
||||
export async function fetchInstances() {
|
||||
const res = await fetch(`${DAEMON_URL}/instances/list`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Daemon returned status code: ${res.status}`);
|
||||
}
|
||||
return await res.json();
|
||||
}
|
||||
|
||||
export async function toggleInstanceStatus(name, currentStatus) {
|
||||
const action = currentStatus === 'RUNNING' ? 'stop' : 'start';
|
||||
const res = await fetch(`${DAEMON_URL}/instances/${action}?name=${encodeURIComponent(name)}`, {
|
||||
method: 'POST'
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
throw new Error(errorText || `Action '${action}' failed`);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function sendServerCommand(name, commandString) {
|
||||
const res = await fetch(`${DAEMON_URL}/instances/command?name=${encodeURIComponent(name)}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ command: commandString })
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
throw new Error(errorText || "Failed to route command to target instance");
|
||||
}
|
||||
|
||||
return await res.text();
|
||||
}
|
||||
|
||||
export async function fetchServerLogs(name) {
|
||||
const res = await fetch(`${DAEMON_URL}/instances/logs?name=${encodeURIComponent(name)}`);
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to sync engine logs");
|
||||
}
|
||||
return await res.json();
|
||||
}
|
||||
Reference in New Issue
Block a user