Start UI early (#23767)

Co-authored-by: Michael Geers <michael@geers.tv>
This commit is contained in:
andig 2025-11-14 10:19:08 +01:00 • committed by GitHub
parent 6a0cdbb724
commit bb61acc99c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 108 additions and 47 deletions

View file

@ -157,8 +157,6 @@ func runRoot(cmd *cobra.Command, args []string) {
err = networkSettings(&conf.Network)
}
log.INFO.Printf("UI listening at :%d", conf.Network.Port)
// start broadcasting values
tee := new(util.Tee)
valueChan := make(chan util.Param, 64)
@ -172,6 +170,21 @@ func runRoot(cmd *cobra.Command, args []string) {
socketHub := server.NewSocketHub()
httpd := server.NewHTTPd(fmt.Sprintf(":%d", conf.Network.Port), socketHub, customCssFile)
// start serving in background, watch for “routine‐only” errors
go func() {
if err := wrapFatalError(httpd.Server.ListenAndServe()); err != nil && err != http.ErrServerClosed {
log.FATAL.Println(err)
os.Exit(1)
}
}()
log.INFO.Printf("UI listening at :%d", conf.Network.Port)
// publish to UI
go socketHub.Run(pipe.NewDropper(ignoreEmpty).Pipe(tee.Attach()), cache)
// signal ui listening
valueChan <- util.Param{Key: keys.StartupCompleted, Val: false}
// metrics
if viper.GetBool("metrics") {
httpd.Router().Handle("/metrics", promhttp.Handler())
@ -182,9 +195,6 @@ func runRoot(cmd *cobra.Command, args []string) {
httpd.Router().PathPrefix("/debug/").Handler(http.DefaultServeMux)
}
// publish to UI
go socketHub.Run(pipe.NewDropper(ignoreEmpty).Pipe(tee.Attach()), cache)
// capture log messages for UI
util.CaptureLogs(valueChan)
@ -236,8 +246,10 @@ func runRoot(cmd *cobra.Command, args []string) {
}
}
// signal restart
valueChan <- util.Param{Key: keys.Startup, Val: true}
// signal devices initialized
valueChan <- util.Param{Key: keys.StartupCompleted, Val: true}
// show onboarding UI
valueChan <- util.Param{Key: keys.SetupRequired, Val: site == nil || len(site.Loadpoints()) == 0}
// setup mqtt publisher
if err == nil && conf.Mqtt.Broker != "" && conf.Mqtt.Topic != "" {
@ -310,19 +322,6 @@ func runRoot(cmd *cobra.Command, args []string) {
once.Do(func() { close(stopC) }) // signal loop to end
}()
// wait for shutdown
go func() {
<-stopC
select {
case <-shutdownDoneC(): // wait for shutdown
case <-time.After(conf.Interval):
}
// exit code 1 on error
os.Exit(cast.ToInt(err != nil))
}()
// allow web access for vehicles
configureAuth(httpd.Router(), valueChan)
@ -383,5 +382,14 @@ func runRoot(cmd *cobra.Command, args []string) {
// uds health check listener
go server.HealthListener(site)
log.FATAL.Println(wrapFatalError(httpd.ListenAndServe()))
// wait for shutdown
<-stopC
select {
case <-shutdownDoneC(): // wait for shutdown
case <-time.After(conf.Interval):
}
// exit code 1 on error
os.Exit(cast.ToInt(err != nil))
}