diff --git a/cmd/server/main.go b/cmd/server/main.go index c9127a4..04437e0 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "strconv" "time" "github.com/gin-gonic/gin" @@ -24,6 +25,7 @@ func main() { // Create a new Gin router. router := gin.Default() + serverPort := strconv.Itoa(int(settings.Port)) // Load HTML templates depending on whether the application is running inside a container. _, container := os.LookupEnv("CONTAINER") @@ -38,18 +40,18 @@ func main() { // Register all application routes, including the static file serving route for locales. web.RegisterRoutes(router) - printWelcomeBanner() + printWelcomeBanner(serverPort) log.Println("--- Fail2Ban-UI started in", gin.Mode(), "mode ---") - log.Println("Server listening on port :8080.") + log.Println("Server listening on port", serverPort, ".") // Start the server on port 8080. - if err := router.Run(":8080"); err != nil { + if err := router.Run(":", serverPort); err != nil { log.Fatalf("Server crashed: %v", err) } } // printWelcomeBanner prints a cool Tux banner with startup info. -func printWelcomeBanner() { +func printWelcomeBanner(appPort string) { greeting := getGreeting() const tuxBanner = ` .--. @@ -62,13 +64,13 @@ func printWelcomeBanner() { Fail2Ban UI - A Swissmade Management Interface ---------------------------------------------- -Developers: https://swissmakers.ch -Mode: %s -Listening on: http://0.0.0.0:8080 +Developers: https://swissmakers.ch +Mode: %s +Listening on: http://0.0.0.0:%s ---------------------------------------------- ` - fmt.Printf(tuxBanner, greeting, gin.Mode()) + fmt.Printf(tuxBanner, greeting, gin.Mode(), appPort) } // getGreeting returns a friendly greeting based on the time of day. diff --git a/internal/config/settings.go b/internal/config/settings.go index 10d2154..d666939 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -40,6 +40,7 @@ type SMTPSettings struct { // AppSettings holds the main UI settings and Fail2ban configuration type AppSettings struct { Language string `json:"language"` + Port int `json:"port"` Debug bool `json:"debug"` ReloadNeeded bool `json:"reloadNeeded"` AlertCountries []string `json:"alertCountries"` @@ -57,10 +58,11 @@ type AppSettings struct { // init paths to key-files const ( - settingsFile = "fail2ban-ui-settings.json" // this is relative to where the app was started - jailFile = "/etc/fail2ban/jail.local" // Path to jail.local (to override conf-values from jail.conf) - jailDFile = "/etc/fail2ban/jail.d/ui-custom-action.conf" - actionFile = "/etc/fail2ban/action.d/ui-custom-action.conf" + settingsFile = "fail2ban-ui-settings.json" // this is relative to where the app was started + defaultjailFile = "/etc/fail2ban/jail.conf" + jailFile = "/etc/fail2ban/jail.local" // Path to jail.local (to override conf-values from jail.conf) + jailDFile = "/etc/fail2ban/jail.d/ui-custom-action.conf" + actionFile = "/etc/fail2ban/action.d/ui-custom-action.conf" ) // in-memory copy of settings @@ -97,6 +99,9 @@ func setDefaults() { if currentSettings.Language == "" { currentSettings.Language = "en" } + if currentSettings.Port == 0 { + currentSettings.Port = 8080 + } if currentSettings.AlertCountries == nil { currentSettings.AlertCountries = []string{"ALL"} }