This commit is contained in:
crusader 2018-04-06 19:47:42 +09:00
parent 36c1017251
commit b0445e9ede
6 changed files with 85 additions and 3 deletions

11
external/external.go vendored Normal file
View File

@ -0,0 +1,11 @@
package external
import "git.loafle.net/overflow/member_gateway_rest/external/grpc"
func InitPackage() {
grpc.InitPackage()
}
func DestroyPackage() {
grpc.DestroyPackage()
}

53
external/grpc/grpc.go vendored Normal file
View File

@ -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
}

View File

@ -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

View File

@ -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 {

View File

@ -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) {

View File

@ -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)
}