From 95befd30fdb6a14c42c10a56b281128fae044a01 Mon Sep 17 00:00:00 2001 From: Michael Reber Date: Thu, 6 Feb 2025 21:41:54 +0100 Subject: [PATCH] Prepare for a multi-language implementation --- cmd/server/main.go | 19 +++++++++++++------ pkg/web/routes.go | 12 ++++++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 05d5889..c9127a4 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -12,36 +12,43 @@ import ( ) func main() { + // Get application settings from the config package. settings := config.GetSettings() - // Set Gin mode based on settings + // Set Gin mode based on the debug flag in settings. if settings.Debug { gin.SetMode(gin.DebugMode) } else { gin.SetMode(gin.ReleaseMode) } + // Create a new Gin router. router := gin.Default() - // To detect if running inside a container or not + // Load HTML templates depending on whether the application is running inside a container. _, container := os.LookupEnv("CONTAINER") if container { - router.LoadHTMLGlob("/app/templates/*") // Load HTML templates + // In container, templates are assumed to be in /app/templates + router.LoadHTMLGlob("/app/templates/*") } else { - router.LoadHTMLGlob("pkg/web/templates/*") // Load HTML templates + // When running locally, load templates from pkg/web/templates + router.LoadHTMLGlob("pkg/web/templates/*") } + + // Register all application routes, including the static file serving route for locales. web.RegisterRoutes(router) printWelcomeBanner() log.Println("--- Fail2Ban-UI started in", gin.Mode(), "mode ---") log.Println("Server listening on port :8080.") + // Start the server on port 8080. if err := router.Run(":8080"); err != nil { log.Fatalf("Server crashed: %v", err) } } -// printWelcomeBanner prints a cool Tux banner with startup info +// printWelcomeBanner prints a cool Tux banner with startup info. func printWelcomeBanner() { greeting := getGreeting() const tuxBanner = ` @@ -64,7 +71,7 @@ Listening on: http://0.0.0.0:8080 fmt.Printf(tuxBanner, greeting, gin.Mode()) } -// getGreeting returns a friendly greeting based on the time of day +// getGreeting returns a friendly greeting based on the time of day. func getGreeting() string { hour := time.Now().Hour() switch { diff --git a/pkg/web/routes.go b/pkg/web/routes.go index def310f..357a201 100644 --- a/pkg/web/routes.go +++ b/pkg/web/routes.go @@ -22,6 +22,10 @@ import ( // RegisterRoutes sets up the routes for the Fail2ban UI. func RegisterRoutes(r *gin.Engine) { + // Serve static files for locales from the "internal/locales" directory. + // (This makes the translation files available under the /locales/ URL.) + r.Static("/locales", "./internal/locales") + // Render the dashboard r.GET("/", IndexHandler) @@ -30,19 +34,19 @@ func RegisterRoutes(r *gin.Engine) { api.GET("/summary", SummaryHandler) api.POST("/jails/:jail/unban/:ip", UnbanIPHandler) - // config endpoints + // Config endpoints api.GET("/jails/:jail/config", GetJailFilterConfigHandler) api.POST("/jails/:jail/config", SetJailFilterConfigHandler) - // settings + // Settings endpoints api.GET("/settings", GetSettingsHandler) api.POST("/settings", UpdateSettingsHandler) api.POST("/settings/test-email", TestEmailHandler) - // filter debugger + // Filter debugger endpoints api.GET("/filters", ListFiltersHandler) api.POST("/filters/test", TestFilterHandler) - // TODO create or generate new filters + // TODO: create or generate new filters // api.POST("/filters/generate", GenerateFilterHandler) // Reload endpoint