refactoring
This commit is contained in:
@@ -2,23 +2,21 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/grpc/metadata"
|
||||
|
||||
"git.loafle.net/commons_go/logging"
|
||||
oas "git.loafle.net/overflow/overflow_api_server/golang"
|
||||
"git.loafle.net/overflow/overflow_gateway_web/grpc"
|
||||
"git.loafle.net/overflow/overflow_gateway_web/server"
|
||||
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
|
||||
"git.loafle.net/overflow/overflow_gateway_websocket/protocol/jsonrpc"
|
||||
ogp "git.loafle.net/overflow/overflow_grpc_pool"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func newWebJSONRpcHandler(ctx context.Context, gPool ogp.Pool) ogw.ProtocolHandler {
|
||||
func newWebJSONRpcHandler(ctx context.Context) ogw.ProtocolHandler {
|
||||
h := &webJSONRpcHandlers{
|
||||
ctx: ctx,
|
||||
logger: logging.WithContext(ctx),
|
||||
gPool: gPool,
|
||||
}
|
||||
|
||||
p := jsonrpc.New(ctx, h)
|
||||
@@ -30,7 +28,6 @@ type webJSONRpcHandlers struct {
|
||||
jsonrpc.JSONRpcHandlers
|
||||
ctx context.Context
|
||||
logger *zap.Logger
|
||||
gPool ogp.Pool
|
||||
}
|
||||
|
||||
func (h *webJSONRpcHandlers) OnRequest(soc ogw.Socket, method string, params []string) (interface{}, error) {
|
||||
@@ -40,30 +37,17 @@ func (h *webJSONRpcHandlers) OnRequest(soc ogw.Socket, method string, params []s
|
||||
zap.Any("params", params),
|
||||
)
|
||||
|
||||
c, err := h.gPool.Get()
|
||||
if err != nil {
|
||||
h.logger.Error("cannot retrive GRPC Client")
|
||||
return nil, err
|
||||
}
|
||||
defer h.gPool.Put(c)
|
||||
uid := server.GetUID(soc)
|
||||
|
||||
sm := strings.Split(method, ".")
|
||||
|
||||
si := &oas.ServerInput{
|
||||
Target: sm[0],
|
||||
Method: sm[1],
|
||||
Params: params,
|
||||
}
|
||||
|
||||
md := metadata.Pairs("email", "overflow@loafle.com")
|
||||
md := metadata.Pairs("email", uid)
|
||||
ctx := metadata.NewOutgoingContext(context.Background(), md)
|
||||
|
||||
so, err := c.(oas.OverflowApiServerClient).Exec(ctx, si)
|
||||
r, err := grpc.Exec(ctx, method, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return so.Result, nil
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (h *webJSONRpcHandlers) OnNotify(soc ogw.Socket, method string, params []string) error {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"git.loafle.net/commons_go/config"
|
||||
"git.loafle.net/commons_go/logging"
|
||||
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
|
||||
ogp "git.loafle.net/overflow/overflow_grpc_pool"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -18,7 +17,7 @@ type webSocketHandlers struct {
|
||||
cfg config.Configurator
|
||||
}
|
||||
|
||||
func NewWebSocketHandler(ctx context.Context, gPool ogp.Pool) ogw.SocketHandler {
|
||||
func NewWebSocketHandler(ctx context.Context) ogw.SocketHandler {
|
||||
h := &webSocketHandlers{
|
||||
ctx: ctx,
|
||||
logger: logging.WithContext(ctx),
|
||||
@@ -32,7 +31,7 @@ func NewWebSocketHandler(ctx context.Context, gPool ogp.Pool) ogw.SocketHandler
|
||||
h.PingPeriod = h.cfg.GetDuration("socket.PingPeriod") * time.Second
|
||||
h.BinaryMessage = h.cfg.GetBool("socket.BinaryMessage")
|
||||
|
||||
ph := newWebJSONRpcHandler(ctx, gPool)
|
||||
ph := newWebJSONRpcHandler(ctx)
|
||||
|
||||
h.Protocol = ph
|
||||
|
||||
|
||||
Reference in New Issue
Block a user