diff --git a/internal/api/api.go b/internal/api/api.go index 67c3a28..fec0234 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -34,7 +34,20 @@ const ( HA_STATE_PREFIX = "homeassistant.states" ) -func Listen(parentLogger *slog.Logger, parentConfig *config.HatsConfig, readme []byte) { +func InitAPI(parentLogger *slog.Logger, parentConfig *config.HatsConfig, readme []byte) { + go func(parentLogger *slog.Logger, parentConfig *config.HatsConfig, readme []byte) { + for { + err := Listen(parentLogger, parentConfig, readme) + if err == nil { + break + } + parentLogger.Error("An error occurred with the API. Restarting now.", "error", err) + Close() + } + }(parentLogger, parentConfig, readme) +} + +func Listen(parentLogger *slog.Logger, parentConfig *config.HatsConfig, readme []byte) error { logger = parentLogger cfg = parentConfig @@ -82,7 +95,7 @@ func Listen(parentLogger *slog.Logger, parentConfig *config.HatsConfig, readme [ Handler: router, } - go server.ListenAndServe() + return server.ListenAndServe() } func Close() { diff --git a/main.go b/main.go index 60ed118..66f0dca 100644 --- a/main.go +++ b/main.go @@ -66,7 +66,7 @@ func main() { ntfy.InitClient(cfg) - api.Listen(logger, cfg, readme) + api.InitAPI(logger, cfg, readme) defer api.Close() for sig := range interrupt {