mirror of
https://github.com/matrix-org/dendrite
synced 2024-11-09 19:31:11 +01:00
d88f71ab71
### Pull Request Checklist <!-- Please read https://matrix-org.github.io/dendrite/development/contributing before submitting your pull request --> * [x] I have added Go unit tests or [Complement integration tests](https://github.com/matrix-org/complement) for this PR _or_ I have justified why this PR doesn't need tests * [x] Pull request includes a [sign off below using a legally identifiable name](https://matrix-org.github.io/dendrite/development/contributing#sign-off) _or_ I have already signed off privately Signed-off-by: `Boris Rybalkin <ribalkin@gmail.com>`
50 lines
1,017 B
Go
50 lines
1,017 B
Go
package config
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|