diff --git a/internal/api/api.go b/internal/api/api.go index d08912df..b8a8c753 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -268,25 +268,27 @@ func restartHandler(w http.ResponseWriter, r *http.Request) { // // No return values are provided since the function writes directly to the response writer. func logHandler(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + logFilePath := app.GetLogFilepath() - if r.Method == "GET" { - data, err := os.ReadFile(app.GetLogFilepath()) + // Send current state of the log file immediately + data, err := os.ReadFile(logFilePath) if err != nil { - http.Error(w, "", http.StatusNotFound) + http.Error(w, "Error reading log file", http.StatusInternalServerError) return } Response(w, data, "text/plain") - } else if r.Method == "DELETE" { + case "DELETE": err := os.Truncate(app.GetLogFilepath(), 0) if err != nil { - http.Error(w, "", http.StatusServiceUnavailable) + http.Error(w, "Error truncating log file", http.StatusServiceUnavailable) return } - } else { - http.Error(w, "", http.StatusBadRequest) - return + Response(w, "Log file deleted", "text/plain") + default: + http.Error(w, "Method not allowed", http.StatusBadRequest) } - } type Source struct { diff --git a/www/log.html b/www/log.html index 4d86a7c3..c07994c9 100644 --- a/www/log.html +++ b/www/log.html @@ -31,12 +31,41 @@ .info { color: #0174DF; } .debug { color: #585858; } .error { color: #DF0101; } + + /* Button styling */ + #clean, .switch { + background-color: #b89d94; + border: none; + color: #695753; + padding: 10px 20px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + margin: 4px 2px; + cursor: pointer; + outline: none; + transition: background-color 0.3s; + } + + /* Switch styling to make it look like a button */ + .switch { + width: auto; + padding: 10px 20px; + background-color: #f4433644; /* Red */ + } + + .switch.active { + background-color: #4caf4f4e; /* Green */ + }
+ +

@@ -44,7 +73,7 @@ const logbody = document.getElementById('log'); document.getElementById('clean').addEventListener('click', async () => { - r = await fetch('api/log', {method: 'DELETE'}); + let r = await fetch('api/log', {method: 'DELETE'}); if (r.ok) { reload(); alert('OK'); @@ -78,6 +107,16 @@ return styledLines.join(''); } + // Handle auto-update switch + const autoUpdateButton = document.getElementById('autoUpdate'); + let autoUpdateEnabled = true; + autoUpdateButton.addEventListener('click', () => { + autoUpdateEnabled = !autoUpdateEnabled; + autoUpdateButton.classList.toggle('active'); + autoUpdateButton.textContent = `Auto Update: ${autoUpdateEnabled ? 'ON' : 'OFF'}`; + }); + + function reload() { const url = new URL('api/log', location.href); fetch(url, {cache: 'no-cache'}) @@ -92,7 +131,11 @@ } // Reload the logs every 5 seconds - setInterval(reload, 5000); + setInterval(() => { + if (autoUpdateEnabled) { + reload(); + } + }, 5000); reload();