This commit is contained in:
crusader
2018-04-14 17:57:01 +09:00
parent 45dc0f9920
commit 2fa1e8f5c8
9 changed files with 246 additions and 149 deletions

77
client/central/auth.go Normal file
View File

@@ -0,0 +1,77 @@
package central
import (
"fmt"
"net/http"
"reflect"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
cur "git.loafle.net/commons/util-go/reflect"
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
"git.loafle.net/overflow/probe/auth/info"
)
func NewAuth(tempkeyHandler func(tempkey string), services []interface{}) (*crc.Client, error) {
config := getConfig()
if nil == config {
return nil, fmt.Errorf("Config is not available")
}
authConfig := getAuthConfig()
if nil == authConfig {
return nil, fmt.Errorf("AuthConfig is not available")
}
connector, err := newConnector("Auth", ocncc.HTTPEntry_Auth)
if nil != err {
return nil, err
}
connector.RequestHeader = func() http.Header {
header := make(map[string][]string)
switch authConfig.State() {
case ocnc.AuthStateTypeRegisterd:
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect}
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey] = []string{*authConfig.TempKey}
default:
rh, err := info.GetRegistHeader(config.Account.APIKey)
if nil != err {
logging.Logger().Error(err)
return header
}
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist}
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Info] = []string{rh}
}
return header
}
connector.ResponseHandler = func(res *http.Response) {
switch authConfig.State() {
case ocnc.AuthStateTypeNotRegisterd:
tempProbeKey := res.Header.Get(ocncc.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
if nil != tempkeyHandler {
tempkeyHandler(tempProbeKey)
}
default:
}
}
return newClient("Auth", connector, services), nil
}
func getAuthConfig() *ocnc.Auth {
_config, err := cdr.GetInstanceByName("AuthConfig")
if nil != err {
logging.Logger().Error(err)
return nil
}
config, ok := _config.(*ocnc.Auth)
if !ok {
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_config))
logging.Logger().Errorf("Cannot convert [%s]%s to Config type", pkg, n)
return nil
}
return config
}

75
client/central/cnetral.go Normal file
View File

@@ -0,0 +1,75 @@
package central
import (
"fmt"
"net/url"
"path"
"reflect"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
crpj "git.loafle.net/commons/rpc-go/protocol/json"
crr "git.loafle.net/commons/rpc-go/registry"
csc "git.loafle.net/commons/server-go/client"
csswc "git.loafle.net/commons/server-go/socket/web/client"
cur "git.loafle.net/commons/util-go/reflect"
"git.loafle.net/overflow/probe/config"
)
func newConnector(name string, entryPath string) (*csswc.Connectors, error) {
config := getConfig()
if nil == config {
return nil, fmt.Errorf("Config is not available")
}
u := url.URL{
Scheme: "ws",
Host: config.Central.Address,
}
u.Path = path.Join(u.Path, entryPath)
_connector := config.Central.Connector.Clone()
connector, ok := _connector.(*csswc.Connectors)
if !ok {
return nil, fmt.Errorf("Cannot convert to Connectors type")
}
connector.Name = name
connector.URL = u.String()
return connector, nil
}
func newClient(name string, connector csc.Connector, services []interface{}) *crc.Client {
codec := crpj.NewClientCodec()
var rpcRegistry crr.RPCRegistry
if nil != services && 0 < len(services) {
rpcRegistry = crr.NewRPCRegistry()
rpcRegistry.RegisterServices(services...)
}
return &crc.Client{
Connector: connector,
Codec: codec,
RPCInvoker: rpcRegistry,
Name: name,
}
}
func getConfig() *config.Config {
_config, err := cdr.GetInstanceByName("Config")
if nil != err {
logging.Logger().Error(err)
return nil
}
config, ok := _config.(*config.Config)
if !ok {
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_config))
logging.Logger().Errorf("Cannot convert [%s]%s to Config type", pkg, n)
return nil
}
return config
}

1
client/central/data.go Normal file
View File

@@ -0,0 +1 @@
package central

1
client/central/probe.go Normal file
View File

@@ -0,0 +1 @@
package central