1
0
Fork 0

Recoverable API wrapper

main
Jordan Hotmann 2024-04-24 13:48:46 -06:00
parent f558f4e323
commit a9cb361e6b
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
2 changed files with 16 additions and 3 deletions

View File

@ -34,7 +34,20 @@ const (
HA_STATE_PREFIX = "homeassistant.states" 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 logger = parentLogger
cfg = parentConfig cfg = parentConfig
@ -82,7 +95,7 @@ func Listen(parentLogger *slog.Logger, parentConfig *config.HatsConfig, readme [
Handler: router, Handler: router,
} }
go server.ListenAndServe() return server.ListenAndServe()
} }
func Close() { func Close() {

View File

@ -66,7 +66,7 @@ func main() {
ntfy.InitClient(cfg) ntfy.InitClient(cfg)
api.Listen(logger, cfg, readme) api.InitAPI(logger, cfg, readme)
defer api.Close() defer api.Close()
for sig := range interrupt { for sig := range interrupt {