This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
57 lines (43 loc) · 1.51 KB
/
server.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
45
46
47
48
49
50
51
52
53
54
55
56
57
package srvcache
import (
"fmt"
"net/url"
)
// BasicServer is a representation of a network server host and port, implements Server
type BasicServer struct {
host string
port uint16
scheme string
}
// Host retrieves the host for the server
func (s *BasicServer) Host() string { return s.host }
// SetHost sets the host for the server
func (s *BasicServer) SetHost(host string) { s.host = host }
// Port retrieves the port for the server
func (s *BasicServer) Port() uint16 { return s.port }
// SetPort sets the port for the server
func (s *BasicServer) SetPort(port int) { s.port = uint16(port) }
// Scheme retrieves the url scheme
func (s *BasicServer) Scheme() string { return s.scheme }
// SetScheme sets the url scheme
func (s *BasicServer) SetScheme(scheme string) { s.scheme = scheme }
// HostPort is a string in hostname:port format
func (s *BasicServer) HostPort() string {
return fmt.Sprintf("%s:%d", s.host, s.port)
}
// URL creates a correct url from the server if scheme is known
func (s *BasicServer) URL() (u *url.URL, err error) {
if s.Scheme() == "" {
return u, fmt.Errorf("server %s:%d has no scheme, cannot make a URL", s.host, s.port)
}
ustring := fmt.Sprintf("%s://%s:%d", s.scheme, s.host, s.port)
u, err = url.Parse(ustring)
if err != nil {
return u, fmt.Errorf("could not parse %s: %s", ustring, err)
}
return u, err
}
// String is a string representation of the server in url format
func (s *BasicServer) String() string {
return fmt.Sprintf("%s://%s:%d", s.scheme, s.host, s.port)
}