/
net.go
57 lines (49 loc) · 1.9 KB
/
net.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
package common
import (
"fmt"
"github.com/hellgate75/go-tcp-common/net/common"
"net/http"
"net/url"
)
// Key-Value Pair structure that contains TLS Client/Server Certificate and Key file path pair
type CertificateKeyPair struct {
Cert string
Key string
}
// Generic Rest Client interface
type RestClient interface {
// Authenticate and open connectivity with the server
Open() error
// Close connection and sign-off the server
Close() error
//Send a requerst to the connected server
Request(protocol common.RestProtocol, path string, method common.RestMethod, accepts *common.MimeType, body *[]byte, values *url.Values) (int, []byte, error)
// Returns information about server connectivity state
IsConnected() bool
}
// Generic Rest Callback function for handling pattern request
type RestCallback func(w http.ResponseWriter, req *http.Request, path string, accepts common.MimeType, produces common.MimeType) ()
// Generic Rest Server interface
type RestServer interface {
AddPath(path string, callback RestCallback, accepts *common.MimeType, produces *common.MimeType, allowedMethods []common.RestMethod) bool
AddRootPath(callback RestCallback, accepts *common.MimeType, produces *common.MimeType, allowedMethods []common.RestMethod) bool
StartTLS(hostOrIpAddress string, port int32, certs []CertificateKeyPair, CaCertificate string, insecure bool) error
Start(hostOrIpAddress string, port int32) error
Stop() error
Shutdown() error
IsRunning() bool
WaitFor() error
}
// Structure containing
type HandlerStruct struct {
Handler *RestCallback
Consumes *common.MimeType
Produces *common.MimeType
Path string
Methods []common.RestMethod
}
// String representation of the Handler Strcture
func (hs HandlerStruct) String() string {
return fmt.Sprintf("HandlerStruct{Handler: %v, Comsumes: %s, Produces: %s, Path: %s, Web Methods: %v}",
hs.Handler != nil, *hs.Consumes, *hs.Produces, hs.Path, hs.Methods)
}