From b0445e9ede38b5f8109bf6d6ada06125474512de Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 6 Apr 2018 19:47:42 +0900 Subject: [PATCH] ing --- external/external.go | 11 ++++++++ external/grpc/grpc.go | 53 +++++++++++++++++++++++++++++++++++++++ glide.yaml | 3 +++ main.go | 6 +++++ service/member-service.go | 11 +++++++- servlet/rest-servlet.go | 4 +-- 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 external/external.go create mode 100644 external/grpc/grpc.go diff --git a/external/external.go b/external/external.go new file mode 100644 index 0000000..5aa384c --- /dev/null +++ b/external/external.go @@ -0,0 +1,11 @@ +package external + +import "git.loafle.net/overflow/member_gateway_rest/external/grpc" + +func InitPackage() { + grpc.InitPackage() +} + +func DestroyPackage() { + grpc.DestroyPackage() +} diff --git a/external/grpc/grpc.go b/external/grpc/grpc.go new file mode 100644 index 0000000..502514e --- /dev/null +++ b/external/grpc/grpc.go @@ -0,0 +1,53 @@ +package grpc + +import ( + "context" + "fmt" + "strings" + "sync" + + "git.loafle.net/commons/logging-go" + oci "git.loafle.net/overflow/central_api/golang" + "google.golang.org/grpc" +) + +var grpcClient oci.OverflowApiServerClient + +func InitPackage() { + conn, err := grpc.Dial("192.168.1.50:50006", grpc.WithInsecure()) + if nil != err { + logging.Logger().Panic(err) + } + grpcClient = oci.NewOverflowApiServerClient(conn) + +} + +func DestroyPackage() { + +} + +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.OverflowApiServerClient).Exec(ctx, si) + if nil != err { + return "", err + } + + return so.Result, nil +} diff --git a/glide.yaml b/glide.yaml index abeed13..6bb6de0 100644 --- a/glide.yaml +++ b/glide.yaml @@ -2,3 +2,6 @@ package: git.loafle.net/overflow/member_gateway_rest import: - package: git.loafle.net/commons/server-go - package: git.loafle.net/commons/rpc-go +- package: git.loafle.net/overflow/central_api +- package: google.golang.org/grpc + version: ^1.11.2 diff --git a/main.go b/main.go index d3b2cbe..8b07efa 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "time" "git.loafle.net/commons/logging-go" + "git.loafle.net/overflow/member_gateway_rest/external" "git.loafle.net/overflow/member_gateway_rest/server" ) @@ -19,6 +20,11 @@ func init() { func main() { s := server.NewServer() + external.InitPackage() + defer func() { + external.DestroyPackage() + }() + go func() { err := s.ListenAndServe() if nil != err { diff --git a/service/member-service.go b/service/member-service.go index f9824da..51752dd 100644 --- a/service/member-service.go +++ b/service/member-service.go @@ -1,7 +1,10 @@ package service import ( + "context" + "git.loafle.net/commons/server-go" + "git.loafle.net/overflow/member_gateway_rest/external/grpc" "github.com/valyala/fasthttp" ) @@ -10,7 +13,13 @@ type MemberService struct { func (ms *MemberService) Signin(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, id string, pw string) (string, error) { - return "dkfksddfk", nil + gRPCCtx := context.Background() + r, err := grpc.Exec(gRPCCtx, "MemberService.signin", id, pw) + if nil != err { + return "", err + } + + return r, nil } func (ms *MemberService) Register(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, id string, pw string) (string, error) { diff --git a/servlet/rest-servlet.go b/servlet/rest-servlet.go index 434fa06..47bf65b 100644 --- a/servlet/rest-servlet.go +++ b/servlet/rest-servlet.go @@ -72,7 +72,7 @@ func (s *RESTServlet) HandleGet(servletCtx server.ServletCtx, ctx *fasthttp.Requ reply, err := s.RPCInvoker.Invoke(reqCodec, servletCtx, ctx) - buf, err := reqCodec.NewResponse(reply, err) + buf, err := reqCodec.NewResponseWithString(reply.(string), err) if nil != err { return csw.NewError(fasthttp.StatusInternalServerError, err) } @@ -95,7 +95,7 @@ func (s *RESTServlet) HandlePost(servletCtx server.ServletCtx, ctx *fasthttp.Req reply, err := s.RPCInvoker.Invoke(reqCodec, servletCtx, ctx) - buf, err = reqCodec.NewResponse(reply, err) + buf, err = reqCodec.NewResponseWithString(reply.(string), err) if nil != err { return csw.NewError(fasthttp.StatusInternalServerError, err) }