Fix console logger file and reorder routes, add comments for interlnal/external APIs

This commit is contained in:
2026-02-15 20:55:52 +01:00
parent da05afd115
commit 737d634704
2 changed files with 46 additions and 45 deletions

View File

@@ -1,6 +1,6 @@
// Fail2ban UI - A Swiss made, management interface for Fail2ban.
//
// Copyright (C) 2025 Swissmakers GmbH (https://swissmakers.ch)
// Copyright (C) 2026 Swissmakers GmbH (https://swissmakers.ch)
//
// Licensed under the GNU General Public License, Version 3 (GPL-3.0)
// You may not use this file except in compliance with the License.
@@ -25,8 +25,11 @@ import (
"github.com/swissmakers/fail2ban-ui/internal/config"
)
// ConsoleLogWriter is a multi-writer that writes to both the original log output
// and broadcasts to WebSocket clients when console output is enabled
// =========================================================================
// Console log writer that mirrors log output to the WebSocket hub
// so the browser can display server logs in real time.
// =========================================================================
type ConsoleLogWriter struct {
originalWriter io.Writer
hub *Hub
@@ -34,7 +37,6 @@ type ConsoleLogWriter struct {
enabled bool
}
// NewConsoleLogWriter creates a new console log writer
func NewConsoleLogWriter(hub *Hub, originalWriter io.Writer) *ConsoleLogWriter {
return &ConsoleLogWriter{
originalWriter: originalWriter,
@@ -43,25 +45,22 @@ func NewConsoleLogWriter(hub *Hub, originalWriter io.Writer) *ConsoleLogWriter {
}
}
// SetEnabled enables or disables console output broadcasting
func (c *ConsoleLogWriter) SetEnabled(enabled bool) {
c.mu.Lock()
defer c.mu.Unlock()
c.enabled = enabled
}
// Write implements io.Writer interface
// Write sends bytes to the original writer and, when enabled,
// broadcasts the trimmed line to WebSocket clients.
func (c *ConsoleLogWriter) Write(p []byte) (n int, err error) {
// Always write to original writer
n, err = c.originalWriter.Write(p)
// Broadcast to WebSocket if enabled
c.mu.RLock()
enabled := c.enabled
c.mu.RUnlock()
if enabled && c.hub != nil {
// Remove trailing newline for cleaner display
message := string(p)
if len(message) > 0 && message[len(message)-1] == '\n' {
message = message[:len(message)-1]
@@ -70,26 +69,23 @@ func (c *ConsoleLogWriter) Write(p []byte) (n int, err error) {
c.hub.BroadcastConsoleLog(message)
}
}
return n, err
}
// =========================================================================
// Global Setup
// =========================================================================
var globalConsoleLogWriter *ConsoleLogWriter
var consoleLogWriterOnce sync.Once
// SetupConsoleLogWriter sets up the console log writer and replaces the standard log output
// This captures all log.Printf, log.Println, etc. output
func SetupConsoleLogWriter(hub *Hub) {
consoleLogWriterOnce.Do(func() {
// Create a multi-writer that writes to both original stdout and our console writer
globalConsoleLogWriter = NewConsoleLogWriter(hub, os.Stdout)
// Replace log output - this captures all log.Printf, log.Println, etc.
log.SetOutput(globalConsoleLogWriter)
})
}
// UpdateConsoleLogEnabled updates the enabled state based on settings
func UpdateConsoleLogEnabled() {
if globalConsoleLogWriter != nil {
settings := config.GetSettings()
@@ -97,7 +93,6 @@ func UpdateConsoleLogEnabled() {
}
}
// SetConsoleLogEnabled directly sets the enabled state
func SetConsoleLogEnabled(enabled bool) {
if globalConsoleLogWriter != nil {
globalConsoleLogWriter.SetEnabled(enabled)