54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
package grpc
|
|
|
|
import (
|
|
"sync"
|
|
"fmt"
|
|
"strings"
|
|
"context"
|
|
"git.loafle.net/commons/logging-go"
|
|
ocag "git.loafle.net/overflow/central_api/golang"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
var execMtx sync.RWMutex
|
|
|
|
func Exec(ctx context.Context, method string, params []string) (string, error) {
|
|
if nil == grpcClient {
|
|
return "", fmt.Errorf("App: GRPC Client is not initialized")
|
|
}
|
|
|
|
// var client interface{}
|
|
var err error
|
|
// if client, err = grpcPool.Get(); nil != err {
|
|
// return "", err
|
|
// }
|
|
// defer grpcPool.Put(client)
|
|
|
|
sm := strings.Split(method, ".")
|
|
si := &ocag.ServerInput{
|
|
Target: sm[0],
|
|
Method: sm[1],
|
|
Params: params,
|
|
}
|
|
|
|
execMtx.RLock()
|
|
so, err := grpcClient.(ocag.OverflowApiServerClient).Exec(ctx, si)
|
|
if nil != err {
|
|
execMtx.RUnlock()
|
|
return "", err
|
|
}
|
|
execMtx.RUnlock()
|
|
|
|
return so.Result, nil
|
|
}
|
|
|
|
var grpcClient ocag.OverflowApiServerClient
|
|
|
|
func ExternalInit() {
|
|
conn, err := grpc.Dial("192.168.1.50:50006", grpc.WithInsecure())
|
|
if nil != err {
|
|
logging.Logger().Panic(err)
|
|
}
|
|
grpcClient = ocag.NewOverflowApiServerClient(conn)
|
|
|
|
} |