probe/client/central/probe.go

42 lines
1.3 KiB
Go
Raw Normal View History

2018-04-14 08:57:01 +00:00
package central
2018-04-14 11:04:07 +00:00
import (
"fmt"
"net/http"
2018-04-18 14:56:13 +00:00
logging "git.loafle.net/commons/logging-go"
2018-04-14 11:04:07 +00:00
crc "git.loafle.net/commons/rpc-go/client"
2018-04-18 14:56:13 +00:00
csc "git.loafle.net/commons/server-go/client"
2018-04-26 08:39:32 +00:00
occp "git.loafle.net/overflow/commons-go/config/probe"
2018-04-14 11:04:07 +00:00
"git.loafle.net/overflow/probe/config"
)
2018-04-17 14:11:13 +00:00
func NewProbe(encryptionKeyHandler func(encryptionKey string), services ...interface{}) (*crc.Client, error) {
2018-04-14 11:04:07 +00:00
config := config.GetConfig()
if nil == config {
return nil, fmt.Errorf("Config is not available")
}
2018-04-26 08:39:32 +00:00
connector, err := newConnector("Probe", occp.HTTPEntry_Probe)
2018-04-14 11:04:07 +00:00
if nil != err {
return nil, err
}
connector.RequestHeader = func() http.Header {
header := make(map[string][]string)
2018-04-26 08:39:32 +00:00
header[occp.HTTPRequestHeaderKey_Probe_Method] = []string{occp.HTTPRequestHeaderValue_Probe_Method_Connect}
header[occp.HTTPRequestHeaderKey_Probe_ProbeKey] = []string{*config.Probe.Key}
2018-04-14 11:04:07 +00:00
return header
}
connector.ResponseHandler = func(res *http.Response) {
2018-04-26 08:39:32 +00:00
encryptionKey := res.Header.Get(occp.HTTPResponseHeaderKey_Probe_SetEncryptionKey)
2018-04-14 11:04:07 +00:00
if nil != encryptionKeyHandler {
encryptionKeyHandler(encryptionKey)
}
}
2018-04-18 14:56:13 +00:00
connector.OnDisconnected = func(connector csc.Connector) {
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
}
2018-04-14 11:04:07 +00:00
return newClient("Probe", connector, services), nil
}