107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/config"
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/homeassistant"
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/logger"
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/server"
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/services"
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/udplistener"
|
|
)
|
|
|
|
const stopTimeout = 10 * time.Second
|
|
|
|
type cliArgs struct {
|
|
configPath string
|
|
}
|
|
|
|
func parseArgs() *cliArgs {
|
|
configPath := flag.String("config", "", "Path to the JSON configuration file")
|
|
|
|
flag.Parse()
|
|
|
|
return &cliArgs{
|
|
configPath: *configPath,
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
args := parseArgs()
|
|
|
|
mainCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
conf, err := config.New(args.configPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
logger.Init(conf.LogLevel)
|
|
logger.L.Infof("Initialized logger with level %v", conf.LogLevel)
|
|
|
|
logger.L.Info("Initializing data access service")
|
|
services.NewClientHandlerService(conf.DataFilepath, logger.L).Init()
|
|
|
|
logger.L.Info("Initializing server")
|
|
s, err := server.New(conf, logger.L)
|
|
if err != nil {
|
|
logger.L.Fatalf("Failed to initialize server: %s", err.Error())
|
|
}
|
|
|
|
logger.L.Info("Initializing UDP listener")
|
|
listener, err := udplistener.New(conf, logger.L)
|
|
if err != nil {
|
|
logger.L.Fatalf("Failed to initialize UDP listener: %s", err.Error())
|
|
}
|
|
if err := listener.Init(); err != nil {
|
|
logger.L.Fatalf("Failed to start UDP listener: %s", err.Error())
|
|
}
|
|
|
|
if conf.HomeAssistantEnabled {
|
|
logger.L.Info("Home assistant integration enabled, sending current configuration to the host")
|
|
haClt := homeassistant.New(conf)
|
|
cltSrv := services.NewClientHandlerService(conf.DataFilepath, logger.L)
|
|
clts, err := cltSrv.GetAllClientConfig()
|
|
if err != nil {
|
|
logger.L.Fatalf("Failed to get current clients from the storage: %s", err.Error())
|
|
}
|
|
for _, c := range clts {
|
|
if err := haClt.SendBootOption(context.Background(), c.Name, c.Options[c.SelectedOption].Name); err != nil {
|
|
logger.L.Errorf("Failed to send config to homeassistant: %s", err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
go s.Run(mainCtx)
|
|
go listener.Run(mainCtx)
|
|
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
logger.L.Info("Application successfully started")
|
|
|
|
logger.L.Debug("Waiting for stop signal")
|
|
select {
|
|
case <-s.Done():
|
|
logger.L.Fatal("Unexpected exit from server")
|
|
case <-c:
|
|
logger.L.Info("Stopping main application")
|
|
cancel()
|
|
}
|
|
|
|
logger.L.Debugf("Waiting %v for all daemons to stop", stopTimeout)
|
|
select {
|
|
case <-time.After(stopTimeout):
|
|
logger.L.Fatalf("Failed to stop all daemons in the expected time")
|
|
case <-s.Done():
|
|
logger.L.Info("web server successfully stopped")
|
|
}
|
|
|
|
logger.L.Info("Application successfully stopped")
|
|
os.Exit(0)
|
|
}
|