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
This commit is contained in:
Andreas Linde 2022-02-07 10:03:07 +01:00 • committed by GitHub
parent f4e47926bc
commit 5cac3af5c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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())