aboutsummaryrefslogtreecommitdiff
path: root/setup/config/config_address.go
blob: a35cc3f96355cbe0a3354bdba590e24fd878964d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
}