1
0
Fork 0
hats/main.go

72 lines
1.3 KiB
Go
Raw Normal View History

2023-10-12 17:23:35 +00:00
package main
import (
"context"
2023-11-17 18:43:50 +00:00
_ "embed"
2023-10-12 17:23:35 +00:00
"log/slog"
"os"
"os/signal"
"code.jhot.me/jhot/hats/internal/api"
"code.jhot.me/jhot/hats/internal/homeassistant"
"code.jhot.me/jhot/hats/internal/nats"
2023-11-08 23:12:04 +00:00
"code.jhot.me/jhot/hats/internal/ntfy"
"code.jhot.me/jhot/hats/pkg/config"
2023-10-12 17:23:35 +00:00
)
2023-11-17 18:43:50 +00:00
//go:embed README.md
var readme []byte
2023-10-12 17:23:35 +00:00
var (
interrupt chan os.Signal
cfg *config.HatsConfig
2023-10-12 17:23:35 +00:00
logger *slog.Logger
ctx context.Context
cancel context.CancelFunc
)
func main() {
cfg = config.FromEnvironment()
2023-10-12 17:23:35 +00:00
ctx, cancel = context.WithCancel(context.Background())
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: cfg.GetLogLevel(),
2023-10-12 17:23:35 +00:00
}))
interrupt = make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
err := nats.JetstreamConnect(ctx, logger)
if err != nil {
panic(err)
}
2023-10-13 20:31:23 +00:00
defer nats.Close()
2023-10-12 17:23:35 +00:00
err = nats.KvConnect()
if err != nil {
panic(err)
}
2023-10-13 20:31:23 +00:00
err = nats.ScheduleStoreConnect()
if err != nil {
panic(err)
}
nats.GetExistingSchedules()
defer nats.StopSchedules()
2023-10-12 17:23:35 +00:00
err = homeassistant.Subscribe(logger)
if err != nil {
panic(err)
}
defer homeassistant.CloseSubscription()
2023-11-08 23:12:04 +00:00
ntfy.InitClient(cfg)
2023-11-17 18:43:50 +00:00
api.Listen(logger, readme)
2023-10-12 17:23:35 +00:00
defer api.Close()
for sig := range interrupt {
logger.Debug("Interrupt:", "signal", sig.String())
cancel()
break
}
}