From 5cac3af5c9dc5c06d1dfc82651a9b3d769fa4e3e Mon Sep 17 00:00:00 2001 From: Andreas Linde <42185+DerAndereAndi@users.noreply.github.com> Date: Mon, 7 Feb 2022 10:03:07 +0100 Subject: [PATCH] Provide easier access to evcc web interface (#2492) - Use mdns to provide access to the evcc website via `http://evcc.local:7070` - Correctly shutdown the mdns announcement on exit - Unknown: We need to check how this works in a docker environment, as that may provide unreachable IP addresses --- cmd/root.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index d6646ca6e..af9cd864c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,10 +2,12 @@ package cmd import ( "fmt" + "net" "net/http" _ "net/http/pprof" // pprof handler "os" "os/signal" + "strconv" "sync" "syscall" "time" @@ -16,6 +18,7 @@ import ( "github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util/pipe" "github.com/evcc-io/evcc/util/sponsor" + "github.com/grandcat/zeroconf" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spf13/cobra" @@ -190,6 +193,17 @@ func run(cmd *cobra.Command, args []string) { socketHub := server.NewSocketHub() httpd := server.NewHTTPd(uri, site, socketHub, cache) + // announce webserver on mDNS + if _, port, err := net.SplitHostPort(uri); err == nil { + if portInt, err := strconv.Atoi(port); err == nil { + if zc, err := zeroconf.RegisterProxy("evcc Website", "_http._tcp", "local.", portInt, "evcc", nil, []string{}, nil); err == nil { + shutdown.Register(zc.Shutdown) + } else { + log.ERROR.Printf("mDNS announcement: %s", err) + } + } + } + // metrics if viper.GetBool("metrics") { httpd.Router().Handle("/metrics", promhttp.Handler())