1
0
Fork 0
hats/main.go

78 lines
1.4 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() {
2023-12-20 18:29:23 +00:00
var err error
cfg, err = config.New()
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
}))
2023-12-20 18:29:23 +00:00
if err != nil {
logger.Error("Error during config initialization", "error", err)
}
2023-10-12 17:23:35 +00:00
interrupt = make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
2023-12-20 18:29:23 +00:00
err = nats.JetstreamConnect(ctx, logger, cfg)
2023-10-12 17:23:35 +00:00
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
2023-12-20 18:29:23 +00:00
err = homeassistant.Subscribe(logger, cfg)
2023-10-12 17:23:35 +00:00
if err != nil {
panic(err)
}
defer homeassistant.CloseSubscription()
2023-11-08 23:12:04 +00:00
ntfy.InitClient(cfg)
2024-04-24 19:48:46 +00:00
api.InitAPI(logger, cfg, readme)
2023-10-12 17:23:35 +00:00
defer api.Close()
for sig := range interrupt {
logger.Debug("Interrupt:", "signal", sig.String())
cancel()
break
}
}