Refactor log handling and add UI auto-update toggle

This commit refactors the log handling in the API to use a switch statement for improved readability and maintainability. It also introduces error messages with more context when reading or truncating the log file fails.

On the frontend, a new auto-update toggle button has been added to the log viewer, allowing users to enable or disable automatic log updates. The button's appearance changes based on its state, providing a clear visual indication of whether auto-update is active. Additionally, the button styling has been updated to ensure consistency across the interface.
This commit is contained in:
Sergey Krashevich
2023-11-28 22:55:50 +03:00
parent 8d382afa0f
commit ab47d5718f
2 changed files with 56 additions and 11 deletions

View File

@@ -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 {