diff --git a/external/external.go b/external/external.go deleted file mode 100644 index 5aa384c..0000000 --- a/external/external.go +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index 502514e..0000000 --- a/external/grpc/grpc.go +++ /dev/null @@ -1,53 +0,0 @@ -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 6bb6de0..335bc5d 100644 --- a/glide.yaml +++ b/glide.yaml @@ -5,3 +5,5 @@ import: - package: git.loafle.net/overflow/central_api - package: google.golang.org/grpc version: ^1.11.2 +- package: git.loafle.net/overflow/gateway_rest +- package: git.loafle.net/overflow/gateway diff --git a/main.go b/main.go index 8b07efa..d3b2cbe 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "time" "git.loafle.net/commons/logging-go" - "git.loafle.net/overflow/member_gateway_rest/external" "git.loafle.net/overflow/member_gateway_rest/server" ) @@ -20,11 +19,6 @@ func init() { func main() { s := server.NewServer() - external.InitPackage() - defer func() { - external.DestroyPackage() - }() - go func() { err := s.ListenAndServe() if nil != err { diff --git a/server/server-handler.go b/server/server-handler.go index 673196a..b50a426 100644 --- a/server/server-handler.go +++ b/server/server-handler.go @@ -1,21 +1,13 @@ package server import ( - "net" - - "git.loafle.net/commons/server-go" - cswf "git.loafle.net/commons/server-go/web/fasthttp" + cgrs "git.loafle.net/overflow/gateway_rest/server" ) +type ServerHandler interface { + cgrs.ServerHandler +} + type ServerHandlers struct { - cswf.ServerHandlers -} - -func (sh *ServerHandlers) Listener(serverCtx server.ServerCtx) (net.Listener, error) { - l, err := net.Listen("tcp", "192.168.1.101:44449") - if nil != err { - return nil, err - } - - return l, nil + cgrs.ServerHandlers } diff --git a/server/server.go b/server/server.go index eda538e..06a3ce2 100644 --- a/server/server.go +++ b/server/server.go @@ -18,7 +18,7 @@ func NewServer() *cswf.Server { logging.Logger().Error(err) } - webappS := &servlet.WebappServlet{} + webappS := &servlet.WebappServlets{} webappS.ServerCodec = serverCodec webappS.RPCInvoker = rpcRegistry diff --git a/service/member-service.go b/service/member-service.go index 51752dd..f6a0390 100644 --- a/service/member-service.go +++ b/service/member-service.go @@ -4,7 +4,7 @@ import ( "context" "git.loafle.net/commons/server-go" - "git.loafle.net/overflow/member_gateway_rest/external/grpc" + "git.loafle.net/overflow/gateway/external/grpc" "github.com/valyala/fasthttp" ) diff --git a/servlet/rest-servlet.go b/servlet/rest-servlet.go deleted file mode 100644 index 47bf65b..0000000 --- a/servlet/rest-servlet.go +++ /dev/null @@ -1,106 +0,0 @@ -package servlet - -import ( - "fmt" - - crp "git.loafle.net/commons/rpc-go/protocol" - crr "git.loafle.net/commons/rpc-go/registry" - "git.loafle.net/commons/server-go" - csw "git.loafle.net/commons/server-go/web" - cswf "git.loafle.net/commons/server-go/web/fasthttp" - "github.com/valyala/fasthttp" -) - -type MethodMapping struct { - Method string - ParamKeys []string -} - -type RESTServlet struct { - cswf.Servlets - - ServerCodec crp.ServerCodec - RPCInvoker crr.RPCInvoker - - MethodMapping map[string]MethodMapping -} - -func (s *RESTServlet) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error { - method := string(ctx.Method()) - - switch method { - case "GET": - return s.HandleGet(servletCtx, ctx) - case "POST": - return s.HandlePost(servletCtx, ctx) - } - - return nil -} - -func (s *RESTServlet) HandleGet(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error { - path := string(ctx.Path()) - if nil == s.MethodMapping { - return csw.NewError(fasthttp.StatusNotFound, fmt.Errorf("Not Found for %s", path)) - } - mapping, ok := s.MethodMapping[path] - if !ok { - return csw.NewError(fasthttp.StatusNotFound, fmt.Errorf("Not Found for %s", path)) - } - - method := mapping.Method - params := make([]string, 0) - if nil != mapping.ParamKeys && 0 < len(mapping.ParamKeys) { - qargs := ctx.QueryArgs() - if nil == qargs { - return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter is not valied")) - } - - for _, k := range mapping.ParamKeys { - buf := qargs.Peek(k) - if nil == buf { - return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter for %s is not valied", k)) - } - params = append(params, string(buf)) - } - } - - reqCodec, err := s.ServerCodec.NewRequestWithString(method, params, nil) - if nil != err { - return csw.NewError(fasthttp.StatusBadRequest, err) - } - - reply, err := s.RPCInvoker.Invoke(reqCodec, servletCtx, ctx) - - buf, err := reqCodec.NewResponseWithString(reply.(string), err) - if nil != err { - return csw.NewError(fasthttp.StatusInternalServerError, err) - } - - ctx.SetBody(buf) - - return nil -} - -func (s *RESTServlet) HandlePost(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error { - buf := ctx.PostBody() - if nil == buf { - return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter is not valied")) - } - - reqCodec, err := s.ServerCodec.NewRequest(buf) - if nil != err { - return csw.NewError(fasthttp.StatusBadRequest, err) - } - - reply, err := s.RPCInvoker.Invoke(reqCodec, servletCtx, ctx) - - buf, err = reqCodec.NewResponseWithString(reply.(string), err) - if nil != err { - return csw.NewError(fasthttp.StatusInternalServerError, err) - } - - ctx.SetBody(buf) - - return nil -} diff --git a/servlet/webapp-servlet.go b/servlet/webapp-servlet.go index dbebd54..e53f24d 100644 --- a/servlet/webapp-servlet.go +++ b/servlet/webapp-servlet.go @@ -1,5 +1,13 @@ package servlet -type WebappServlet struct { - RESTServlet +import ( + cgrs "git.loafle.net/overflow/gateway_rest/servlet" +) + +type WebappServlet interface { + cgrs.RESTServlet +} + +type WebappServlets struct { + cgrs.RESTServlets }