mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-05 08:16:55 +08:00
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:
@@ -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 {
|
||||
|
47
www/log.html
47
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 */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<script src="main.js"></script>
|
||||
<div>
|
||||
<button id="clean">Clean</button>
|
||||
<!-- Switch for auto-update -->
|
||||
<button class="switch active" id="autoUpdate">Auto Update: ON</button>
|
||||
</div>
|
||||
<br>
|
||||
<div class="log-viewer" id="log"></div>
|
||||
@@ -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();
|
||||
</script>
|
||||
|
Reference in New Issue
Block a user