2023-03-01 21:57:30 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"net/url"
|
2023-03-16 07:51:21 +00:00
|
|
|
"strconv"
|
2023-03-01 21:57:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NetworkTCP = "tcp"
|
|
|
|
NetworkUnix = "unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerAddress struct {
|
|
|
|
Address string
|
|
|
|
Scheme string
|
|
|
|
UnixSocketPermission fs.FileMode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ServerAddress) Enabled() bool {
|
|
|
|
return s.Address != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ServerAddress) IsUnixSocket() bool {
|
|
|
|
return s.Scheme == NetworkUnix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ServerAddress) Network() string {
|
|
|
|
if s.Scheme == NetworkUnix {
|
|
|
|
return NetworkUnix
|
|
|
|
} else {
|
|
|
|
return NetworkTCP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 07:51:21 +00:00
|
|
|
func UnixSocketAddress(path string, perm string) (ServerAddress, error) {
|
|
|
|
permission, err := strconv.ParseInt(perm, 8, 32)
|
|
|
|
if err != nil {
|
|
|
|
return ServerAddress{}, err
|
|
|
|
}
|
|
|
|
return ServerAddress{Address: path, Scheme: NetworkUnix, UnixSocketPermission: fs.FileMode(permission)}, nil
|
2023-03-01 21:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func HTTPAddress(urlAddress string) (ServerAddress, error) {
|
|
|
|
parsedUrl, err := url.Parse(urlAddress)
|
|
|
|
if err != nil {
|
|
|
|
return ServerAddress{}, err
|
|
|
|
}
|
|
|
|
return ServerAddress{parsedUrl.Host, parsedUrl.Scheme, 0}, nil
|
|
|
|
}
|