-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilities.go
44 lines (38 loc) · 1.02 KB
/
utilities.go
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
package web
import (
"net"
"strings"
"time"
)
// Разбор адреса, определение порта через net.LookupPort, в том числе портов заданных как ":http"
func parseAddress(addr string) (ret *Configuration, err error) {
const bColon = ":"
var (
sp []string
n int
)
addr = strings.TrimSpace(addr)
ret, sp = new(Configuration), make([]string, 2)
if sp[0], sp[1], err = net.SplitHostPort(addr); err != nil {
ret.Host, err = addr, nil
return
}
if n, err = net.LookupPort(netTcp, strings.Join(sp[1:], bColon)); err != nil {
return
}
ret.Host, ret.Port = sp[0], uint16(n)
switch sp, err = net.LookupHost(sp[0]); err {
case nil:
ret.Host = sp[0]
default:
err = nil
}
return
}
// Заполнение конфигурации значениями по умолчанию.
func defaultConfiguration(cfg *Configuration) {
const defaultShutdownTimeout = time.Duration(30) * time.Second
if cfg.ShutdownTimeout == 0 {
cfg.ShutdownTimeout = defaultShutdownTimeout
}
}