-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (114 loc) · 4.01 KB
/
Copy pathapp.js
File metadata and controls
130 lines (114 loc) · 4.01 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const logBox = document.getElementById("log");
const pathInput = document.getElementById("path");
const argsInput = document.getElementById("args");
const sendBtn = document.getElementById("send");
const pauseBtn = document.getElementById("pause");
const clearBtn = document.getElementById("clear");
const filterSelect = document.getElementById("filter");
const searchInput = document.getElementById("search");
const modeToggle = document.getElementById("mode");
const errorDiv = document.getElementById("error-message");
const settingsBtn = document.getElementById("settings-btn");
const settingsDiv = document.getElementById("settings");
const portInput = document.getElementById("port");
const saveSettingsBtn = document.getElementById("save-settings");
let isPaused = false;
let currentFilter = "ALL";
let currentSearch = "";
let controller = null;
let logLines = [];
let currentPort = localStorage.getItem("freedomCorePort") || 8087;
portInput.value = currentPort;
const getLevelClass = (line) => {
if (line.includes("INFO")) return "INFO";
if (line.includes("WARN")) return "WARN";
if (line.includes("ERROR")) return "ERROR";
if (line.includes("DEBUG")) return "DEBUG";
return "UNKNOWN";
};
const highlightText = (line, search) => {
if (!search) return line;
const regex = new RegExp(`(${search})`, "gi");
return line.replace(regex, '<span class="highlight">$1</span>');
};
const appendLog = (line, level) => {
const span = document.createElement("span");
span.className = `log-line ${level}`;
span.innerHTML = highlightText(line, currentSearch) + "<br />";
logBox.appendChild(span);
logBox.scrollTop = logBox.scrollHeight;
};
const applyFilters = () => {
logBox.innerHTML = "";
logLines.forEach(log => {
if (currentFilter === "ALL" || log.line.includes(currentFilter)) {
if (!currentSearch || log.line.toLowerCase().includes(currentSearch.toLowerCase())) {
appendLog(log.line, log.level);
}
}
});
};
async function* streamingFetch(url) {
controller = new AbortController();
const response = await fetch(url, { signal: controller.signal });
if (!response.body) throw new Error("Response body is null, cannot stream.");
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
yield decoder.decode(value, { stream: true });
}
}
const consumeLogs = async (url) => {
try {
errorDiv.style.display = "none";
for await (const chunk of streamingFetch(url)) {
const lines = chunk.split("\n").filter(Boolean);
lines.forEach(line => {
const level = getLevelClass(line);
logLines.push({line, level});
if (!isPaused) appendLog(line, level);
});
}
} catch {
errorDiv.style.display = "block";
}
};
sendBtn.addEventListener("click", () => {
const path = pathInput.value || "/version";
const args = argsInput.value ? "?" + argsInput.value : "";
consumeLogs(`http://localhost:${currentPort}` + path + args);
});
pauseBtn.addEventListener("click", () => {
isPaused = !isPaused;
pauseBtn.textContent = isPaused ? "Resume" : "Pause";
if (!isPaused) applyFilters();
});
clearBtn.addEventListener("click", () => {
logLines = [];
logBox.innerHTML = "";
});
filterSelect.addEventListener("change", (e) => {
currentFilter = e.target.value;
applyFilters();
});
searchInput.addEventListener("input", (e) => {
currentSearch = e.target.value;
applyFilters();
});
modeToggle.addEventListener("click", () => {
document.body.classList.toggle("light-mode");
document.body.classList.toggle("dark-mode");
});
settingsBtn.addEventListener("click", () => {
settingsDiv.style.display = settingsDiv.style.display === "none" ? "block" : "none";
});
saveSettingsBtn.addEventListener("click", () => {
const port = parseInt(portInput.value) || 8087;
localStorage.setItem("freedomCorePort", port);
location.reload();
});
document.addEventListener("DOMContentLoaded", () => {
consumeLogs(`http://localhost:${currentPort}/logs/stream`);
});