Network: allow configuring external url (BC) (#25285)

This commit is contained in:
andig 2025-11-14 17:32:48 +01:00 • committed by GitHub
parent bb61acc99c
commit b535d2ed07
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 195 additions and 126 deletions

View file

@ -1,9 +1,11 @@
package globalconfig
import (
"fmt"
"encoding/json"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/api"
@ -11,6 +13,7 @@ import (
"github.com/evcc-io/evcc/plugin/mqtt"
"github.com/evcc-io/evcc/push"
"github.com/evcc-io/evcc/server/eebus"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/config"
"github.com/evcc-io/evcc/util/modbus"
)
@ -152,18 +155,45 @@ type Tariffs struct {
}
type Network struct {
Schema string `json:"schema"`
Host string `json:"host"`
Port int `json:"port"`
Schema_ string `json:"schema,omitempty"` // TODO deprecated
ExternalUrl string `json:"externalUrl"`
Host string `json:"host"`
Port int `json:"port"`
}
func (c Network) HostPort() string {
if c.Schema == "http" && c.Port == 80 || c.Schema == "https" && c.Port == 443 {
return c.Host
host := "localhost"
if h, err := os.Hostname(); err == nil {
host = h
}
return net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
if ips := util.LocalIPs(); len(ips) > 0 {
host = ips[0].IP.String()
}
if c.Port == 80 {
return host
}
return net.JoinHostPort(host, strconv.Itoa(c.Port))
}
func (c Network) URI() string {
return fmt.Sprintf("%s://%s", c.Schema, c.HostPort())
func (c Network) InternalURL() string {
return "http://" + c.HostPort()
}
func (c Network) ExternalURL() string {
if c.ExternalUrl != "" {
return strings.TrimRight(c.ExternalUrl, "/")
}
return c.InternalURL()
}
// MarshalJSON includes the computed InternalUrl field in JSON output
func (c Network) MarshalJSON() ([]byte, error) {
type networkAlias Network
return json.Marshal(struct {
networkAlias
InternalUrl string `json:"internalUrl"`
}{
networkAlias: networkAlias(c),
InternalUrl: c.InternalURL(),
})
}