This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
/
converter.go
73 lines (65 loc) · 1.54 KB
/
converter.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package client
import (
"errors"
"fmt"
)
type ConnectionMode int
const (
LocalConnection ConnectionMode = iota
RemoteConnection
)
// Protocol represents a URL scheme to use when fetching connection details
type Protocol int
const (
// WS : Web Socket Protocol
WS Protocol = iota
// WSSUFFIX : Web Socket Protocol
WSSUFFIX
// WSS : Web Socket Secure Protocol
WSS
// HTTP : Hypertext Transfer Protocol
HTTP
// HTTPS : Hypertext Transfer Protocol Secure
HTTPS
POSTGRESQL
)
// URLConverter converts ports to URLs
type URLConverter struct {
ci ConnectionInfo
err error
}
// NewURLConverter creates new URLConverter instance
func NewURLConverter(fp ConnectionInfo, err error) *URLConverter {
return &URLConverter{fp, err}
}
// As converts host/port to an URL
func (m *URLConverter) As(conn ConnectionMode, proto Protocol) (string, error) {
if m.err != nil {
return "", m.err
}
var host string
var port uint16
if conn == RemoteConnection {
host = m.ci.Host
port = m.ci.Ports.Remote
} else {
host = "localhost"
port = m.ci.Ports.Local
}
switch proto {
case HTTP:
return fmt.Sprintf("http://%s:%d", host, port), nil
case HTTPS:
return fmt.Sprintf("https://%s:%d", host, port), nil
case WS:
return fmt.Sprintf("ws://%s:%d", host, port), nil
case WSSUFFIX:
return fmt.Sprintf("ws://%s:%d/ws", host, port), nil
case WSS:
return fmt.Sprintf("wss://%s:%d", host, port), nil
case POSTGRESQL:
return fmt.Sprintf("postgresql://%s:%d", host, port), nil
default:
return "", errors.New("unknown protocol conversion type")
}
}