gateway/external/grpc/grpc.go
crusader f3fa65c8b2 ing
2018-04-25 18:56:25 +09:00

75 lines
1.3 KiB
Go

package grpc
import (
"context"
"fmt"
"strings"
"sync"
"git.loafle.net/commons/logging-go"
oci "git.loafle.net/overflow/central-api/golang"
ocec "git.loafle.net/overflow/commons-go/external/config"
"google.golang.org/grpc"
)
var grpcClient oci.CentralAPIClient
func InitPackage(config *ocec.GRPC) {
if nil == config {
return
}
conn, err := grpc.Dial(config.Address, grpc.WithInsecure())
if nil != err {
logging.Logger().Panic(err)
}
grpcClient = oci.NewCentralAPIClient(conn)
logging.Logger().Infof("GRPC: connected to %s", config.Address)
}
func StartPackage(config *ocec.GRPC) {
if nil == config {
return
}
}
func StopPackage(config *ocec.GRPC) {
if nil == config {
return
}
}
func DestroyPackage(config *ocec.GRPC) {
if nil == config {
return
}
}
var execMtx sync.RWMutex
func Exec(ctx context.Context, method string, params ...string) (string, error) {
if nil == grpcClient {
return "", fmt.Errorf("GRPC Client is not initialized")
}
var err error
sm := strings.Split(method, ".")
si := &oci.ServerInput{
Target: sm[0],
Method: sm[1],
Params: params,
}
execMtx.Lock()
defer execMtx.Unlock()
so, err := grpcClient.(oci.CentralAPIClient).Exec(ctx, si)
if nil != err {
return "", err
}
return so.Result, nil
}