probe/client/central/probe.go
crusader d2d34699aa ing
2018-04-18 23:56:13 +09:00

42 lines
1.3 KiB
Go

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